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\Exceptions;
 15: use OpenCloud\Common\Lang;
 16: 
 17: /**
 18:  * This class represents a Database in the Rackspace "Red Dwarf"
 19:  * database-as-a-service product.
 20:  */
 21: class Database extends PersistentObject
 22: {
 23:     protected static $json_collection_name = 'databases';
 24:     protected static $url_resource = 'databases';
 25:     
 26:     public $name;
 27: 
 28:     /**
 29:      * Creates a new database object
 30:      *
 31:      * Unlike other objects (Servers, DataObjects, etc.), passing a database
 32:      * name to the constructor does *not* pull information from the database.
 33:      * For example, if you pass an ID to the `Server()` constructor, it will
 34:      * attempt to retrieve the information on that server from the service,
 35:      * and will return an error if it is not found. However, the Cloud
 36:      * Databases service does not permit retrieval of information on
 37:      * individual databases (only via Collection), and thus passing in a
 38:      * name via the `$info` parameter only creates an in-memory object that
 39:      * is not necessarily tied to an actual database.
 40:      *
 41:      * @param Instance $instance the parent DbService\Instance of the database
 42:      * @param mixed $info if an array or object, treated as properties to set;
 43:      *      if a string, treated as the database name
 44:      * @return void
 45:      * @throws DatabaseNameError if `$info` is not a string, object, or array
 46:      */
 47:     public function __construct(Instance $instance, $info = null)
 48:     {
 49:         $this->setParent($instance);
 50:         // Catering for laziness
 51:         if (is_string($info)) {
 52:             $info = array('name' => $info);
 53:         }
 54:         return parent::__construct($instance->getService(), $info);
 55:     }
 56:     
 57:     /**
 58:      * Returns name of this database. Because it's so important (i.e. as an
 59:      * identifier), it will throw an error if not set/empty.
 60:      * 
 61:      * @return type
 62:      * @throws Exceptions\DatabaseNameError
 63:      */
 64:     public function getName()
 65:     {
 66:         if (empty($this->name)) {
 67:             throw new Exceptions\DatabaseNameError(
 68:                 Lang::translate('The database does not have a Url yet')
 69:             );
 70:         }
 71:         return $this->name;
 72:     }
 73: 
 74:     public function primaryKeyField()
 75:     {
 76:         return 'name';
 77:     }
 78:     
 79:     /**
 80:      * Returns the Instance of the database
 81:      *
 82:      * @return Instance
 83:      */
 84:     public function instance()
 85:     {
 86:         return $this->getParent();
 87:     }
 88: 
 89:     /**
 90:      * Creates a new database
 91:      *
 92:      * @api
 93:      * @param array $params array of attributes to set prior to Create
 94:      * @return \OpenCloud\HttpResponse
 95:      */
 96:     public function create($params = array())
 97:     {
 98:         // target the /databases subresource
 99:         $url = $this->getParent()->url('databases');
100: 
101:         if (isset($params['name'])) {
102:             $this->name = $params['name'];
103:         }
104: 
105:         $json = json_encode($this->createJson($params));
106:         $this->checkJsonError();
107: 
108:         // POST it off
109:         return $this->getClient()->post($url, array(), $json)->send();
110:     }
111: 
112:     /**
113:      * Updates an existing database
114:      *
115:      * @param array $params ignored
116:      * @throws DatabaseUpdateError always; updates are not permitted
117:      * @return void
118:      */
119:     public function update($params = array())
120:     {
121:         return $this->noUpdate();
122:     }
123: 
124:     /**
125:      * Deletes a database
126:      *
127:      * @api
128:      * @return \OpenCloud\HttpResponseb
129:      */
130:     public function delete()
131:     {
132:         return $this->getClient()->delete($this->url())->send();
133:     }
134: 
135:     /**
136:      * Returns the JSON object for creating the database
137:      */
138:     protected function createJson(array $params = array())
139:     {
140:         $database = (object) array_merge(array('name' => $this->getName(), $params));
141: 
142:         return (object) array(
143:             'databases' => array($database)
144:         );
145:     }
146: 
147: }
148: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0