From 3299840a14c7c4fc45d8ee90e247d6ddac9b8131 Mon Sep 17 00:00:00 2001 From: Technosophos Date: Thu, 31 May 2012 11:34:18 -0500 Subject: [PATCH] Updated CDN and OS to use newFromIdentity(). --- doc/documentation.php | 10 +++++++--- doc/oo-tutorial.md | 3 +++ src/HPCloud/Storage/CDN.php | 22 ++++++++++++++++++++++ src/HPCloud/Storage/ObjectStorage.php | 18 ++++++++++++++++++ test/TestCase.php | 14 +------------- test/Tests/CDNTest.php | 12 ++++++++++++ test/Tests/ObjectStorageTest.php | 12 +++++++++++- 7 files changed, 74 insertions(+), 17 deletions(-) diff --git a/doc/documentation.php b/doc/documentation.php index 25a04de..2bdfeb5 100644 --- a/doc/documentation.php +++ b/doc/documentation.php @@ -171,12 +171,16 @@ * * @code * serviceCatalog('object-storage'); - * $objectStorageUrl = storageList[0]['endpoints'][0]['publicURL']; + * // $storageList = $identity->serviceCatalog('object-storage'); + * // $objectStorageUrl = storageList[0]['endpoints'][0]['publicURL']; * * // Create a new ObjectStorage instance: - * $objectStore = new \HPCloud\Storage\ObjectStorage($token, $objectStorageUrl); + * // $objectStore = new \HPCloud\Storage\ObjectStorage($token, $objectStorageUrl); + * + * // Or let ObjectStorage figure out which instance to use: + * $objectStore = \HPCloud\Storage\ObjectStorage::newFromIdentity($identity); * * // List containers: * print_r($objectStore->containers()); diff --git a/doc/oo-tutorial.md b/doc/oo-tutorial.md index 4f1f4d2..bc48453 100644 --- a/doc/oo-tutorial.md +++ b/doc/oo-tutorial.md @@ -269,6 +269,9 @@ Now we can get a new HPCloud::Storage::ObjectStorage instance: $catalog = $idService->serviceCatalog(); $store = ObjectStorage::newFromServiceCatalog($catalog, $token); + +// UPDATE: As of Beta 6, you can use newFromIdentity(): +// $store = ObjectStorage::newFromIdentity($idService); ?> ~~~ diff --git a/src/HPCloud/Storage/CDN.php b/src/HPCloud/Storage/CDN.php index cf1f17c..ad2bfa0 100644 --- a/src/HPCloud/Storage/CDN.php +++ b/src/HPCloud/Storage/CDN.php @@ -138,6 +138,28 @@ class CDN { */ protected $token; + /** + * Create a new instance from an IdentityServices object. + * + * This builds a new CDN instance form an authenticated + * IdentityServices object. + * + * In the service catalog, this selects the first service entry + * for CDN. At this time, that is sufficient. + * + * @param HPCloud::Services::IdentityServices $identity + * The identity to use. + * @retval object + * A CDN object or FALSE if no CDN services could be found + * in the catalog. + */ + public static function newFromIdentity($identity) { + $tok = $identity->token(); + $cat = $identity->serviceCatalog(); + + return self::newFromServiceCatalog($cat, $tok); + } + /** * Create a new CDN object based on a service catalog. * diff --git a/src/HPCloud/Storage/ObjectStorage.php b/src/HPCloud/Storage/ObjectStorage.php index 9336154..238982a 100644 --- a/src/HPCloud/Storage/ObjectStorage.php +++ b/src/HPCloud/Storage/ObjectStorage.php @@ -163,6 +163,24 @@ class ObjectStorage { return $store; } + /** + * Given an IdentityServices instance, create an ObjectStorage instance. + * + * This constructs a new ObjectStorage from an authenticated instance + * of an HPCloud::Services::IdentityServices object. + * + * @param HPCloud::Services::IdentityServices $identity + * An identity services object that already has a valid token and a + * service catalog. + * @retval object ObjectStorage + * A new ObjectStorage instance. + */ + public static function newFromIdentity($identity) { + $cat = $identity->serviceCatalog(); + $tok = $identity->token(); + return self::newFromServiceCatalog($cat, $tok); + } + /** * Given a service catalog and an token, create an ObjectStorage instance. * diff --git a/test/TestCase.php b/test/TestCase.php index b1e78b0..5d27f87 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -145,19 +145,7 @@ class TestCase extends \PHPUnit_Framework_TestCase { if ($reset || empty(self::$ostore)) { $ident = $this->identity($reset); - $services = $ident->serviceCatalog(\HPCloud\Storage\ObjectStorage::SERVICE_TYPE); - - if (empty($services)) { - throw new \Exception('No object-store service found.'); - } - - /* - //$serviceURL = $services[0]['endpoints'][0]['adminURL']; - $serviceURL = $services[0]['endpoints'][0]['publicURL']; - - $objStore = new \HPCloud\Storage\ObjectStorage($ident->token(), $serviceURL); - */ - $objStore = \HPCloud\Storage\ObjectStorage::newFromServiceCatalog($services, $ident->token()); + $objStore = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident); self::$ostore = $objStore; diff --git a/test/Tests/CDNTest.php b/test/Tests/CDNTest.php index 9fc1d2d..cf2cad5 100644 --- a/test/Tests/CDNTest.php +++ b/test/Tests/CDNTest.php @@ -80,6 +80,18 @@ class CDNTest extends \HPCloud\Tests\TestCase { return $cdn; } + /** + * @depends testConstructor + */ + public function testNewFromIdentity() { + $ident = $this->identity(); + $cdn = CDN::newFromIdentity($ident); + + $this->assertInstanceOf('\HPCloud\Storage\CDN', $cdn); + + return $cdn; + } + /** * @depends testNewFromServiceCatalog */ diff --git a/test/Tests/ObjectStorageTest.php b/test/Tests/ObjectStorageTest.php index 540cffb..73657f5 100644 --- a/test/Tests/ObjectStorageTest.php +++ b/test/Tests/ObjectStorageTest.php @@ -77,7 +77,17 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase { } public function testNewFromServiceCatalog() { - $ostore = $this->objectStore(); + $ident = $this->identity(); + $tok = $ident->token(); + $cat = $ident->serviceCatalog(); + $ostore = \HPCloud\Storage\ObjectStorage::newFromServiceCatalog($cat, $tok); + $this->assertInstanceOf('\HPCloud\Storage\ObjectStorage', $ostore); + $this->assertTrue(strlen($ostore->token()) > 0); + } + + public function testNewFromIdnetity() { + $ident = $this->identity(); + $ostore = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident); $this->assertInstanceOf('\HPCloud\Storage\ObjectStorage', $ostore); $this->assertTrue(strlen($ostore->token()) > 0); }