1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace OpenCloud\Database\Resource;
12:
13: use OpenCloud\Common\PersistentObject;
14: use OpenCloud\Common\Lang;
15: use OpenCloud\Common\Exceptions;
16: use OpenCloud\Compute\Resource\Flavor;
17: use OpenCloud\Database\Service;
18: use OpenCloud\Common\Http\Message\Formatter;
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->getClient()->post($this->getUrl('root'))->send();
117: $body = Formatter::decode($response);
118: return (!empty($body->user)) ? new User($this, $body->user) : false;
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: public function isRootEnabled()
129: {
130: $response = $this->getClient()->get($this->url('root'))->send();
131: $body = Formatter::decode($response);
132: return !empty($body->rootEnabled);
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function database($name = '')
142: {
143: return new Database($this, $name);
144: }
145:
146: 147: 148: 149: 150: 151: 152:
153: public function user($name = '', $databases = array())
154: {
155: return new User($this, $name, $databases);
156: }
157:
158: 159: 160: 161: 162:
163: public function databaseList()
164: {
165: return $this->getService()->resourceList('Database', $this->getUrl('databases'), $this);
166: }
167:
168: 169: 170: 171: 172:
173: public function userList()
174: {
175: return $this->getService()->resourceList('User', $this->getUrl('users'), $this);
176: }
177:
178: 179: 180: 181: 182:
183: protected function createJson()
184: {
185: if (empty($this->flavor) || !is_object($this->flavor)) {
186: throw new Exceptions\InstanceFlavorError(
187: Lang::translate('The `flavor` attribute is required and must be a Flavor object')
188: );
189: }
190:
191: if (!isset($this->name)) {
192: throw new Exceptions\InstanceError(
193: Lang::translate('Instance name is required')
194: );
195: }
196:
197: return (object) array(
198: 'instance' => (object) array(
199: 'flavorRef' => $this->flavor->links[0]->href,
200: 'name' => $this->name,
201: 'volume' => $this->volume
202: )
203: );
204: }
205:
206: 207: 208:
209: private function restartJson()
210: {
211: return (object) array('restart' => new \stdClass);
212: }
213:
214: 215: 216:
217: private function resizeJson($flavorRef)
218: {
219: return (object) array(
220: 'resize' => (object) array('flavorRef' => $flavorRef)
221: );
222: }
223:
224: 225: 226:
227: private function resizeVolumeJson($size)
228: {
229: return (object) array(
230: 'resize' => (object) array(
231: 'volume' => (object) array('size' => $size)
232: )
233: );
234: }
235:
236: }
237: