HPCloud-PHP  1.2.0
PHP bindings for HPCloud and OpenStack services.
 All Classes Namespaces Files Functions Variables Pages
InstanceDetails.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 HPCloud::DBaaS::InstanceDetails class.
26  */
27 
28 namespace HPCloud\Services\DBaaS;
29 
31 
32  protected $name;
33  protected $id;
34  protected $links;
35  protected $created;
36  protected $status;
37  protected $hostname;
38 
39  protected $username;
40  protected $password;
41 
42  public function newFromJSON($json) {
43 
44  //fwrite(STDOUT, json_encode($json));
45 
46  $o = new InstanceDetails($json['name'], $json['id']);
47  $o->links = $json['links'];
48  $o->created = $json['created'];
49  $o->status = $json['status'];
50  if (!empty($json['hostname'])) {
51  $o->hostname = $json['hostname'];
52  }
53 
54  if (!empty($json['credential']['username'])) {
55  $o->username = $json['credential']['username'];
56  }
57  if (!empty($json['credential']['password'])) {
58  $o->password = $json['credential']['password'];
59  }
60 
61  if (!empty($json['name'])) {
62  $o->name = $json['name'];
63  }
64 
65  return $o;
66  }
67 
68  public function __construct($name, $id) {
69  $this->name = $name;
70  $this->id = $id;
71  }
72 
73  /**
74  * Get the name of this instance.
75  *
76  * @retval string
77  * @return string
78  * The name of the instance.
79  */
80  public function name() {
81  return $this->name;
82  }
83 
84  /**
85  * Get the ID of the instance.
86  *
87  * @retval string
88  * @return string
89  * The ID.
90  */
91  public function id() {
92  return $this->id;
93  }
94 
95  /**
96  * Get a string expressing the creation time.
97  *
98  * This may only be set during CREATE or DESCRIBE results.
99  *
100  * @retval string
101  * @return string
102  * A string indicating the creation time.
103  * Format is in ISO date format.
104  */
105  public function createdOn() {
106  return $this->created;
107  }
108 
109  /**
110  * Get the status of this instance.
111  *
112  * This indicates whether or not the service is available, along with other
113  * details.
114  *
115  * Known status messages:
116  *- running: Instance is fully operational.
117  *- building: Instance is being created.
118  *- restarting: Instance has been restarted, and is still coming online.
119  *
120  * @retval string
121  * @return string
122  * A short status message.
123  */
124  public function status() {
125  return $this->status;
126  }
127 
128  /**
129  * Check whether the present instance is running.
130  *
131  * This is a convenience function for determining whether a remote
132  * instance reports itself to be running. It is equivalent to
133  * checking that status() returns 'running'.
134  *
135  * @retval boolean
136  * @return boolean
137  * TRUE if this is running, FALSE otherwise.
138  */
139  public function isRunning() {
140  return strcasecmp($this->status(), 'running') == 0;
141  }
142 
143  /**
144  * Get the hostname.
145  *
146  * Note that the port is always 3306, the MySQL default. Only the hostname
147  * is returned.
148  *
149  * @attention
150  * In version 1.0 of the DBaaS protocol, this is ONLY available after the
151  * DB instance has been brought all the way up.
152  *
153  * This returns the DNS name of the host (or possibly an IP address).
154  *
155  * @retval string
156  * @return string
157  * The FQDN or IP address of the MySQL server.
158  */
159  public function hostname() {
160  return $this->hostname;
161  }
162 
163  /**
164  * Set the hostname.
165  *
166  * @param string $hostname
167  * The hostname for this server.
168  *
169  * @retval HPCloud::Services::DBaaS::InstanceDetails
170  * @return \HPCloud\Services\DBaaS\InstanceDetails
171  * $this so the method can be used in chaining.
172  */
173  public function setHostname($hostname) {
174  $this->hostname = $hostname;
175 
176  return $this;
177  }
178 
179  /**
180  * The username field, if available.
181  *
182  * @attention
183  * Typically this is only available at creation time!
184  *
185  * @retval string
186  * @return string
187  * The username for the MySQL instance.
188  */
189  public function username() {
190  return $this->username;
191  }
192 
193  /**
194  * Set the username.
195  *
196  * @param string $username
197  * The username for this server.
198  *
199  * @retval HPCloud::Services::DBaaS::InstanceDetails
200  * @return \HPCloud\Services\DBaaS\InstanceDetails
201  * $this so the method can be used in chaining.
202  */
203  public function setUsername($username) {
204  $this->username = $username;
205 
206  return $this;
207  }
208 
209  /**
210  * The password field, if available.
211  *
212  * This is the password for this instance's MySQL database.
213  *
214  * @attention
215  * This is only returned when a database is first created.
216  *
217  * @retval string
218  * @return string
219  * A password string.
220  */
221  public function password() {
222  return $this->password;
223  }
224 
225  /**
226  * Set the password.
227  *
228  * @param string $password
229  * The password for this server.
230  *
231  * @retval HPCloud::Services::DBaaS::InstanceDetails
232  * @return \HPCloud\Services\DBaaS\InstanceDetails
233  * $this so the method can be used in chaining.
234  */
235  public function setPassword($password) {
236  $this->password = $password;
237 
238  return $this;
239  }
240 
241  /**
242  * An array of links about this database.
243  *
244  * Format:
245  * @code
246  * <?php
247  * array(
248  * 0 => array(
249  * "rel" => "self",
250  * "url" => "https://some.long/url",
251  * ),
252  * );
253  * ?>
254  * @endcode
255  *
256  * At the time of this writing, there is no definition of what URLs may
257  * appear here. However, the `self` URL us a URL to the present instance's
258  * definition.
259  *
260  * @retval array
261  * @return array
262  * An array of related links to DBaaS URLs.
263  */
264  public function links() {
265  return $this->links;
266  }
267 
268  /**
269  * Get the DSN to connect to the database instance.
270  *
271  * A convenience function for PDO.
272  *
273  * @see http://us3.php.net/manual/en/ref.pdo-mysql.connection.php
274  *
275  * @param string $dbName
276  * The name of the database to connect to. If none is specified,
277  * this will be left off of the DSN.
278  * @param string $charset
279  * This will attempt to set the character set. Not all versions
280  * of PHP use this.
281  *
282  * @retval string
283  * @return string
284  * The DSN, including driver, host, port, and database name.
285  * @todo
286  * At this time, 'mysql' is hard-coded as the driver name. Does this
287  * need to change?
288  */
289  public function dsn($dbName = NULL, $charset = NULL) {
290  $dsn = sprintf('mysql:host=%s;port=3306', $this->hostname());
291  if (!empty($dbName)) {
292  $dsn .= ';dbname=' . $dbName;
293  }
294  if (!empty($charset)) {
295  $dsn .= ';charset=' . $charset;
296  }
297 
298  return $dsn;
299 
300  }
301 
302 }