Skip to content

Commit 29e1f27

Browse files
author
Jamie Hannaford
committed
Merge pull request rackspace#605 from evanlucas/backups
Add ability to interact with backups.
2 parents e2ce394 + 6742812 commit 29e1f27

File tree

9 files changed

+311
-6
lines changed

9 files changed

+311
-6
lines changed
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+
}
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/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
}

tests/OpenCloud/Tests/Database/DatabaseTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function setupObjects()
3939
$this->configuration = $this->service->configuration('005a8bb7-a8df-40ee-b0b7-fc144641abc2');
4040
$this->datastore = $this->service->datastore('10000000-0000-0000-0000-000000000001');
4141
$this->datastoreVersion = $this->datastore->version('b00000b0-00b0-0b00-00b0-000b000000bb');
42+
$this->backup = $this->service->backup();
4243
}
4344

4445
protected function assertCriticalMessageWasLogged()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Tests\Database\Resource;
19+
20+
use OpenCloud\Tests\Database\DatabaseTestCase;
21+
22+
class BackupTest extends DatabaseTestCase
23+
{
24+
public function test_Class()
25+
{
26+
$this->assertInstanceOf('OpenCloud\Database\Resource\Backup', $this->backup);
27+
}
28+
29+
public function testDelete()
30+
{
31+
$this->backup->delete();
32+
}
33+
34+
/**
35+
* @expectedException OpenCloud\Common\Exceptions\BackupInstanceError
36+
*/
37+
public function test_Create_Fails_Without_InstanceId()
38+
{
39+
$this->assertFalse($this->backup->create(array(
40+
'name' => 'test',
41+
'description' => 'test desc'
42+
)));
43+
}
44+
45+
/**
46+
* @expectedException OpenCloud\Common\Exceptions\BackupNameError
47+
*/
48+
public function test_Create_Fails_Without_Name()
49+
{
50+
$this->assertFalse($this->backup->create(array(
51+
'description' => 'test description',
52+
'instanceId' => '1234'
53+
)));
54+
}
55+
56+
public function testCreate()
57+
{
58+
$this->backup->create(array(
59+
'name' => 'test backup',
60+
'instanceId' => '1234'
61+
));
62+
63+
$this->assertEquals('test backup', $this->backup->name);
64+
$this->assertEquals('1234', $this->backup->instanceId);
65+
}
66+
}

tests/OpenCloud/Tests/Database/Resource/InstanceTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function testUpdateJson()
3131
$replacementValues = array(
3232
'configuration' => '005a8bb7-a8df-40ee-b0b7-fc144641abc2'
3333
);
34-
34+
3535
$method = new \ReflectionMethod('OpenCloud\Database\Resource\Instance', 'updateJson');
3636
$method->setAccessible(true);
37-
37+
3838
$expected = (object) array(
3939
'instance' => $replacementValues
4040
);
@@ -91,4 +91,18 @@ public function testUserList()
9191
{
9292
$this->assertInstanceOf(self::COLLECTION_CLASS, $this->instance->userList());
9393
}
94+
95+
public function testBackupList()
96+
{
97+
$this->assertInstanceOf(self::COLLECTION_CLASS, $this->instance->backupList());
98+
}
99+
100+
public function testCreateBackup()
101+
{
102+
$backup = $this->instance->createBackup(array(
103+
'name' => 'test-backup',
104+
'description' => 'test'
105+
));
106+
$this->assertInstanceOf('OpenCloud\Database\Resource\Backup', $backup);
107+
}
94108
}

tests/OpenCloud/Tests/Database/ServiceTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,14 @@ public function testDatastoreList()
7373
{
7474
$this->assertInstanceOf(self::COLLECTION_CLASS, $this->service->datastoreList());
7575
}
76+
77+
public function testBackup()
78+
{
79+
$this->assertInstanceOf('OpenCloud\Database\Resource\Backup', $this->service->Backup());
80+
}
81+
82+
public function testBackupList()
83+
{
84+
$this->assertInstanceOf(self::COLLECTION_CLASS, $this->service->backupList());
85+
}
7686
}

0 commit comments

Comments
 (0)