Skip to content

Commit 46745a4

Browse files
author
Jamie Hannaford
committed
Merge pull request rackspace#620 from rackspace/working
Merge working into master before new release
2 parents 5532b88 + 6737f9e commit 46745a4

File tree

28 files changed

+556
-45
lines changed

28 files changed

+556
-45
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"require-dev" : {
3535
"phpunit/phpunit": "4.3.*",
36+
"phpspec/prophecy": "~1.4",
3637
"satooshi/php-coveralls": "0.6.*@dev",
3738
"jakub-onderka/php-parallel-lint": "0.*",
3839
"fabpot/php-cs-fixer": "1.0.*@dev",

doc/services/database/users.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ Check if root user is enabled
6565
6666
// true for yes, false for no
6767
$instance->isRootEnabled();
68+
69+
70+
Grant database access
71+
---------------------
72+
73+
To grant access to one or more databases, you can run:
74+
75+
.. code-block:: php
76+
77+
$user = $instance->user('{userName}');
78+
$user->grantDbAccess(['{dbName1}', '{dbName2}']);
79+

doc/services/networking/ports.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ You can list all the ports to which you have access as shown in the following ex
8686
8787
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Networking/list-ports.php>`_
8888

89+
The port list query may be filtered by numerous optional parameters as per the `API documentation <http://developer.openstack.org/api-ref-networking-v2.html#listPorts>`_
90+
91+
.. code-block:: php
92+
93+
$ports = $networkingService->listPorts([
94+
'status' => 'ACTIVE',
95+
'device_id' => '9ae135f4-b6e0-4dad-9e91-3c223e385824'
96+
]);
97+
98+
foreach ($ports as $port) {
99+
/** @var $port OpenCloud\Networking\Resource\Port **/
100+
}
101+
102+
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Networking/list-ports-filtered.php>`_
103+
89104

90105
Get a port
91106
----------
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Common\Exceptions;
19+
20+
class BackupInstanceError extends \Exception
21+
{
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Common\Exceptions;
19+
20+
class BackupNameError extends \Exception
21+
{
22+
}

lib/OpenCloud/Common/Http/Client.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace OpenCloud\Common\Http;
1919

2020
use Guzzle\Http\Client as GuzzleClient;
21-
use Guzzle\Http\Curl\CurlVersion;
21+
use OpenCloud\Version;
2222
use OpenCloud\Common\Exceptions\UnsupportedVersionError;
2323

2424
/**
@@ -27,7 +27,6 @@
2727
*/
2828
class Client extends GuzzleClient
2929
{
30-
const VERSION = '1.9.0';
3130
const MINIMUM_PHP_VERSION = '5.3.0';
3231

3332
public function __construct($baseUrl = '', $config = null)
@@ -46,9 +45,10 @@ public function __construct($baseUrl = '', $config = null)
4645

4746
public function getDefaultUserAgent()
4847
{
49-
return 'OpenCloud/' . self::VERSION
50-
. ' cURL/' . CurlVersion::getInstance()->get('version')
51-
. ' PHP/' . PHP_VERSION;
48+
return 'OpenCloud/' . Version::getVersion()
49+
. ' Guzzle/' . Version::getGuzzleVersion()
50+
. ' cURL/' . Version::getCurlVersion()
51+
. ' PHP/' . PHP_VERSION;
5252
}
5353

5454
public function getUserAgent()

lib/OpenCloud/Common/Service/CatalogItem.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,18 @@ public function getEndpoints()
134134
/**
135135
* Using a standard data object, extract its endpoint.
136136
*
137-
* @param $region
137+
* @param string $region
138+
* @param bool $isRegionless
139+
*
138140
* @return mixed
139141
* @throws \OpenCloud\Common\Exceptions\EndpointError
140142
*/
141-
public function getEndpointFromRegion($region)
143+
public function getEndpointFromRegion($region, $isRegionless = false)
142144
{
143145
foreach ($this->endpoints as $endpoint) {
144146
// Return the endpoint if it is regionless OR if the endpoint's
145147
// region matches the $region supplied by the caller.
146-
if (!isset($endpoint->region) || $endpoint->region == $region) {
148+
if ($isRegionless || !isset($endpoint->region) || $endpoint->region == $region) {
147149
return $endpoint;
148150
}
149151
}

lib/OpenCloud/Common/Service/CatalogService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ private function findEndpoint()
215215
// Search each service to find The One
216216
foreach ($catalog->getItems() as $service) {
217217
if ($service->hasType($this->type) && $service->hasName($this->name)) {
218-
return Endpoint::factory($service->getEndpointFromRegion($this->region), static::SUPPORTED_VERSION, $this->getClient());
218+
$endpoint = $service->getEndpointFromRegion($this->region, $this->regionless);
219+
return Endpoint::factory($endpoint, static::SUPPORTED_VERSION, $this->getClient());
219220
}
220221
}
221222

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\Lang;
21+
use OpenCloud\Common\Exceptions;
22+
use OpenCloud\Common\Resource\PersistentResource;
23+
use OpenCloud\Database\Service;
24+
25+
/**
26+
* This class represents a Backup
27+
*/
28+
class Backup extends PersistentResource
29+
{
30+
public $id;
31+
public $name;
32+
public $description;
33+
public $created;
34+
public $datastore;
35+
public $updated;
36+
public $instance;
37+
public $instanceId;
38+
public $locationRef;
39+
40+
protected static $json_name = 'backup';
41+
protected static $url_resource = 'backups';
42+
43+
protected $createKeys = array(
44+
'name',
45+
'instanceId',
46+
'description'
47+
);
48+
49+
protected $associatedResources = array(
50+
'instance' => 'Instance'
51+
);
52+
53+
protected $aliases = array(
54+
'instance_id' => 'instanceId'
55+
);
56+
57+
public function __construct(Service $service, $info = null)
58+
{
59+
$this->instance = new \stdClass;
60+
return parent::__construct($service, $info);
61+
}
62+
63+
/**
64+
* Returns the JSON object for creating the backup
65+
*/
66+
protected function createJson()
67+
{
68+
if (!isset($this->instanceId)) {
69+
throw new Exceptions\BackupInstanceError(
70+
Lang::translate('The `instanceId` attribute is required and must be a string')
71+
);
72+
}
73+
74+
if (!isset($this->name)) {
75+
throw new Exceptions\BackupNameError(
76+
Lang::translate('Backup name is required')
77+
);
78+
}
79+
80+
$out = [
81+
'backup' => [
82+
'name' => $this->name,
83+
'instance' => $this->instanceId
84+
]
85+
];
86+
87+
if (isset($this->description)) {
88+
$out['backup']['description'] = $this->description;
89+
}
90+
return (object) $out;
91+
}
92+
}

lib/OpenCloud/Database/Resource/Instance.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Instance extends NovaResource
3939
public $created;
4040
public $updated;
4141
public $flavor;
42+
public $backupRef;
4243

4344
protected static $json_name = 'instance';
4445
protected static $url_resource = 'instances';
@@ -171,6 +172,49 @@ public function userList()
171172
return $this->getService()->resourceList('User', $this->getUrl('users'), $this);
172173
}
173174

175+
/**
176+
* Returns a Collection of all backups for the instance
177+
*
178+
* @return OpenCloud\Common\Collection\PaginatedIterator
179+
*/
180+
public function backupList()
181+
{
182+
return $this->getService()->resourceList('Backup', $this->getUrl('backups'), $this);
183+
}
184+
185+
/**
186+
* Creates a backup for the given instance
187+
*
188+
* @api
189+
* @param array $params - an associate array of key/value pairs
190+
* name is required
191+
* description is optional
192+
* @return Backup
193+
*/
194+
public function createBackup($params = array())
195+
{
196+
if (!isset($params['instanceId'])) {
197+
$params['instanceId'] = $this->id;
198+
}
199+
200+
$backup = new Backup($this->getService(), $params);
201+
$backup->create($params);
202+
return $backup;
203+
}
204+
205+
public function populate($info, $setObjects = true)
206+
{
207+
parent::populate($info, $setObjects);
208+
209+
if (is_object($info)) {
210+
$info = (array) $info;
211+
}
212+
213+
if (isset($info['restorePoint']['backupRef'])) {
214+
$this->backupRef = $info['restorePoint']['backupRef'];
215+
}
216+
}
217+
174218
/**
175219
* Generates the JSON string for Create()
176220
*
@@ -190,13 +234,21 @@ protected function createJson()
190234
);
191235
}
192236

193-
return (object) array(
194-
'instance' => (object) array(
237+
$out = [
238+
'instance' => [
195239
'flavorRef' => $this->flavor->links[0]->href,
196240
'name' => $this->name,
197241
'volume' => $this->volume
198-
)
199-
);
242+
]
243+
];
244+
245+
if (isset($this->backupRef)) {
246+
$out['instance']['restorePoint'] = [
247+
'backupRef' => $this->backupRef
248+
];
249+
}
250+
251+
return (object) $out;
200252
}
201253

202254
/**

lib/OpenCloud/Database/Resource/User.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,29 @@ protected function createJson()
152152
'users' => array($user)
153153
);
154154
}
155+
156+
/**
157+
* Grant access to a set of one or more databases to a user.
158+
*
159+
* @param []string $databases An array of one or more database names that this user will be granted access to. For
160+
* example, ['foo', 'bar'] or ['baz'] are valid inputs.
161+
*
162+
* @return \Guzzle\Http\Message\Response
163+
*/
164+
public function grantDbAccess(array $databases)
165+
{
166+
$json = [];
167+
168+
foreach ($databases as $database) {
169+
$json[] = ['name' => $database];
170+
}
171+
172+
$json = ['databases' => $json];
173+
174+
$url = $this->getUrl('databases');
175+
$headers = self::getJsonHeader();
176+
$body = json_encode($json);
177+
178+
return $this->getClient()->put($url, $headers, $body)->send();
179+
}
155180
}

lib/OpenCloud/Database/Service.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OpenCloud\Database\Resource\Instance;
2323
use OpenCloud\Database\Resource\Configuration;
2424
use OpenCloud\Database\Resource\Datastore;
25+
use OpenCloud\Database\Resource\Backup;
2526

2627
/**
2728
* The Rackspace Database service
@@ -105,4 +106,29 @@ public function datastoreList($params = array())
105106

106107
return $this->resourceList('Datastore', $url);
107108
}
109+
110+
/**
111+
* Returns a Backup
112+
*
113+
* @param string $id the ID of the backup to retrieve
114+
* @return \OpenCloud\Database\Resource\Backup
115+
*/
116+
public function backup($id = null)
117+
{
118+
return $this->resource('Backup', $id);
119+
}
120+
121+
/**
122+
* Returns a Collection of Backup objects
123+
*
124+
* @param array $params
125+
* @return \OpenCloud\Common\Collection\PaginatedIterator
126+
*/
127+
public function backupList($params = array())
128+
{
129+
$url = clone $this->getUrl();
130+
$url->addPath(Backup::resourceName())->setQuery($params);
131+
132+
return $this->resourceList('Backup', $url);
133+
}
108134
}

0 commit comments

Comments
 (0)