ObjectStorage now has support for listing.
This commit is contained in:
@@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
namespace HPCloud\Storage;
|
namespace HPCloud\Storage;
|
||||||
|
|
||||||
|
use HPCloud\Storage\ObjectStorage\Container;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access to ObjectStorage (Swift).
|
* Access to ObjectStorage (Swift).
|
||||||
*
|
*
|
||||||
@@ -88,9 +90,11 @@ class ObjectStorage {
|
|||||||
// X-Trans-Id: tx33f1257e09f64bc58f28e66e0577268a
|
// 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;
|
return $store;
|
||||||
}
|
}
|
||||||
@@ -122,7 +126,7 @@ class ObjectStorage {
|
|||||||
* @return string
|
* @return string
|
||||||
* The authentication token.
|
* The authentication token.
|
||||||
*/
|
*/
|
||||||
public function getAuthToken() {
|
public function token() {
|
||||||
return $this->token;
|
return $this->token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +136,55 @@ class ObjectStorage {
|
|||||||
* @return string
|
* @return string
|
||||||
* The URL that is the endpoint for this service.
|
* The URL that is the endpoint for this service.
|
||||||
*/
|
*/
|
||||||
public function getUrl() {
|
public function url() {
|
||||||
return $this->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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
90
src/HPCloud/Storage/ObjectStorage/Container.php
Normal file
90
src/HPCloud/Storage/ObjectStorage/Container.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
*
|
||||||
|
* Contains the class for ObjectStorage Container objects.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace HPCloud\Storage\ObjectStorage;
|
||||||
|
|
||||||
|
class Container implements \Countable {
|
||||||
|
|
||||||
|
protected $properties = array();
|
||||||
|
protected $name = NULL;
|
||||||
|
|
||||||
|
protected $count = 0;
|
||||||
|
protected $bytes = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Container from JSON data.
|
||||||
|
*
|
||||||
|
* This is used in lieue of a standard constructor when
|
||||||
|
* fetching containers from ObjectStorage.
|
||||||
|
*
|
||||||
|
* @param array $jsonArray
|
||||||
|
* An associative array as returned by json_decode($foo, TRUE);
|
||||||
|
*/
|
||||||
|
public static function newFromJSON($jsonArray) {
|
||||||
|
$container = new Container($jsonArray['name']);
|
||||||
|
|
||||||
|
// Access to count and bytes is basically controlled. This is is to
|
||||||
|
// prevent a local copy of the object from getting out of sync with
|
||||||
|
// the remote copy.
|
||||||
|
if (!empty($jsonArray['count'])) {
|
||||||
|
$container->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
|
||||||
|
* <?php
|
||||||
|
* count($container) === $container->count();
|
||||||
|
* ?>
|
||||||
|
* @endcode
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
* The number of items in this container.
|
||||||
|
*/
|
||||||
|
public function count() {
|
||||||
|
return $this->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,13 +4,64 @@
|
|||||||
*
|
*
|
||||||
* Unit tests for ObjectStorage.
|
* Unit tests for ObjectStorage.
|
||||||
*/
|
*/
|
||||||
namespace HPCloud\tests\units;
|
namespace HPCloud\Storage\Tests\Units;
|
||||||
|
|
||||||
require_once 'mageekguy.atoum.phar';
|
require_once 'mageekguy.atoum.phar';
|
||||||
require_once 'src/HPCloud/Bootstrap.php';
|
require_once 'src/HPCloud/Bootstrap.php';
|
||||||
|
require_once 'test/TestCase.php';
|
||||||
|
|
||||||
use \mageekguy\atoum;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user