Updated DBaaS to actually work.

This commit is contained in:
Technosophos
2012-05-22 15:38:46 -05:00
parent db3e788483
commit c7f814108f
3 changed files with 133 additions and 16 deletions

View File

@@ -48,7 +48,7 @@ class Instance {
$res = $this->client->doRequest($url, 'GET', $this->headers());
$json = json_decode($res->content(), TRUE);
return InstanceDetails::newFromJSON($json);
return InstanceDetails::newFromJSON($json['instance']);
}
public function listInstances() {
@@ -108,13 +108,18 @@ class Instance {
$url = $this->url . '/instances';
$postData = json_encode($json);
fwrite(STDOUT, "POST DATA: $postData\n");
$length = strlen($postData);
$headers = $this->headers(array('Accept' => 'application/json', 'Content-length' => $length));
$headers = $this->headers(array(
'Accept' => 'application/json',
'Content-Length' => $length,
'Content-Type' => 'application/json',
));
$res = $this->client->doRequest($url, 'POST', $headers, $postData);
$results = json_decode($res->content(), TRUE);
return InstanceDetails::newFromJSON($results);
return InstanceDetails::newFromJSON($results['instance']);
}
public function delete($instanceId) {

View File

@@ -42,50 +42,155 @@ class InstanceDetails {
public function newFromJSON($json) {
fwrite(STDOUT, json_encode($json));
$o = new InstanceDetails($json['name'], $json['id']);
$o->links = $json['links'];
$o->created = $json['created'];
$o->status = $json['status'];
$o->hostname = $json['hostname'];
$o->port= !empty($json['port']) ? $json['port'] : '3306';
if (!empty($json['hostname'])) {
$o->hostname = $json['hostname'];
$o->port= !empty($json['port']) ? $json['port'] : '3306';
}
if (!empty($json['credential']['username'])) {
$o->username = $json['credential']['username'];
}
if (!empty($json['credential']['password'])) {
$o->username = $json['credential']['pasword'];
$o->password = $json['credential']['password'];
}
return $o;
}
public function __construct($name, $id) {
$this->name = $name;
$this->id = $id;
}
/**
* Get the name of this instance.
*
* @retval string
* The name of the instance.
*/
public function name() {
return $this->name;
}
/**
* Get the ID of the instance.
*
* @retval string
* The ID.
*/
public function id() {
return $this->id;
}
/**
* Get a string expressing the creation time.
*
* @retval string
* A string indicating the creation time.
* Format is in ISO date format.
*/
public function createdOn() {
return $this->created;
}
/**
* Get the status of this instance.
*
* This indicates whether or not the service is available, along with other
* details.
*
* Known status messages:
*- running: Instance is fully operational.
*- building: Instance is being created.
*
* @retval string
* A short status message.
*/
public function status() {
return $this->status;
}
/**
* Get the hostname.
*
* @attention
* In version 1.0 of the DBaaS protocol, this is ONLY available immediately
* after creation.
*
* This returns the DNS name of the host (or possibly an IP address).
*
* @retval string
* The FQDN or IP address of the MySQL server.
*/
public function hostname() {
return $this->hostname;
}
/**
* The port number of the MySQL server.
*
* @retval integer
* The port number. If this is empty, the
* default (3306) should be assumed.
*/
public function port() {
return $this->port;
}
/**
* The username field, if available.
*
* @attention
* Typically this is only available at creation time!
*
* @retval string
* The username for the MySQL instance.
*/
public function username() {
return $this->username;
}
/**
* The password field, if available.
*
* This is the password for this instance's MySQL database.
*
* @attention
* This is only returned when a database is first created.
*
* @retval string
* A password string.
*/
public function password() {
return $this->password;
}
/**
* An array of links about this database.
*
* Format:
* @code
* <?php
* array(
* 0 => array(
* "rel" => "self",
* "url" => "https://some.long/url",
* ),
* );
* ?>
* @endcode
*
* At the time of this writing, there is no definition of what URLs may
* appear here. However, the `self` URL us a URL to the present instance's
* definition.
*
* @retval array
* An array of related links to DBaaS URLs.
*/
public function links() {
return $this->links;
}
@@ -95,6 +200,11 @@ class InstanceDetails {
*
* A convenience function for PDO.
*
* @attention
* This may only be available immediately after the creation of
* the database. The current version of DBaaS does not return
* hostname and port in subsequent queries.
*
* @see http://us3.php.net/manual/en/ref.pdo-mysql.connection.php
*
* @param string $dbName

View File

@@ -48,12 +48,12 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$list = $inst->listInstances();
fwrite(STDOUT, print_r($list, TRUE));
if (!empty($list['instances'])) {
if (!empty($list)) {
$dbName = self::conf('hpcloud.dbaas.database');
foreach ($list['instances'] as $item) {
if ($item['name'] == $dbName) {
fprintf(STDOUT, "Deleting %s (%s)\n", $item['name'], $item['id']);
$inst->delete($item['id']);
foreach ($list as $item) {
if ($item->name() == $dbName) {
fprintf(STDOUT, "Deleting %s (%s)\n", $item->name(), $item->id());
$inst->delete($item->id());
}
}
}
@@ -80,6 +80,8 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
// failed run.
$this->destroyDatabase();
//throw new \Exception("Stopped here.");
$dbName = self::conf('hpcloud.dbaas.database');
$details = $this->inst()->create($dbName, 'small', '3307');
@@ -94,7 +96,7 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$this->assertNotEmpty($details->createdOn());
$this->assertEquals($dbName, $details->name());
$dsn = sprint('mysql:host=%s;port=3307;dbname=foo;charset=utf-8', $details->hostname());
$dsn = sprintf('mysql:host=%s;port=3307;dbname=foo;charset=utf-8', $details->hostname());
$this->assertEquals($dsn, $details->dsn('foo', 'utf-8'));
@@ -169,7 +171,7 @@ class DBaaSInstanceTest extends \HPCloud\Tests\TestCase {
$match = 0;
$dbName = self::conf('hpcloud.dbaas.database');
foreach ($instances['instances'] as $server) {
foreach ($instances as $server) {
$this->assertInstanceOf('\HPCloud\Services\DBaaS\InstanceDetails', $server);
$this->assertNotEmpty($server->id());
if ($server->name() == $dbName) {