-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bucket options and key management #66
base: master
Are you sure you want to change the base?
Changes from all commits
fe3ee8a
1f216e9
02bb1a8
04c3fd9
25ef0a1
4bcd32b
6edc18c
f85c054
86dcb8e
aec7d62
dc8da99
1713182
4429f99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ public function createBucket(array $options) | |
'bucketType' => $options['BucketType'], | ||
]); | ||
|
||
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); | ||
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); | ||
} | ||
|
||
/** | ||
|
@@ -107,7 +107,7 @@ public function updateBucket(array $options) | |
'bucketType' => $options['BucketType'], | ||
]); | ||
|
||
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']); | ||
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']); | ||
} | ||
|
||
/** | ||
|
@@ -127,7 +127,7 @@ public function listBuckets() | |
]); | ||
|
||
foreach ($response['buckets'] as $bucket) { | ||
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']); | ||
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType'], $bucket['options'], $bucket['corsRules']); | ||
} | ||
|
||
return $buckets; | ||
|
@@ -776,6 +776,99 @@ protected function finishLargeFile($fileId, array $sha1s) | |
); | ||
} | ||
|
||
/** | ||
* List key pairs. | ||
* | ||
* @param array $options | ||
* | ||
* @throws ValidationException | ||
* @throws GuzzleException If the request fails. | ||
* @throws B2Exception If the B2 server replies with an error. | ||
* | ||
* @return Key | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
public function listKeys(array $options = []) | ||
{ | ||
$request = [ | ||
'accountId' => $this->accountId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra spacing here which isn't picked up by the linter for some reason, please fix it up while you're at it. |
||
]; | ||
|
||
if (array_key_exists('MaxKeyCount', $options)) { | ||
$request['maxKeyCount'] = $options['MaxKeyCount']; | ||
} | ||
|
||
if (array_key_exists('StartApplicationKeyId', $options)) { | ||
$request['startApplicationKeyId'] = $options['StartApplicationKeyId']; | ||
} | ||
|
||
$response = $this->sendAuthorizedRequest('POST', 'b2_list_keys', $request); | ||
|
||
$keys = []; | ||
foreach ($response['keys'] as $key) { | ||
$keys[] = new Key($key['applicationKeyId'], $key['keyName'], null, $key['capabilities'], $key['bucketId'], $key['namePrefix'], $key['expirationTimestamp']); | ||
} | ||
|
||
return $keys; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to go a step further and use |
||
} | ||
|
||
/** | ||
* Create a key pair for the given bucket and permissions. | ||
* | ||
* @param array $options | ||
* | ||
* @throws ValidationException | ||
* @throws GuzzleException If the request fails. | ||
* @throws B2Exception If the B2 server replies with an error. | ||
* | ||
* @return Key | ||
*/ | ||
public function createKey(array $options) | ||
{ | ||
$request = [ | ||
'accountId' => $this->accountId, | ||
'capabilities' => $options['Capabilities'], | ||
'keyName' => $options['KeyName'], | ||
]; | ||
|
||
if (array_key_exists('BucketId', $options)) { | ||
$request['bucketId'] = $options['BucketId']; | ||
} | ||
|
||
if (array_key_exists('NamePrefix', $options)) { | ||
$request['namePrefix'] = $options['NamePrefix']; | ||
} | ||
|
||
if (array_key_exists('ValidDurationInSeconds', $options)) { | ||
$request['validDurationInSeconds'] = $options['ValidDurationInSeconds']; | ||
} | ||
|
||
$response = $this->sendAuthorizedRequest('POST', 'b2_create_key', $request); | ||
|
||
return new Key($response['applicationKeyId'], $response['keyName'], $response['applicationKey'], $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); | ||
} | ||
|
||
/** | ||
* Delete a key pair. | ||
* | ||
* @param array $options | ||
* | ||
* @throws ValidationException | ||
* @throws GuzzleException If the request fails. | ||
* @throws B2Exception If the B2 server replies with an error. | ||
* | ||
* @return Key | ||
*/ | ||
public function deleteKey(array $options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding a test for each of the new functions? |
||
{ | ||
$request = [ | ||
'applicationKeyId' => $options['ApplicationKeyId'], | ||
]; | ||
|
||
$response = $this->sendAuthorizedRequest('POST', 'b2_delete_key', $request); | ||
|
||
return new Key($response['applicationKeyId'], $response['keyName'], null, $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']); | ||
} | ||
|
||
/** | ||
* Sends a authorized request to b2 API. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace BackblazeB2; | ||
|
||
class Key | ||
{ | ||
const PERMISSION_LIST_KEYS = 'listKeys'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to include these constants? I would expect that we would do something with them, like validate them against the capabilities the dev specified when creating the key, and also to recognise that some capabilities (eg. writeBucketRetentions) are present in the response but cannot be specified in the request. But I don't want to do any of that, I'd rather just remove the consts and let the dev figure it out from the backblaze documentation. |
||
const PERMISSION_WRITE_KEYS = 'writeKeys'; | ||
const PERMISSION_DELETE_KEYS = 'deleteKeys'; | ||
const PERMISSION_LIST_BUCKETS = 'listBuckets'; | ||
const PERMISSION_WRITE_BUCKETS = 'writeBuckets'; | ||
const PERMISSION_DELETE_BUCKETS = 'deleteBuckets'; | ||
const PERMISSION_LIST_FILES = 'listFiles'; | ||
const PERMISSION_READ_FILES = 'readFiles'; | ||
const PERMISSION_SHARE_FILES = 'shareFiles'; | ||
const PERMISSION_WRITE_FILES = 'writeFiles'; | ||
const PERMISSION_DELETE_FILES = 'deleteFiles'; | ||
|
||
protected $id; | ||
protected $name; | ||
protected $secret; | ||
protected $capabilities; | ||
protected $bucketId; | ||
protected $namePrefix; | ||
protected $expirationTimestamp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also |
||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the new params to this docblock. |
||
* Key constructor. | ||
* | ||
* @param $id | ||
* @param $name | ||
* @param $secret | ||
*/ | ||
public function __construct($id, $name, $secret, $capabilities, $bucketId, $namePrefix, $expirationTimestamp) | ||
{ | ||
$this->id = $id; | ||
$this->name = $name; | ||
$this->secret = $secret; | ||
$this->capabilities = $capabilities; | ||
$this->bucketId = $bucketId; | ||
$this->namePrefix = $namePrefix; | ||
$this->expirationTimestamp = $expirationTimestamp; | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getSecret() | ||
{ | ||
return $this->secret; | ||
} | ||
|
||
public function getCapabilities() | ||
{ | ||
return $this->capabilities; | ||
} | ||
|
||
public function getBucketId() | ||
{ | ||
return $this->bucketId; | ||
} | ||
|
||
public function getNamePrefix() | ||
{ | ||
return $this->namePrefix; | ||
} | ||
|
||
public function getExpirationTimestamp() | ||
{ | ||
return $this->expirationTimestamp; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
{ | ||
"bucketId" : "4a48fe8875c6214145260818", | ||
"accountId" : "010203040506", | ||
"bucketName" : "Test bucket", | ||
"bucketType" : "allPublic" | ||
} | ||
"bucketId": "4a48fe8875c6214145260818", | ||
"accountId": "010203040506", | ||
"bucketName": "Test bucket", | ||
"bucketType": "allPublic", | ||
|
||
"bucketInfo": [], | ||
"corsRules": [], | ||
"defaultFileLockConfiguration": { | ||
"mode": null, | ||
"period": null, | ||
"status": "disabled" | ||
}, | ||
"lifecycleRules": [], | ||
"options": [ | ||
"s3" | ||
], | ||
"revision": 2 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're adding the extra fields you can include the remaining ones,
bucketInfo, defaultFileLockConfiguration, lifecycleRules, revision