Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
23 changes: 22 additions & 1 deletion src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ class Bucket
protected $id;
protected $name;
protected $type;
protected $options;
protected $corsRules;

/**
* Bucket constructor.
*
* @param $id
* @param $name
* @param $type
* @param $options
* @param $corsRules
*/
public function __construct($id, $name, $type)
public function __construct($id, $name, $type, $options, $corsRules)
Copy link
Contributor

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

{
$this->id = $id;
$this->name = $name;
$this->type = $type;
$this->options = $options;
$this->corsRules = $corsRules;
}

public function getId()
Expand All @@ -39,4 +45,19 @@ public function getType()
{
return $this->type;
}

public function getOptions()
{
return $this->options;
}

public function getCorsRules()
{
return $this->corsRules;
}

public function isS3Compatible()
{
return in_array('s3', $this->getOptions());
}
}
99 changes: 96 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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']);
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return Key[]

*/
public function listKeys(array $options = [])
{
$request = [
'accountId' => $this->accountId,
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to go a step further and use $response['nextApplicationKeyId'] to continually call the API to list keys beyond the first page, similar to how listFiles works with its while (true) loop?

}

/**
* 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*
Expand Down
79 changes: 79 additions & 0 deletions src/Key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace BackblazeB2;

class Key
{
const PERMISSION_LIST_KEYS = 'listKeys';
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also $options returned by the create, list and delete key api calls, please include it in the same way as these.


/**
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}
17 changes: 15 additions & 2 deletions tests/responses/create_bucket_private.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,18 @@
"bucketId" : "4a48fe8875c6214145260818",
"accountId" : "010203040506",
"bucketName" : "Test bucket",
"bucketType" : "allPrivate"
}
"bucketType" : "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
23 changes: 18 additions & 5 deletions tests/responses/create_bucket_public.json
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
}
47 changes: 43 additions & 4 deletions tests/responses/list_buckets_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,58 @@
"bucketId": "4a48fe8875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName" : "Kitten Videos",
"bucketType": "allPrivate"
"bucketType": "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
},
{
"bucketId" : "5b232e8875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName": "Puppy Videos",
"bucketType": "allPublic"
"bucketType": "allPublic",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
},
{
"bucketId": "87ba238875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName": "Vacation Pictures",
"bucketType" : "allPrivate"
"bucketType" : "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
]
}
}
17 changes: 15 additions & 2 deletions tests/responses/update_bucket_to_private.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,18 @@
"accountId": "accountId",
"bucketId": "bucketId",
"bucketName": "test-bucket",
"bucketType": "allPrivate"
}
"bucketType": "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
Loading