diff --git a/CHANGELOG.md b/CHANGELOG.md index 46ba27e..d55fb5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ This changelog contains the relevant feature additions and bug fixes. To obtain a complete diff between versions you can got to https://github.com/hpcloud/HPCloud-PHP/compare/XXX...XXX where the XXX values are two different tagged versions of the library. For example, https://github.com/hpcloud/HPCloud-PHP/compare/1.0.0-beta6...1.0.0 +* x.x.x + + * ObjectStorage::newFromIdentity now works for multiple regoins. + * 1.1.0 + * DBaaS::newFromIdentity was modified to support the new base URL format. * All newFromIdentity() constructor functions now support $region diff --git a/src/HPCloud/Services/DBaaS.php b/src/HPCloud/Services/DBaaS.php index 57d09ab..2e75311 100644 --- a/src/HPCloud/Services/DBaaS.php +++ b/src/HPCloud/Services/DBaaS.php @@ -29,6 +29,7 @@ namespace HPCloud\Services; use \HPCloud\Services\DBaaS\Instance; use \HPCloud\Services\DBaaS\Snapshot; +use \HPCloud\Services\DBaaS\Flavor; /** * Database As A Service. @@ -135,6 +136,10 @@ class DBaaS { return new Snapshot($this->token, $this->projectId, $this->url); } + public function flavor() { + return new Flavor($this->token, $this->projectId, $this->url); + } + /** * Get the project ID for this session. * diff --git a/src/HPCloud/Services/DBaaS/Flavor.php b/src/HPCloud/Services/DBaaS/Flavor.php new file mode 100644 index 0000000..d72b4cb --- /dev/null +++ b/src/HPCloud/Services/DBaaS/Flavor.php @@ -0,0 +1,92 @@ +token = $token; + $this->projectId = $projectId; + $this->url = $endpoint; + $this->client = Transport::instance(); + } + + /** + * Retrieve a list of available instance flavors. + * + * @retval array + * @return array + * An array of \HPCloud\Service\DBaaS\Flavor objects listing the available + * flavors. + */ + public function listFlavors() { + $url = $this->url . '/flavors'; + $res = $this->client->doRequest($url, 'GET', $this->headers()); + $json = json_decode($res->content(), TRUE); + + $list = array(); + foreach ($json['flavors'] as $instance) { + $list[] = FlavorDetails::newFromArray($instance); + } + + return $list; + } + + /** + * Get a FlavorDetails object by its name. + * + * @param string $name + * The name of the flavor. + * + * @retval HPCloud::Services::DBaaS::FlavorDetails + * @return \HPCloud\Services\DBaaS\FlavorDetails + * A flavor details object for the flavor. + */ + public function getFlavorByName($name) { + $flavors = $this->listFlavors(); + foreach ($flavors as $k => $v) { + if ($v->name() == $name) { + return $v; + } + } + + // If we got here there was either no flavor with that name or a problem + // getting the flavors. Throw an exception. + throw new Exception("DBaaS Flavor {$name} not available."); + } +} \ No newline at end of file diff --git a/src/HPCloud/Services/DBaaS/FlavorDetails.php b/src/HPCloud/Services/DBaaS/FlavorDetails.php new file mode 100644 index 0000000..5bcfece --- /dev/null +++ b/src/HPCloud/Services/DBaaS/FlavorDetails.php @@ -0,0 +1,124 @@ +links = $array['links']; + $o->ram = $array['ram']; + $o->vcpu = $array['vcpu']; + + if (isset($array['links'][0]) && $array['links'][0]['rel'] == 'self') { + $o->url = $array['links'][0]['href']; + } + + return $o; + } + + public function __construct($name, $id) { + $this->name = $name; + $this->id = $id; + } + + /** + * Get the name of a flavor (e.g., small). + * + * @return string + * The name of a flavor. + */ + public function name() { + return $this->name; + } + + /** + * Get the id of a flavor. + * + * @return int + * The id of a flavor. + */ + public function id() { + return $this->id; + } + + /** + * Get the links for a flavor. + * + * @retval array + * @return array + * Get an array of links for the flavor. + */ + public function links() { + return $this->links; + } + + /** + * Get the callback url for the flavor. + * + * @retval string + * @return string + * The callback url for the flavor. This is in the form + * [DaaSBaseURI]/{tenant_id}/flavors/{flavorId} + */ + public function url() { + return $this->url; + } + + /** + * Get the amount of ram available to this flavor. + * + * @retval int + * @return int + * The amount of ram available to the flavor. + */ + public function ram() { + return $this->ram; + } + + /** + * Get the number of virtual CPUs available to this flavor. + * + * @retval int + * @return int + * The number of virtual CPUs available to the flavor. + */ + public function vcpu() { + return $this->vcpu; + } +} diff --git a/src/HPCloud/Services/DBaaS/Instance.php b/src/HPCloud/Services/DBaaS/Instance.php index 72e957b..461cf5c 100644 --- a/src/HPCloud/Services/DBaaS/Instance.php +++ b/src/HPCloud/Services/DBaaS/Instance.php @@ -85,6 +85,11 @@ class Instance extends Operations { * @see http://api-docs.hpcloud.com/hpcloud-dbaas/1.0/content/instance-create.html */ public function create($name, $flavor = 'medium', $typeSpec = NULL) { + + // Based on the name we need to get the flavor details. + $f = new Flavor($this->token, $this->projectId, $this->url); + $flavorObject = $f->getFlavorByName($flavor); + // Set type spec. As of the initial release of DBaaS, the only support // type is mysql 5.5. if (empty($typeSpec)) { @@ -96,7 +101,7 @@ class Instance extends Operations { $json = array( 'instance' => array( 'name' => $name, - 'flavorRef' => $flavor, + 'flavorRef' => $flavorObject->url(), 'dbtype' => $typeSpec, ), ); diff --git a/src/HPCloud/Storage/ObjectStorage.php b/src/HPCloud/Storage/ObjectStorage.php index ad8e30e..b447403 100644 --- a/src/HPCloud/Storage/ObjectStorage.php +++ b/src/HPCloud/Storage/ObjectStorage.php @@ -181,7 +181,7 @@ class ObjectStorage { public static function newFromIdentity($identity, $region = ObjectStorage::DEFAULT_REGION) { $cat = $identity->serviceCatalog(); $tok = $identity->token(); - return self::newFromServiceCatalog($cat, $tok); + return self::newFromServiceCatalog($cat, $tok, $region); } /** diff --git a/test/Tests/DBaaSFlavorTest.php b/test/Tests/DBaaSFlavorTest.php new file mode 100644 index 0000000..e03a374 --- /dev/null +++ b/test/Tests/DBaaSFlavorTest.php @@ -0,0 +1,68 @@ +dbaas()->flavor()->listFlavors(); + + $this->assertNotEmpty($flavors); + + $this->assertInstanceOf('\HPCloud\Services\DBaaS\FlavorDetails', $flavors[0]); + } + + public function testGetFlavorByName() { + $flavor = $this->dbaas()->flavor(); + + $small = $flavor->getFlavorByName('small'); + + $this->assertInstanceOf('\HPCloud\Services\DBaaS\FlavorDetails', $small); + $this->assertEquals('small', $small->name()); + + // make sure a failure happens well + try { + $foo = $flavor->getFlavorByName('foo'); // This should be a non-real name. + + $this->fail("Found flavor that should not exist."); + + } catch (Exception $e) { + if ($e->getMessage() != 'DBaaS Flavor foo not available.') { + $this->fail('Flavor not found with wrong error message.'); + } + } + } +} \ No newline at end of file diff --git a/test/Tests/ObjectStorageTest.php b/test/Tests/ObjectStorageTest.php index 9bb993e..39844fa 100644 --- a/test/Tests/ObjectStorageTest.php +++ b/test/Tests/ObjectStorageTest.php @@ -100,6 +100,17 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase { $this->assertTrue(strlen($ostore->token()) > 0); } + public function testNewFromIdentityAltRegion() { + $ident = $this->identity(); + $ostore = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident, 'region-b.geo-1'); + $this->assertInstanceOf('\HPCloud\Storage\ObjectStorage', $ostore); + $this->assertTrue(strlen($ostore->token()) > 0); + + // Make sure the store is not the same as the default region. + $ostoreDefault = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident); + $this->assertNotEquals($ostore, $ostoreDefault); + } + /** * @group auth * @ group acl