Merge branch 'develop'

This commit is contained in:
Matt Farina
2013-01-10 17:14:42 -05:00
8 changed files with 312 additions and 2 deletions

View File

@@ -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

View File

@@ -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.
*

View File

@@ -0,0 +1,92 @@
<?php
/* ============================================================================
(c) Copyright 2013 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
/**
* @file
* This file contains the Database Flavor class.
*/
namespace HPCloud\Services\DBaaS;
use \HPCloud\Transport;
use \HPCloud\Exception;
/**
* Class for working with Database Flavors.
*/
class Flavor extends Operations {
protected $token;
protected $projectId;
protected $url;
protected $client;
public function __construct($token, $projectId, $endpoint) {
$this->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.");
}
}

View File

@@ -0,0 +1,124 @@
<?php
/* ============================================================================
(c) Copyright 2013 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
/**
* @file
* This file contains the Database FlavorDetails class.
*/
namespace HPCloud\Services\DBaaS;
/**
* Class for working with Database Flavors Details.
*/
class FlavorDetails {
protected $name;
protected $id;
protected $url;
protected $links;
protected $ram;
protected $vcpu;
public static function newFromArray(array $array) {
$o = new FlavorDetails($array['name'], $array['id']);
$o->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;
}
}

View File

@@ -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,
),
);

View File

@@ -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);
}
/**

View File

@@ -0,0 +1,68 @@
<?php
/* ============================================================================
(c) Copyright 2013 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
/**
* @file
*
* Unit tests for HPCloud::DBaaS::Flavor.
*/
namespace HPCloud\Tests\Services\DBaaS;
require_once __DIR__ . '/DBaaSTestCase.php';
use \HPCloud\Services\DBaaS;
use \HPCloud\Services\DBaaS\Flavor;
use \HPCloud\Services\DBaaS\FlavorDetails;
use \HPCloud\Exception;
/**
* @group dbaas
*/
class DBaaSFlavorTest extends DBaaSTestCase {
public function testListFlavors() {
$flavors = $this->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.');
}
}
}
}

View File

@@ -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