HPCloud-PHP  1.2.0
PHP bindings for HPCloud and OpenStack services.
 All Classes Namespaces Files Functions Variables Pages
Instance.php
Go to the documentation of this file.
1 <?php
2 /* ============================================================================
3 (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
8 the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 ============================================================================ */
22 /**
23  * @file
24  *
25  * This file contains the DBaaS Instance class.
26  */
27 
28 namespace HPCloud\Services\DBaaS;
29 
30 use \HPCloud\Transport;
31 
32 class Instance extends Operations {
33 
34  protected $token;
35  protected $projectId;
36  protected $url;
37  protected $client;
38 
39  public function __construct($token, $projectId, $endpoint) {
40  $this->token = $token;
41  $this->projectId = $projectId;
42  $this->url = $endpoint;
43  $this->client = Transport::instance();
44  }
45 
46  public function describe($instanceId) {
47  $url = sprintf('%s/instances/%s', $this->url, $instanceId);
48  $res = $this->client->doRequest($url, 'GET', $this->headers());
49 
50  $json = json_decode($res->content(), TRUE);
51  return InstanceDetails::newFromJSON($json['instance']);
52  }
53 
54  public function listInstances() {
55  $url = $this->url . '/instances';
56  $res = $this->client->doRequest($url, 'GET', $this->headers());
57  $json = json_decode($res->content(), TRUE);
58 
59  $list = array();
60  foreach ($json['instances'] as $instance) {
61  $list[] = InstanceDetails::newFromJSON($instance);
62  }
63  return $list;
64  }
65 
66  /**
67  * Create a new database.
68  *
69  * This creates a database tuned according to the $flavor settings. The
70  * return data will include login/password and connection information.
71  *
72  * @attention
73  * This is the only time that the login and password info will be returned.
74  *
75  * @param string $name
76  * The name of the database.
77  * @param string $flavor
78  * The string flavor name. Known values are:
79  *- medium
80  * @param array $typeSpec
81  * A typespec array. Currently, only 'mysql', '5.5' is supported.
82  * @retval HPCloud::Services::DBaaS::InstanceDetails
83  * @return \HPCloud\Services\DBaaS\InstanceDetails
84  * The details of creation, including login and password info.
85  * @see http://api-docs.hpcloud.com/hpcloud-dbaas/1.0/content/instance-create.html
86  */
87  public function create($name, $flavor = 'medium', $typeSpec = NULL) {
88 
89  // Based on the name we need to get the flavor details.
90  $f = new Flavor($this->token, $this->projectId, $this->url);
91  $flavorObject = $f->getFlavorByName($flavor);
92 
93  // Set type spec. As of the initial release of DBaaS, the only support
94  // type is mysql 5.5.
95  if (empty($typeSpec)) {
96  $typeSpec = array(
97  'name' => 'mysql',
98  'version' => '5.5',
99  );
100  }
101  $json = array(
102  'instance' => array(
103  'name' => $name,
104  'flavorRef' => $flavorObject->url(),
105  'dbtype' => $typeSpec,
106  ),
107  );
108  $url = $this->url . '/instances';
109  $postData = json_encode($json);
110  //fwrite(STDOUT, "POST DATA: $postData\n");
111  $length = strlen($postData);
112  $headers = $this->headers(array(
113  'Accept' => 'application/json',
114  'Content-Length' => $length,
115  'Content-Type' => 'application/json',
116  ));
117  $res = $this->client->doRequest($url, 'POST', $headers, $postData);
118 
119  $results = json_decode($res->content(), TRUE);
120 
121  return InstanceDetails::newFromJSON($results['instance']);
122  }
123 
124  public function delete($instanceId) {
125  $url = sprintf('%s/instances/%s', $this->url, $instanceId);
126  $this->client->doRequest($url, 'DELETE', $this->headers());
127  return TRUE;
128  }
129 
130  public function restart($instanceId) {
131  $url = sprintf('%s/instances/%s/restart', $this->url, $instanceId);
132  $headers = $this->headers(array('Content-Length' => '0'));
133  $this->client->doRequest($url, 'POST', $headers);
134  return TRUE;
135  }
136 
137  /**
138  * Reset the primary password on this instance.
139  *
140  * @retval string
141  * @return string
142  * The new (autogenerated) password.
143  */
144  public function resetPassword($instanceId) {
145  $url = sprintf('%s/instances/%s/resetpassword', $this->url, $instanceId);
146  $headers = $this->headers(array('Content-Length' => '0'));
147  $res = $this->client->doRequest($url, 'POST', $headers);
148  $json = json_decode($res);
149 
150  return $json->password;
151  }
152 
153  /*
154  protected function headers($add = array()) {
155  return $add + array(
156  'X-Auth-Token' => $this->token,
157  'X-Auth-Project-Id' => $this->projectId,
158  );
159  }
160  */
161 }