From 0946c586699ea3f59d99c7cf1144abc07c8958f0 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 21 Dec 2011 22:18:02 -0600 Subject: [PATCH] ObjectStorage now has support for listing. --- src/HPCloud/Storage/ObjectStorage.php | 60 ++++++++++++- .../Storage/ObjectStorage/Container.php | 90 +++++++++++++++++++ test/Tests/ObjectStorageTest.php | 55 +++++++++++- 3 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 src/HPCloud/Storage/ObjectStorage/Container.php diff --git a/src/HPCloud/Storage/ObjectStorage.php b/src/HPCloud/Storage/ObjectStorage.php index 0eecc46..e8b97f3 100644 --- a/src/HPCloud/Storage/ObjectStorage.php +++ b/src/HPCloud/Storage/ObjectStorage.php @@ -12,6 +12,8 @@ namespace HPCloud\Storage; +use HPCloud\Storage\ObjectStorage\Container; + /** * Access to ObjectStorage (Swift). * @@ -88,9 +90,11 @@ class ObjectStorage { // X-Trans-Id: tx33f1257e09f64bc58f28e66e0577268a - $token = $res->getHeader('X-Auth-Token'); + $token = $res->header('X-Auth-Token'); + $newUrl = $res->header('X-Storage-Url'); - $store = new ObjectStorage($token, $url); + + $store = new ObjectStorage($token, $newUrl); return $store; } @@ -122,7 +126,7 @@ class ObjectStorage { * @return string * The authentication token. */ - public function getAuthToken() { + public function token() { return $this->token; } @@ -132,7 +136,55 @@ class ObjectStorage { * @return string * The URL that is the endpoint for this service. */ - public function getUrl() { + public function url() { return $this->url; } + + public function containers() { + + $url = $this->url() . '?format=json'; + + $containers = $this->get($url); + + $containerList = array(); + foreach ($containers as $container) { + $containerList[$container['name']] = Container::newFromJSON($container); + } + + return $containerList; + } + + /** + * Check to see if this container name exists. + * + * Unless you are working with a huge list of containers, this + * operation is as slow as simply fetching the entire container list. + */ + public function hasContainer($name) { + $containers = $this->containers(); + return isset($containers[$name]); + } + + public function createContainer($name) { + + } + + /** + * Do a GET on Swift. + * + * This is a convenience method that handles the + * most common case of Swift requests. + */ + protected function get($url, $jsonDecode = TRUE) { + $client = \HPCloud\Transport::instance(); + $headers = array( + 'X-Auth-Token' => $this->token(), + ); + + $raw = $client->doRequest($url, 'GET', $headers); + if (!$jsonDecode) { + return $raw; + } + return json_decode($raw->content(), TRUE); + } } diff --git a/src/HPCloud/Storage/ObjectStorage/Container.php b/src/HPCloud/Storage/ObjectStorage/Container.php new file mode 100644 index 0000000..c80899a --- /dev/null +++ b/src/HPCloud/Storage/ObjectStorage/Container.php @@ -0,0 +1,90 @@ +count = $jsonArray['count']; + } + + if (!empty($jsonArray['bytes'])) { + $container->bytes = $jsonArray['bytes']; + } + + return $container; + } + + /** + * Construct a new Container. + */ + public function __construct($name) { + $this->name = $name; + } + + /** + * Get the name of this container. + * + * @return string + * The name of the container. + */ + public function name() { + return $this->name; + } + + /** + * Get the number of bytes in this container. + * + * @return int + * The number of bytes in this container. + */ + public function bytes() { + return $this->bytes; + } + + /** + * Get the number of items in this container. + * + * Since Container implements Countable, the PHP builtin + * count() can be used on a Container instance: + * + * @code + * count(); + * ?> + * @endcode + * + * @return int + * The number of items in this container. + */ + public function count() { + return $this->count(); + } + +} diff --git a/test/Tests/ObjectStorageTest.php b/test/Tests/ObjectStorageTest.php index 0ef546c..aa5ffa5 100644 --- a/test/Tests/ObjectStorageTest.php +++ b/test/Tests/ObjectStorageTest.php @@ -4,13 +4,64 @@ * * Unit tests for ObjectStorage. */ -namespace HPCloud\tests\units; +namespace HPCloud\Storage\Tests\Units; require_once 'mageekguy.atoum.phar'; require_once 'src/HPCloud/Bootstrap.php'; +require_once 'test/TestCase.php'; use \mageekguy\atoum; -class ObjectStorage extends atoum\test { +class ObjectStorage extends \HPCloud\TestCase { + + protected function auth() { + + static $ostore = NULL; + + if (empty($ostore)) { + $user = $this->settings['hpcloud.swift.account']; + $key = $this->settings['hpcloud.swift.key']; + $url = $this->settings['hpcloud.swift.url']; + + $ostore = \HPCloud\Storage\ObjectStorage::newFromSwiftAuth($user, $key, $url); + } + + return $ostore; + } + + /** + * Canary test. + */ + public function testSettings() { + $this->assert->array($this->settings)->isNotEmpty(); + } + + /** + * Test Swift-based authentication. + * */ + public function testAuthentication() { + + $ostore = $this->auth(); + + $this + ->assert->object($ostore)->isInstanceOf('\HPCloud\Storage\ObjectStorage') + ->assert->string($ostore->token())->isNotEmpty(); + } + + /** + * Test the process of fetching a list of containers. + * + * @FIXME This needs to be updated to check an actual container. + */ + public function testContainers() { + $store = $this->auth(); + $containers = $store->containers(); + + $this->assert->array($containers)->isNotEmpty(); + + $first = array_shift($containers); + + $this->assert->string($first->name())->isNotEmpty(); + } }