1: <?php
2: /**
3: * Copyright 2012-2014 Rackspace US, Inc.
4: *
5: * Licensed under the Apache License, Version 2.0 (the "License");
6: * you may not use this file except in compliance with the License.
7: * You may obtain a copy of the License at
8: *
9: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: namespace OpenCloud\Database\Resource;
19:
20: use OpenCloud\Common\Http\Message\Formatter;
21: use OpenCloud\Common\Lang;
22: use OpenCloud\Common\Exceptions;
23: use OpenCloud\Common\Resource\NovaResource;
24: use OpenCloud\Compute\Resource\Flavor;
25: use OpenCloud\Database\Service;
26:
27: /**
28: * Instance represents an instance of DbService, similar to a Server in a
29: * Compute service
30: */
31: class Instance extends NovaResource
32: {
33: public $id;
34: public $name;
35: public $status;
36: public $links;
37: public $hostname;
38: public $volume;
39: public $created;
40: public $updated;
41: public $flavor;
42:
43: protected static $json_name = 'instance';
44: protected static $url_resource = 'instances';
45:
46: private $_databases; // used to Create databases simultaneously
47: private $_users; // used to Create users simultaneously
48:
49: /**
50: * Creates a new instance object
51: *
52: * This could use the default constructor, but we want to make sure that
53: * the volume attribute is an object.
54: *
55: * @param \OpenCloud\DbService $service the DbService object associated
56: * with this
57: * @param mixed $info the ID or array of info for the object
58: */
59: public function __construct(Service $service, $info = null)
60: {
61: $this->volume = new \stdClass;
62:
63: return parent::__construct($service, $info);
64: }
65:
66: /**
67: * Restarts the database instance
68: *
69: * @api
70: * @returns \OpenCloud\HttpResponse
71: */
72: public function restart()
73: {
74: return $this->action($this->restartJson());
75: }
76:
77: /**
78: * Resizes the database instance (sets RAM)
79: *
80: * @api
81: * @param \OpenCloud\Compute\Flavor $flavor a flavor object
82: * @returns \OpenCloud\HttpResponse
83: */
84: public function resize(Flavor $flavor)
85: {
86: return $this->action($this->resizeJson($flavor->id));
87: }
88:
89: /**
90: * Resizes the volume associated with the database instance (disk space)
91: *
92: * @api
93: * @param integer $newvolumesize the size of the new volume, in gigabytes
94: * @return \OpenCloud\HttpResponse
95: */
96: public function resizeVolume($newvolumesize)
97: {
98: return $this->action($this->resizeVolumeJson($newvolumesize));
99: }
100:
101: /**
102: * Enables the root user for the instance
103: *
104: * @api
105: * @return User the root user, including name and password
106: * @throws InstanceError if HTTP response is not Success
107: */
108: public function enableRootUser()
109: {
110: $response = $this->getClient()->post($this->getUrl('root'))->send();
111: $body = Formatter::decode($response);
112:
113: return (isset($body->user)) ? new User($this, $body->user) : false;
114: }
115:
116: /**
117: * Returns TRUE if the root user is enabled
118: *
119: * @api
120: * @return boolean TRUE if the root user is enabled; FALSE otherwise
121: * @throws InstanceError if HTTP status is not Success
122: */
123: public function isRootEnabled()
124: {
125: $response = $this->getClient()->get($this->url('root'))->send();
126: $body = Formatter::decode($response);
127:
128: return !empty($body->rootEnabled);
129: }
130:
131: /**
132: * Returns a new Database object
133: *
134: * @param string $name the database name
135: * @return Database
136: */
137: public function database($name = '')
138: {
139: return new Database($this, $name);
140: }
141:
142: /**
143: * Returns a new User object
144: *
145: * @param string $name the user name
146: * @param array $databases a simple array of database names
147: * @return User
148: */
149: public function user($name = '', $databases = array())
150: {
151: return new User($this, $name, $databases);
152: }
153:
154: /**
155: * Returns a Collection of all databases in the instance
156: *
157: * @return OpenCloud\Common\Collection\PaginatedIterator
158: */
159: public function databaseList()
160: {
161: return $this->getService()->resourceList('Database', $this->getUrl('databases'), $this);
162: }
163:
164: /**
165: * Returns a Collection of all users in the instance
166: *
167: * @return OpenCloud\Common\Collection\PaginatedIterator
168: */
169: public function userList()
170: {
171: return $this->getService()->resourceList('User', $this->getUrl('users'), $this);
172: }
173:
174: /**
175: * Generates the JSON string for Create()
176: *
177: * @return \stdClass
178: */
179: protected function createJson()
180: {
181: if (empty($this->flavor) || !is_object($this->flavor)) {
182: throw new Exceptions\InstanceFlavorError(
183: Lang::translate('The `flavor` attribute is required and must be a Flavor object')
184: );
185: }
186:
187: if (!isset($this->name)) {
188: throw new Exceptions\InstanceError(
189: Lang::translate('Instance name is required')
190: );
191: }
192:
193: return (object) array(
194: 'instance' => (object) array(
195: 'flavorRef' => $this->flavor->links[0]->href,
196: 'name' => $this->name,
197: 'volume' => $this->volume
198: )
199: );
200: }
201:
202: /**
203: * Generates the JSON string for update()
204: *
205: * @return \@stdClass
206: */
207: protected function updateJson($params = array())
208: {
209: return (object) array(
210: self::$json_name => $params
211: );
212: }
213:
214: /**
215: * Generates the JSON object for Restart
216: */
217: private function restartJson()
218: {
219: return (object) array('restart' => new \stdClass);
220: }
221:
222: /**
223: * Generates the JSON object for Resize
224: */
225: private function resizeJson($flavorRef)
226: {
227: return (object) array(
228: 'resize' => (object) array('flavorRef' => $flavorRef)
229: );
230: }
231:
232: /**
233: * Generates the JSON object for ResizeVolume
234: */
235: private function resizeVolumeJson($size)
236: {
237: return (object) array(
238: 'resize' => (object) array(
239: 'volume' => (object) array('size' => $size)
240: )
241: );
242: }
243: }
244: