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