1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace OpenCloud\Database;
13:
14: use OpenCloud\Common\Collection;
15: use OpenCloud\Common\PersistentObject;
16: use OpenCloud\Common\Lang;
17: use OpenCloud\Common\Exceptions;
18: use OpenCloud\Compute\Flavor;
19:
20: 21: 22: 23:
24: class Instance extends PersistentObject
25: {
26:
27: public $id;
28: public $name;
29: public $status;
30: public $links;
31: public $hostname;
32: public $volume;
33: public $created;
34: public $updated;
35: public $flavor;
36:
37: protected static $json_name = 'instance';
38: protected static $url_resource = 'instances';
39:
40: private $_databases;
41: private $_users;
42:
43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: public function __construct(Service $service, $info = null)
54: {
55: $this->volume = new \stdClass;
56: return parent::__construct($service, $info);
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66:
67: public function update($params = array())
68: {
69: return $this->noUpdate();
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function restart()
79: {
80: return $this->action($this->restartJson());
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function resize(Flavor $flavor)
91: {
92: return $this->action($this->resizeJson($flavor));
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: public function resizeVolume($newvolumesize)
103: {
104: return $this->action($this->resizeVolumeJson($newvolumesize));
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function enableRootUser()
115: {
116: $response = $this->getService()->request($this->url('root'), 'POST');
117:
118:
119: if ($response->HttpStatus() > 202) {
120: throw new Exceptions\InstanceError(sprintf(
121: Lang::translate('Error enabling root user for instance [%s], status [%d] response [%s]'),
122: $this->name,
123: $response->HttpStatus(),
124: $response->HttpBody()
125: ));
126: }
127:
128:
129: $object = json_decode($response->HttpBody());
130:
131: $this->checkJsonError();
132:
133: return (!empty($object->user)) ? new User($this, $object->user) : false;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function isRootEnabled()
144: {
145: $response = $this->getService()->Request($this->Url('root'), 'GET');
146:
147:
148: if ($response->HttpStatus() > 202) {
149: throw new Exceptions\InstanceError(sprintf(
150: Lang::translate('Error enabling root user for instance [%s], status [%d] response [%s]'),
151: $this->name,
152: $response->HttpStatus(),
153: $response->HttpBody()
154: ));
155: }
156:
157:
158: $object = json_decode($response->httpBody());
159:
160: $this->checkJsonError();
161:
162: return !empty($object->rootEnabled);
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function database($name = '')
172: {
173: return new Database($this, $name);
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public function user($name = '', $databases = array())
184: {
185: return new User($this, $name, $databases);
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function databaseList()
195: {
196: $response = $this->getService()->request($this->Url('databases'));
197:
198:
199: if ($response->HttpStatus() > 200) {
200: throw new Exceptions\DatabaseListError(sprintf(
201: Lang::translate('Error listing databases for instance [%s], status [%d] response [%s]'),
202: $this->name,
203: $response->HttpStatus(),
204: $response->HttpBody()
205: ));
206: }
207:
208:
209: $object = json_decode($response->httpBody());
210:
211: $this->checkJsonError();
212:
213: $data = (!empty($object->databases)) ? $object->databases : array();
214: return new Collection($this, 'OpenCloud\DbService\Database', $data);
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function userList()
224: {
225: $response = $this->getService()->Request($this->Url('users'));
226:
227:
228: if ($response->HttpStatus() > 200) {
229: throw new Exceptions\UserListError(sprintf(
230: Lang::translate('Error listing users for instance [%s], status [%d] response [%s]'),
231: $this->name,
232: $response->HttpStatus(),
233: $response->HttpBody()
234: ));
235: }
236:
237:
238: $object = json_decode($response->HttpBody());
239:
240: $this->checkJsonError();
241:
242: $data = (!empty($object->users)) ? $object->users : array();
243: return new Collection($this, 'OpenCloud\DbService\User', $data);
244: }
245:
246: 247: 248: 249: 250:
251: protected function createJson()
252: {
253: if (empty($this->flavor) || !is_object($this->flavor)) {
254: throw new Exceptions\InstanceFlavorError(
255: Lang::translate('The `flavor` attribute is required and must be a Flavor object')
256: );
257: }
258:
259: if (!isset($this->name)) {
260: throw new Exceptions\InstanceError(
261: Lang::translate('Instance name is required')
262: );
263: }
264:
265: return (object) array(
266: 'instance' => (object) array(
267: 'flavorRef' => $this->flavor->links[0]->href,
268: 'name' => $this->name,
269: 'volume' => $this->volume
270: )
271: );
272: }
273:
274: 275: 276:
277: private function restartJson()
278: {
279: return (object) array('restart' => new \stdClass);
280: }
281:
282: 283: 284:
285: private function resizeJson($flavorRef)
286: {
287: return (object) array(
288: 'resize' => (object) array('flavorRef' => $flavorRef)
289: );
290: }
291:
292: 293: 294:
295: private function resizeVolumeJson($size)
296: {
297: return (object) array(
298: 'resize' => (object) array(
299: 'volume' => (object) array('size' => $size)
300: )
301: );
302: }
303:
304: }
305: