HPCloud-PHP  1.2.0
PHP bindings for HPCloud and OpenStack services.
 All Classes Namespaces Files Functions Variables Pages
DBaaS.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 main Database as a Service class.
26  */
27 
28 namespace HPCloud\Services;
29 
30 use \HPCloud\Services\DBaaS\Instance;
31 use \HPCloud\Services\DBaaS\Snapshot;
32 use \HPCloud\Services\DBaaS\Flavor;
33 
34 /**
35  * Database As A Service.
36  *
37  * This package provides access to HP Cloud's Database As A Service (DBaaS)
38  * service.
39  *
40  * ## About DBaaS
41  *
42  * DBaaS is a service for creating and managing database instances and
43  * snapshots (backups) of that service. It is not a data access layer.
44  * That is, SQL is not proxied through this service. Rather, once a
45  * database instance is set up, apps may connect directly to that service
46  * using built-in drivers. See, for example,
47  * HPCloud::Services::DBaaS::Instance::dsn(), which returns the PDO DSN for
48  * connecting to a MySQL database.
49  *
50  * To create and manage new database servers, you will work with
51  * HPCloud::Services::DBaaS::instance().
52  *
53  * To take snapshots of an existing database server (which is recommended
54  * at regular intervals), you will work with
55  * HPCloud::Services::DBaaS::snapshot().
56  *
57  * ## Authentication to the Service
58  *
59  * To authenticate to the service, you will use IdentityServices. Note,
60  * however, that DBaaS requires a Tenant Name (not Tenant ID). After
61  * authentication, this will attempt to retrieve the name from IdentityServices.
62  *
63  * ## Authentication to an Instance
64  *
65  * Upon creating a new database instance, this library will return login
66  * credentials (username and password) from
67  * HPCloud::Services::DBaaS::Instance::create(). <i>This is the only time
68  * that credentials are returned</i>. Make note of them.
69  *
70  * Those credentials can then be used to connect to the database instance,
71  * and create new databases, users, and tables.
72  */
73 class DBaaS {
74 
75  const SERVICE_TYPE = 'hpext:dbaas';
76 
77  const API_VERSION = '1';
78 
79  const DEFAULT_REGION = 'region-a.geo-1';
80 
81  /**
82  * The auth token for the current session.
83  */
84  protected $token;
85  /**
86  * The base URL to the DBaaS for a given account.
87  */
88  protected $url;
89  /**
90  * The tenant name.
91  *
92  * Typically, this is an email address.
93  */
94  protected $projectId;
95 
96  public static function newFromIdentity($identity, $region = DBaaS::DEFAULT_REGION) {
97 
98  $catalog = $identity->serviceCatalog();
99 
100  $c = count($catalog);
101  for ($i = 0; $i < $c; ++$i) {
102  if ($catalog[$i]['type'] == self::SERVICE_TYPE) {
103  foreach ($catalog[$i]['endpoints'] as $endpoint) {
104  if (isset($endpoint['publicURL']) && $endpoint['region'] == $region) {
105  $dbaas = new DBaaS($identity->token(), $endpoint['publicURL'], $identity->tenantName());
106 
107  return $dbaas;
108  }
109  }
110  }
111  }
112  }
113 
114  /**
115  * Build a new DBaaS object.
116  *
117  * @param string $token
118  * The auth token from identity services.
119  * @param string $endpoint
120  * The endpoint URL, typically from IdentityServices.
121  * @param string $projectId
122  * The project ID. Typically, this is the tenant name.
123  */
124  public function __construct($token, $endpoint, $projectId) {
125  $this->token = $token;
126  $this->url= $endpoint;
127  $this->projectId = $projectId;
128  }
129 
130 
131  public function instance() {
132  return new Instance($this->token, $this->projectId, $this->url);
133  }
134 
135  public function snapshot() {
136  return new Snapshot($this->token, $this->projectId, $this->url);
137  }
138 
139  public function flavor() {
140  return new Flavor($this->token, $this->projectId, $this->url);
141  }
142 
143  /**
144  * Get the project ID for this session.
145  *
146  * @retval string
147  * @return string
148  * The project ID.
149  */
150  public function projectId() {
151  return $this->projectId;
152  }
153 
154  /**
155  * Get the endpoint URL to the DBaaS session.
156  *
157  * @retval string
158  * @return string
159  * The URL.
160  */
161  public function url() {
162  return $this->url;
163  }
164 }