1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
<?php
/**
* Copyright 2012-2014 Rackspace US, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace OpenCloud\Database\Resource;
use OpenCloud\Common\Http\Message\Formatter;
use OpenCloud\Common\Lang;
use OpenCloud\Common\Exceptions;
use OpenCloud\Common\Resource\NovaResource;
use OpenCloud\Compute\Resource\Flavor;
use OpenCloud\Database\Service;
/**
* Instance represents an instance of DbService, similar to a Server in a
* Compute service
*/
class Instance extends NovaResource
{
public $id;
public $name;
public $status;
public $links;
public $hostname;
public $volume;
public $created;
public $updated;
public $flavor;
public $backupRef;
protected static $json_name = 'instance';
protected static $url_resource = 'instances';
private $_databases; // used to Create databases simultaneously
private $_users; // used to Create users simultaneously
/**
* Creates a new instance object
*
* This could use the default constructor, but we want to make sure that
* the volume attribute is an object.
*
* @param \OpenCloud\DbService $service the DbService object associated
* with this
* @param mixed $info the ID or array of info for the object
*/
public function __construct(Service $service, $info = null)
{
$this->volume = new \stdClass;
return parent::__construct($service, $info);
}
/**
* Restarts the database instance
*
* @api
* @returns \OpenCloud\HttpResponse
*/
public function restart()
{
return $this->action($this->restartJson());
}
/**
* Resizes the database instance (sets RAM)
*
* @api
* @param \OpenCloud\Compute\Flavor $flavor a flavor object
* @returns \OpenCloud\HttpResponse
*/
public function resize(Flavor $flavor)
{
return $this->action($this->resizeJson($flavor->id));
}
/**
* Resizes the volume associated with the database instance (disk space)
*
* @api
* @param integer $newvolumesize the size of the new volume, in gigabytes
* @return \OpenCloud\HttpResponse
*/
public function resizeVolume($newvolumesize)
{
return $this->action($this->resizeVolumeJson($newvolumesize));
}
/**
* Enables the root user for the instance
*
* @api
* @return User the root user, including name and password
* @throws InstanceError if HTTP response is not Success
*/
public function enableRootUser()
{
$response = $this->getClient()->post($this->getUrl('root'))->send();
$body = Formatter::decode($response);
return (isset($body->user)) ? new User($this, $body->user) : false;
}
/**
* Returns TRUE if the root user is enabled
*
* @api
* @return boolean TRUE if the root user is enabled; FALSE otherwise
* @throws InstanceError if HTTP status is not Success
*/
public function isRootEnabled()
{
$response = $this->getClient()->get($this->url('root'))->send();
$body = Formatter::decode($response);
return !empty($body->rootEnabled);
}
/**
* Returns a new Database object
*
* @param string $name the database name
* @return Database
*/
public function database($name = '')
{
return new Database($this, $name);
}
/**
* Returns a new User object
*
* @param string $name the user name
* @param array $databases a simple array of database names
* @return User
*/
public function user($name = '', $databases = array())
{
return new User($this, $name, $databases);
}
/**
* Returns a Collection of all databases in the instance
*
* @return OpenCloud\Common\Collection\PaginatedIterator
*/
public function databaseList()
{
return $this->getService()->resourceList('Database', $this->getUrl('databases'), $this);
}
/**
* Returns a Collection of all users in the instance
*
* @return OpenCloud\Common\Collection\PaginatedIterator
*/
public function userList()
{
return $this->getService()->resourceList('User', $this->getUrl('users'), $this);
}
/**
* Returns a Collection of all backups for the instance
*
* @return OpenCloud\Common\Collection\PaginatedIterator
*/
public function backupList()
{
return $this->getService()->resourceList('Backup', $this->getUrl('backups'), $this);
}
/**
* Creates a backup for the given instance
*
* @api
* @param array $params - an associate array of key/value pairs
* name is required
* description is optional
* @return Backup
*/
public function createBackup($params = array())
{
if (!isset($params['instanceId'])) {
$params['instanceId'] = $this->id;
}
$backup = new Backup($this->getService(), $params);
$backup->create($params);
return $backup;
}
public function populate($info, $setObjects = true)
{
parent::populate($info, $setObjects);
if (is_object($info)) {
$info = (array) $info;
}
if (isset($info['restorePoint']['backupRef'])) {
$this->backupRef = $info['restorePoint']['backupRef'];
}
}
/**
* Generates the JSON string for Create()
*
* @return \stdClass
*/
protected function createJson()
{
if (empty($this->flavor) || !is_object($this->flavor)) {
throw new Exceptions\InstanceFlavorError(
Lang::translate('The `flavor` attribute is required and must be a Flavor object')
);
}
if (!isset($this->name)) {
throw new Exceptions\InstanceError(
Lang::translate('Instance name is required')
);
}
$out = [
'instance' => [
'flavorRef' => $this->flavor->links[0]->href,
'name' => $this->name,
'volume' => $this->volume
]
];
if (isset($this->backupRef)) {
$out['instance']['restorePoint'] = [
'backupRef' => $this->backupRef
];
}
return (object) $out;
}
/**
* Generates the JSON string for update()
*
* @return \@stdClass
*/
protected function updateJson($params = array())
{
return (object) array(
self::$json_name => $params
);
}
/**
* Generates the JSON object for Restart
*/
private function restartJson()
{
return (object) array('restart' => new \stdClass);
}
/**
* Generates the JSON object for Resize
*/
private function resizeJson($flavorRef)
{
return (object) array(
'resize' => (object) array('flavorRef' => $flavorRef)
);
}
/**
* Generates the JSON object for ResizeVolume
*/
private function resizeVolumeJson($size)
{
return (object) array(
'resize' => (object) array(
'volume' => (object) array('size' => $size)
)
);
}
}