diff --git a/README.md b/README.md index 60c69d0..58a2032 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This is a work in progress, in it's current state is still unusable. *ComputeClient* - Client for Compute Service (Nova) -*StorageClient* - Client for Storage Service (Swift) <- Development hasn't started yet. +*StorageClient* - Client for Storage Service (Swift) <- Development has started *OpenstackClient* - One client to rule them all! diff --git a/src/Guzzle/Openstack/Storage/Command/CreateContainer.php b/src/Guzzle/Openstack/Storage/Command/CreateContainer.php index a24772c..cd706f5 100644 --- a/src/Guzzle/Openstack/Storage/Command/CreateContainer.php +++ b/src/Guzzle/Openstack/Storage/Command/CreateContainer.php @@ -83,7 +83,7 @@ public function setName($name) * * @param int $tenantId * - * @return CreateUser + * @return CreateContainer */ public function setTenantId($tenantId) { diff --git a/src/Guzzle/Openstack/Storage/Command/DeleteContainer.php b/src/Guzzle/Openstack/Storage/Command/DeleteContainer.php new file mode 100644 index 0000000..62bffea --- /dev/null +++ b/src/Guzzle/Openstack/Storage/Command/DeleteContainer.php @@ -0,0 +1,76 @@ + + * @license MIT See the LICENSE file that was distributed with this source code. + * @link https://www.cloudvps.com + */ +namespace Guzzle\Openstack\Storage\Command; + +use Guzzle\Openstack\Common\Command\AbstractJsonCommand; +use Guzzle\Common\Exception\UnexpectedValueException; + +/** + * Delete a container + * + * http://docs.openstack.org/api/openstack-object-storage/1.0/content/delete-container.html + * + * @guzzle name doc="Name of the new container" required="true" + * @guzzle tenantId doc="Tenant id of the new user" required="true" + */ +class DeleteContainer extends AbstractJsonCommand +{ + + /** + * Maximum number of bytes of the container name according to the manual. + * http://docs.openstack.org/trunk/openstack-object-storage/developer/content/create-container.html + */ + const MAX_NAME_BYTES = 256; + + /** + * Set the user name + * + * @param string $name + * + * @return DeleteContainer + * + * @throws \Guzzle\Common\Exception\UnexpectedValueException + */ + public function setName($name) + { + // Slashes are removed + $name = str_replace('/', '', $name); + $bytes = mb_strlen($name, 'latin1'); + if ($bytes <= self::MAX_NAME_BYTES) { + return $this->set('name', $name); + } else { + throw new UnexpectedValueException( + "Container name may be max " . self::MAX_NAME_BYTES . " bytes!"); + } + } + + /** + * Set the tenant id + * + * @param int $tenantId + * + * @return DeleteContainer + */ + public function setTenantId($tenantId) + { + return $this->set('tenantId', $tenantId); + } + + protected function build() + { + $this->request = $this->client->delete( + 'AUTH_' . $this->get('tenantId') . '/' . $this->get('name') + ); + } + +} diff --git a/tests/Guzzle/Openstack/Tests/Storage/Command/DeleteContainerTest.php b/tests/Guzzle/Openstack/Tests/Storage/Command/DeleteContainerTest.php new file mode 100644 index 0000000..d2eb481 --- /dev/null +++ b/tests/Guzzle/Openstack/Tests/Storage/Command/DeleteContainerTest.php @@ -0,0 +1,51 @@ + + * @license MIT See the LICENSE file that was distributed with this source code. + * @link https://www.cloudvps.com + * + */ + +namespace Guzzle\Openstack\Tests\Storage\Command; + +/** + * Create Container command unit test + */ +class DeleteContainerTest extends + \Guzzle\Openstack\Tests\Storage\Common\StorageTestCase +{ + + public function testDeleteContainer() + { + $this->setMockResponse($this->client, 'storage/DeleteContainer'); + $command = $this->client->getCommand('DeleteContainer'); + $command->setTenantId('tenant2'); + $command->setName('container1'); + $command->prepare(); + + //Check method and resource + $this->assertEquals( + 'http://192.168.4.100:8080/v1/AUTH_tenant2/container1', + $command->getRequest()->getUrl() + ); + $this->assertEquals('DELETE', $command->getRequest()->getMethod()); + + //Check for authentication header + $this->assertTrue($command->getRequest()->hasHeader('X-Auth-Token')); + + $this->client->execute($command); + // There's no actual result, so we have to look at the response code and + // check the body is empty. + $result = $command->getResponse(); + + $this->assertEquals(204, $result->getStatusCode()); + $this->assertEquals(0, $result->getContentLength()); + } + +} \ No newline at end of file diff --git a/tests/mock/storage/DeleteContainer b/tests/mock/storage/DeleteContainer new file mode 100644 index 0000000..8b25cb7 --- /dev/null +++ b/tests/mock/storage/DeleteContainer @@ -0,0 +1,5 @@ +HTTP/1.1 204 No Content +Content-Length: 0 +Content-Type: text/html; charset=UTF-8 +X-Trans-Id: tx3fa3857f266f44319d9b8f4bf7ce7fc8 +Date: Mon, 07 Nov 2011 20:42:58 GMT \ No newline at end of file diff --git a/tests/mock/storage/DeleteContainerNotEmpty b/tests/mock/storage/DeleteContainerNotEmpty new file mode 100644 index 0000000..903fbff --- /dev/null +++ b/tests/mock/storage/DeleteContainerNotEmpty @@ -0,0 +1,4 @@ +HTTP/1.1 409 Conflict +Content-Type: application/json; charset=UTF-8 +Content-Length: 0 +Date: Sat, 07 Jan 2012 01:50:36 GMT \ No newline at end of file diff --git a/tests/mock/storage/DeleteContainerNotFound b/tests/mock/storage/DeleteContainerNotFound new file mode 100644 index 0000000..a255d38 --- /dev/null +++ b/tests/mock/storage/DeleteContainerNotFound @@ -0,0 +1,4 @@ +HTTP/1.1 404 Not Found +Content-Type: application/json; charset=UTF-8 +Content-Length: 0 +Date: Sat, 07 Jan 2012 01:50:36 GMT \ No newline at end of file