      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\Database\Resource;
     19:
     20: use OpenCloud\Common\Exceptions;
     21: use OpenCloud\Common\Lang;
     22: use OpenCloud\Common\Resource\PersistentResource;
     23:
     24: /**
     25:  * This class represents a Database in the Rackspace "Red Dwarf"
     26:  * database-as-a-service product.
     27:  */
     28: class Database extends PersistentResource
     29: {
     30:     /** @var string */
     31:     public $name;
     32:
     33:     protected static $json_collection_name = 'databases';
     34:     protected static $url_resource = 'databases';
     35:
     36:     public function __construct(Instance $instance, $info = null)
     37:     {
     38:         $this->setParent($instance);
     39:
     40:         // Catering for laziness
     41:         if (is_string($info)) {
     42:             $info = array('name' => $info);
     43:         }
     44:
     45:         return parent::__construct($instance->getService(), $info);
     46:     }
     47:
     48:     /**
     49:      * Returns name of this database. Because it's so important (i.e. as an
     50:      * identifier), it will throw an error if not set/empty.
     51:      *
     52:      * @return type
     53:      * @throws Exceptions\DatabaseNameError
     54:      */
     55:     public function getName()
     56:     {
     57:         if (empty($this->name)) {
     58:             throw new Exceptions\DatabaseNameError(
     59:                 Lang::translate('The database does not have a Url yet')
     60:             );
     61:         }
     62:
     63:         return $this->name;
     64:     }
     65:
     66:     public function primaryKeyField()
     67:     {
     68:         return 'name';
     69:     }
     70:
     71:     /**
     72:      * Returns the Instance of the database
     73:      *
     74:      * @return Instance
     75:      */
     76:     public function instance()
     77:     {
     78:         return $this->getParent();
     79:     }
     80:
     81:     /**
     82:      * Creates a new database
     83:      *
     84:      * @api
     85:      * @param array $params array of attributes to set prior to Create
     86:      * @return \OpenCloud\HttpResponse
     87:      */
     88:     public function create($params = array())
     89:     {
     90:         // target the /databases subresource
     91:         $url = $this->getParent()->url('databases');
     92:
     93:         if (isset($params['name'])) {
     94:             $this->name = $params['name'];
     95:         }
     96:
     97:         $json = json_encode($this->createJson($params));
     98:         $this->checkJsonError();
     99:
    100:         // POST it off
    101:         return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
    102:     }
    103:
    104:     /**
    105:      * Updates an existing database
    106:      *
    107:      * @param array $params ignored
    108:      * @throws DatabaseUpdateError always; updates are not permitted
    109:      * @return void
    110:      */
    111:     public function update($params = array())
    112:     {
    113:         return $this->noUpdate();
    114:     }
    115:
    116:     /**
    117:      * Returns the JSON object for creating the database
    118:      */
    119:     protected function createJson(array $params = array())
    120:     {
    121:         $database = (object) array_merge(array('name' => $this->getName(), $params));
    122:
    123:         return (object) array(
    124:             'databases' => array($database)
    125:         );
    126:     }
    127: }
    128: