Skip to content

Commit

Permalink
Merge pull request #620 from rackspace/working
Browse files Browse the repository at this point in the history
Merge working into master before new release
  • Loading branch information
Jamie Hannaford committed Aug 4, 2015
2 parents 5532b88 + 6737f9e commit 46745a4
Show file tree
Hide file tree
Showing 28 changed files with 556 additions and 45 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"require-dev" : {
"phpunit/phpunit": "4.3.*",
"phpspec/prophecy": "~1.4",
"satooshi/php-coveralls": "0.6.*@dev",
"jakub-onderka/php-parallel-lint": "0.*",
"fabpot/php-cs-fixer": "1.0.*@dev",
Expand Down
12 changes: 12 additions & 0 deletions doc/services/database/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ Check if root user is enabled
// true for yes, false for no
$instance->isRootEnabled();
Grant database access
---------------------

To grant access to one or more databases, you can run:

.. code-block:: php
$user = $instance->user('{userName}');
$user->grantDbAccess(['{dbName1}', '{dbName2}']);
15 changes: 15 additions & 0 deletions doc/services/networking/ports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ You can list all the ports to which you have access as shown in the following ex
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Networking/list-ports.php>`_

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>`_

.. code-block:: php
$ports = $networkingService->listPorts([
'status' => 'ACTIVE',
'device_id' => '9ae135f4-b6e0-4dad-9e91-3c223e385824'
]);
foreach ($ports as $port) {
/** @var $port OpenCloud\Networking\Resource\Port **/
}
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Networking/list-ports-filtered.php>`_


Get a port
----------
Expand Down
22 changes: 22 additions & 0 deletions lib/OpenCloud/Common/Exceptions/BackupInstanceError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Common\Exceptions;

class BackupInstanceError extends \Exception
{
}
22 changes: 22 additions & 0 deletions lib/OpenCloud/Common/Exceptions/BackupNameError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Common\Exceptions;

class BackupNameError extends \Exception
{
}
10 changes: 5 additions & 5 deletions lib/OpenCloud/Common/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace OpenCloud\Common\Http;

use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\Curl\CurlVersion;
use OpenCloud\Version;
use OpenCloud\Common\Exceptions\UnsupportedVersionError;

/**
Expand All @@ -27,7 +27,6 @@
*/
class Client extends GuzzleClient
{
const VERSION = '1.9.0';
const MINIMUM_PHP_VERSION = '5.3.0';

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

public function getDefaultUserAgent()
{
return 'OpenCloud/' . self::VERSION
. ' cURL/' . CurlVersion::getInstance()->get('version')
. ' PHP/' . PHP_VERSION;
return 'OpenCloud/' . Version::getVersion()
. ' Guzzle/' . Version::getGuzzleVersion()
. ' cURL/' . Version::getCurlVersion()
. ' PHP/' . PHP_VERSION;
}

public function getUserAgent()
Expand Down
8 changes: 5 additions & 3 deletions lib/OpenCloud/Common/Service/CatalogItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,18 @@ public function getEndpoints()
/**
* Using a standard data object, extract its endpoint.
*
* @param $region
* @param string $region
* @param bool $isRegionless
*
* @return mixed
* @throws \OpenCloud\Common\Exceptions\EndpointError
*/
public function getEndpointFromRegion($region)
public function getEndpointFromRegion($region, $isRegionless = false)
{
foreach ($this->endpoints as $endpoint) {
// Return the endpoint if it is regionless OR if the endpoint's
// region matches the $region supplied by the caller.
if (!isset($endpoint->region) || $endpoint->region == $region) {
if ($isRegionless || !isset($endpoint->region) || $endpoint->region == $region) {
return $endpoint;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/OpenCloud/Common/Service/CatalogService.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ private function findEndpoint()
// Search each service to find The One
foreach ($catalog->getItems() as $service) {
if ($service->hasType($this->type) && $service->hasName($this->name)) {
return Endpoint::factory($service->getEndpointFromRegion($this->region), static::SUPPORTED_VERSION, $this->getClient());
$endpoint = $service->getEndpointFromRegion($this->region, $this->regionless);
return Endpoint::factory($endpoint, static::SUPPORTED_VERSION, $this->getClient());
}
}

Expand Down
92 changes: 92 additions & 0 deletions lib/OpenCloud/Database/Resource/Backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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\Lang;
use OpenCloud\Common\Exceptions;
use OpenCloud\Common\Resource\PersistentResource;
use OpenCloud\Database\Service;

/**
* This class represents a Backup
*/
class Backup extends PersistentResource
{
public $id;
public $name;
public $description;
public $created;
public $datastore;
public $updated;
public $instance;
public $instanceId;
public $locationRef;

protected static $json_name = 'backup';
protected static $url_resource = 'backups';

protected $createKeys = array(
'name',
'instanceId',
'description'
);

protected $associatedResources = array(
'instance' => 'Instance'
);

protected $aliases = array(
'instance_id' => 'instanceId'
);

public function __construct(Service $service, $info = null)
{
$this->instance = new \stdClass;
return parent::__construct($service, $info);
}

/**
* Returns the JSON object for creating the backup
*/
protected function createJson()
{
if (!isset($this->instanceId)) {
throw new Exceptions\BackupInstanceError(
Lang::translate('The `instanceId` attribute is required and must be a string')
);
}

if (!isset($this->name)) {
throw new Exceptions\BackupNameError(
Lang::translate('Backup name is required')
);
}

$out = [
'backup' => [
'name' => $this->name,
'instance' => $this->instanceId
]
];

if (isset($this->description)) {
$out['backup']['description'] = $this->description;
}
return (object) $out;
}
}
60 changes: 56 additions & 4 deletions lib/OpenCloud/Database/Resource/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Instance extends NovaResource
public $created;
public $updated;
public $flavor;
public $backupRef;

protected static $json_name = 'instance';
protected static $url_resource = 'instances';
Expand Down Expand Up @@ -171,6 +172,49 @@ 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()
*
Expand All @@ -190,13 +234,21 @@ protected function createJson()
);
}

return (object) array(
'instance' => (object) array(
$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;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/OpenCloud/Database/Resource/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,29 @@ protected function createJson()
'users' => array($user)
);
}

/**
* Grant access to a set of one or more databases to a user.
*
* @param []string $databases An array of one or more database names that this user will be granted access to. For
* example, ['foo', 'bar'] or ['baz'] are valid inputs.
*
* @return \Guzzle\Http\Message\Response
*/
public function grantDbAccess(array $databases)
{
$json = [];

foreach ($databases as $database) {
$json[] = ['name' => $database];
}

$json = ['databases' => $json];

$url = $this->getUrl('databases');
$headers = self::getJsonHeader();
$body = json_encode($json);

return $this->getClient()->put($url, $headers, $body)->send();
}
}
26 changes: 26 additions & 0 deletions lib/OpenCloud/Database/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OpenCloud\Database\Resource\Instance;
use OpenCloud\Database\Resource\Configuration;
use OpenCloud\Database\Resource\Datastore;
use OpenCloud\Database\Resource\Backup;

/**
* The Rackspace Database service
Expand Down Expand Up @@ -105,4 +106,29 @@ public function datastoreList($params = array())

return $this->resourceList('Datastore', $url);
}

/**
* Returns a Backup
*
* @param string $id the ID of the backup to retrieve
* @return \OpenCloud\Database\Resource\Backup
*/
public function backup($id = null)
{
return $this->resource('Backup', $id);
}

/**
* Returns a Collection of Backup objects
*
* @param array $params
* @return \OpenCloud\Common\Collection\PaginatedIterator
*/
public function backupList($params = array())
{
$url = clone $this->getUrl();
$url->addPath(Backup::resourceName())->setQuery($params);

return $this->resourceList('Backup', $url);
}
}

0 comments on commit 46745a4

Please sign in to comment.