blob: baaf5a0b4f6946334d55fd680e38b772801eca1d [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
72 def wait_for_backup_status(self, backup_id, status):
73 """Waits for a Backup to reach a given status."""
74 resp, body = self.get_backup(backup_id)
75 backup_status = body['status']
76 start = int(time.time())
77
78 while backup_status != status:
79 time.sleep(self.build_interval)
80 resp, body = self.get_backup(backup_id)
81 backup_status = body['status']
82 if backup_status == 'error':
83 raise exceptions.VolumeBackupException(backup_id=backup_id)
84
85 if int(time.time()) - start >= self.build_timeout:
86 message = ('Volume backup %s failed to reach %s status within '
87 'the required time (%s s).' %
88 (backup_id, status, self.build_timeout))
89 raise exceptions.TimeoutException(message)