From 7af18942db6f87aa2ae62e999145861e69d2502e Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Wed, 9 Jan 2013 10:10:33 -0500 Subject: [PATCH] Added framework in for DBaaS Flavors. --- src/HPCloud/Services/DBaaS.php | 5 + src/HPCloud/Services/DBaaS/Flavor.php | 68 ++++++++++ src/HPCloud/Services/DBaaS/FlavorDetails.php | 124 +++++++++++++++++++ test/Tests/DBaaSFlavorTest.php | 43 +++++++ test/Tests/DBaaSInstanceTest.php | 6 + 5 files changed, 246 insertions(+) create mode 100644 src/HPCloud/Services/DBaaS/Flavor.php create mode 100644 src/HPCloud/Services/DBaaS/FlavorDetails.php create mode 100644 test/Tests/DBaaSFlavorTest.php 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..1fcdcab --- /dev/null +++ b/src/HPCloud/Services/DBaaS/Flavor.php @@ -0,0 +1,68 @@ +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; + } +} \ 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/test/Tests/DBaaSFlavorTest.php b/test/Tests/DBaaSFlavorTest.php new file mode 100644 index 0000000..9e33a41 --- /dev/null +++ b/test/Tests/DBaaSFlavorTest.php @@ -0,0 +1,43 @@ +dbaas()->flavor()->listFlavors(); + + $this->assertNotEmpty($flavors); + } +} \ No newline at end of file diff --git a/test/Tests/DBaaSInstanceTest.php b/test/Tests/DBaaSInstanceTest.php index 6771367..8705d9c 100644 --- a/test/Tests/DBaaSInstanceTest.php +++ b/test/Tests/DBaaSInstanceTest.php @@ -52,6 +52,12 @@ class DBaaSInstanceTest extends DBaaSTestCase { $this->assertInstanceOf('\HPCloud\Services\DBaaS\Instance', $inst); } + public function testListFlavors() { + $flavors = $this->dbaas()->flavor()->listFlavors(); + + $this->assertNotEmpty($flavors); + } + public function testCreate() { // Make sure there aren't old fixtures hanging around from a // failed run.