blob: 183d06bb6085d38ea29cf942b84196a3a2e347d6 [file] [log] [blame]
Giulio Fidente74b08ad2014-01-18 04:02:51 +01001# Copyright 2014 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import json
17import time
18
19from tempest.common import rest_client
20from tempest import config
21from tempest import exceptions
22
23CONF = config.CONF
24
25
26class BackupsClientJSON(rest_client.RestClient):
27 """
28 Client class to send CRUD Volume backup API requests to a Cinder endpoint
29 """
30
31 def __init__(self, auth_provider):
32 super(BackupsClientJSON, self).__init__(auth_provider)
33 self.service = CONF.volume.catalog_type
34 self.build_interval = CONF.volume.build_interval
35 self.build_timeout = CONF.volume.build_timeout
36
37 def create_backup(self, volume_id, container=None, name=None,
38 description=None):
39 """Creates a backup of volume."""
40 post_body = {'volume_id': volume_id}
41 if container:
42 post_body['container'] = container
43 if name:
44 post_body['name'] = name
45 if description:
46 post_body['description'] = description
47 post_body = json.dumps({'backup': post_body})
48 resp, body = self.post('backups', post_body)
49 body = json.loads(body)
50 return resp, body['backup']
51
52 def restore_backup(self, backup_id, volume_id=None):
53 """Restore volume from backup."""
54 post_body = {'volume_id': volume_id}
55 post_body = json.dumps({'restore': post_body})
56 resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
57 body = json.loads(body)
58 return resp, body['restore']
59
60 def delete_backup(self, backup_id):
61 """Delete a backup of volume."""
62 resp, body = self.delete('backups/%s' % (str(backup_id)))
63 return resp, body
64
65 def get_backup(self, backup_id):
66 """Returns the details of a single backup."""
67 url = "backups/%s" % str(backup_id)
68 resp, body = self.get(url)
69 body = json.loads(body)
70 return resp, body['backup']
71
raiesmh08f04da342014-02-28 17:14:43 +053072 def list_backups_with_detail(self):
73 """Information for all the tenant's backups."""
74 url = "backups/detail"
75 resp, body = self.get(url)
76 body = json.loads(body)
77 return resp, body['backups']
78
Giulio Fidente74b08ad2014-01-18 04:02:51 +010079 def wait_for_backup_status(self, backup_id, status):
80 """Waits for a Backup to reach a given status."""
81 resp, body = self.get_backup(backup_id)
82 backup_status = body['status']
83 start = int(time.time())
84
85 while backup_status != status:
86 time.sleep(self.build_interval)
87 resp, body = self.get_backup(backup_id)
88 backup_status = body['status']
89 if backup_status == 'error':
90 raise exceptions.VolumeBackupException(backup_id=backup_id)
91
92 if int(time.time()) - start >= self.build_timeout:
93 message = ('Volume backup %s failed to reach %s status within '
94 'the required time (%s s).' %
95 (backup_id, status, self.build_timeout))
96 raise exceptions.TimeoutException(message)