Overview

Namespaces

  • None
  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Exceptions
      • Log
      • Request
        • Response
    • Compute
    • Database
    • DNS
    • LoadBalancer
      • Resources
    • ObjectStore
      • Resource
    • Orchestration
    • Volume
  • PHP

Classes

  • Database
  • Instance
  • Service
  • User
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
  7:  * @version   1.6.0
  8:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  9:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 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:  * Instance represents an instance of DbService, similar to a Server in a
 22:  * Compute service
 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;    // used to Create databases simultaneously
 41:     private $_users;        // used to Create users simultaneously
 42: 
 43:     /**
 44:      * Creates a new instance object
 45:      *
 46:      * This could use the default constructor, but we want to make sure that
 47:      * the volume attribute is an object.
 48:      *
 49:      * @param \OpenCloud\DbService $service the DbService object associated 
 50:      *      with this
 51:      * @param mixed $info the ID or array of info for the object
 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:      * Updates a database instance (not permitted)
 61:      *
 62:      * Update() is not supported by database instances; thus, this always
 63:      * throws an exception.
 64:      *
 65:      * @throws InstanceUpdateError always
 66:      */
 67:     public function update($params = array()) 
 68:     {
 69:         return $this->noUpdate();
 70:     }
 71: 
 72:     /**
 73:      * Restarts the database instance
 74:      *
 75:      * @api
 76:      * @returns \OpenCloud\HttpResponse
 77:      */
 78:     public function restart() 
 79:     {
 80:         return $this->action($this->restartJson());
 81:     }
 82: 
 83:     /**
 84:      * Resizes the database instance (sets RAM)
 85:      *
 86:      * @api
 87:      * @param \OpenCloud\Compute\Flavor $flavor a flavor object
 88:      * @returns \OpenCloud\HttpResponse
 89:      */
 90:     public function resize(Flavor $flavor) 
 91:     {
 92:         return $this->action($this->resizeJson($flavor));
 93:     }
 94: 
 95:     /**
 96:      * Resizes the volume associated with the database instance (disk space)
 97:      *
 98:      * @api
 99:      * @param integer $newvolumesize the size of the new volume, in gigabytes
100:      * @return \OpenCloud\HttpResponse
101:      */
102:     public function resizeVolume($newvolumesize) 
103:     {
104:         return $this->action($this->resizeVolumeJson($newvolumesize));
105:     }
106: 
107:     /**
108:      * Enables the root user for the instance
109:      *
110:      * @api
111:      * @return User the root user, including name and password
112:      * @throws InstanceError if HTTP response is not Success
113:      */
114:     public function enableRootUser() 
115:     {
116:         $response = $this->getService()->request($this->url('root'), 'POST');
117: 
118:         // @codeCoverageIgnoreStart
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:         // @codeCoverageIgnoreEnd
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:      * Returns TRUE if the root user is enabled
138:      *
139:      * @api
140:      * @return boolean TRUE if the root user is enabled; FALSE otherwise
141:      * @throws InstanceError if HTTP status is not Success
142:      */
143:     public function isRootEnabled() 
144:     {
145:         $response = $this->getService()->Request($this->Url('root'), 'GET');
146: 
147:         // @codeCoverageIgnoreStart
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:         // @codeCoverageIgnoreEnd
157:         
158:         $object = json_decode($response->httpBody());
159: 
160:         $this->checkJsonError();
161: 
162:         return !empty($object->rootEnabled);
163:     }
164: 
165:     /**
166:      * Returns a new Database object
167:      *
168:      * @param string $name the database name
169:      * @return Database
170:      */
171:     public function database($name = '') 
172:     {
173:         return new Database($this, $name);
174:     }
175: 
176:     /**
177:      * Returns a new User object
178:      *
179:      * @param string $name the user name
180:      * @param array $databases a simple array of database names
181:      * @return User
182:      */
183:     public function user($name = '', $databases = array()) 
184:     {
185:         return new User($this, $name, $databases);
186:     }
187: 
188:     /**
189:      * Returns a Collection of all databases in the instance
190:      *
191:      * @return Collection
192:      * @throws DatabaseListError if HTTP status is not Success
193:      */
194:     public function databaseList() 
195:     {
196:         $response = $this->getService()->request($this->Url('databases'));
197: 
198:         // @codeCoverageIgnoreStart
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:         // @codeCoverageIgnoreEnd
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:      * Returns a Collection of all users in the instance
219:      *
220:      * @return Collection
221:      * @throws UserListError if HTTP status is not Success
222:      */
223:     public function userList() 
224:     {
225:         $response = $this->getService()->Request($this->Url('users'));
226: 
227:         // @codeCoverageIgnoreStart
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:         // @codeCoverageIgnoreEnd
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:      * Generates the JSON string for Create()
248:      *
249:      * @return \stdClass
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:      * Generates the JSON object for Restart
276:      */
277:     private function restartJson() 
278:     {
279:         return (object) array('restart' => new \stdClass);
280:     }
281: 
282:     /**
283:      * Generates the JSON object for Resize
284:      */
285:     private function resizeJson($flavorRef) 
286:     {
287:         return (object) array(
288:             'resize' => (object) array('flavorRef' => $flavorRef)
289:         );
290:     }
291: 
292:     /**
293:      * Generates the JSON object for ResizeVolume
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: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0