Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Identity
      • Log
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Resource
    • LoadBalancer
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • Database
  • Instance
  • User
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
  7:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  8:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
  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:  * 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->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:      * Returns TRUE if the root user is enabled
123:      *
124:      * @api
125:      * @return boolean TRUE if the root user is enabled; FALSE otherwise
126:      * @throws InstanceError if HTTP status is not Success
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:      * Returns a new Database object
137:      *
138:      * @param string $name the database name
139:      * @return Database
140:      */
141:     public function database($name = '') 
142:     {
143:         return new Database($this, $name);
144:     }
145: 
146:     /**
147:      * Returns a new User object
148:      *
149:      * @param string $name the user name
150:      * @param array $databases a simple array of database names
151:      * @return User
152:      */
153:     public function user($name = '', $databases = array()) 
154:     {
155:         return new User($this, $name, $databases);
156:     }
157: 
158:     /**
159:      * Returns a Collection of all databases in the instance
160:      *
161:      * @return OpenCloud\Common\Collection\PaginatedIterator
162:      */
163:     public function databaseList() 
164:     {
165:         return $this->getService()->resourceList('Database', $this->getUrl('databases'), $this);
166:     }
167: 
168:     /**
169:      * Returns a Collection of all users in the instance
170:      *
171:      * @return OpenCloud\Common\Collection\PaginatedIterator
172:      */
173:     public function userList() 
174:     {
175:         return $this->getService()->resourceList('User', $this->getUrl('users'), $this);
176:     }
177: 
178:     /**
179:      * Generates the JSON string for Create()
180:      *
181:      * @return \stdClass
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:      * Generates the JSON object for Restart
208:      */
209:     private function restartJson() 
210:     {
211:         return (object) array('restart' => new \stdClass);
212:     }
213: 
214:     /**
215:      * Generates the JSON object for Resize
216:      */
217:     private function resizeJson($flavorRef) 
218:     {
219:         return (object) array(
220:             'resize' => (object) array('flavorRef' => $flavorRef)
221:         );
222:     }
223: 
224:     /**
225:      * Generates the JSON object for ResizeVolume
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: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0