blob: da47639e009d0c8206e8adc79e8dfa45d0681b9e [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
jun xieebc3da32014-11-18 14:34:56 +080026class BaseBackupsClientJSON(rest_client.RestClient):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010027 """
28 Client class to send CRUD Volume backup API requests to a Cinder endpoint
29 """
30
31 def __init__(self, auth_provider):
jun xieebc3da32014-11-18 14:34:56 +080032 super(BaseBackupsClientJSON, self).__init__(auth_provider)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010033 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)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000050 self.expected_success(202, resp.status)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010051 return resp, body['backup']
52
53 def restore_backup(self, backup_id, volume_id=None):
54 """Restore volume from backup."""
55 post_body = {'volume_id': volume_id}
56 post_body = json.dumps({'restore': post_body})
57 resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
58 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000059 self.expected_success(202, resp.status)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010060 return resp, body['restore']
61
62 def delete_backup(self, backup_id):
63 """Delete a backup of volume."""
64 resp, body = self.delete('backups/%s' % (str(backup_id)))
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000065 self.expected_success(202, resp.status)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010066 return resp, body
67
68 def get_backup(self, backup_id):
69 """Returns the details of a single backup."""
70 url = "backups/%s" % str(backup_id)
71 resp, body = self.get(url)
72 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000073 self.expected_success(200, resp.status)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010074 return resp, body['backup']
75
raiesmh08f04da342014-02-28 17:14:43 +053076 def list_backups_with_detail(self):
77 """Information for all the tenant's backups."""
78 url = "backups/detail"
79 resp, body = self.get(url)
80 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000081 self.expected_success(200, resp.status)
raiesmh08f04da342014-02-28 17:14:43 +053082 return resp, body['backups']
83
Giulio Fidente74b08ad2014-01-18 04:02:51 +010084 def wait_for_backup_status(self, backup_id, status):
85 """Waits for a Backup to reach a given status."""
86 resp, body = self.get_backup(backup_id)
87 backup_status = body['status']
88 start = int(time.time())
89
90 while backup_status != status:
91 time.sleep(self.build_interval)
92 resp, body = self.get_backup(backup_id)
93 backup_status = body['status']
94 if backup_status == 'error':
95 raise exceptions.VolumeBackupException(backup_id=backup_id)
96
97 if int(time.time()) - start >= self.build_timeout:
98 message = ('Volume backup %s failed to reach %s status within '
99 'the required time (%s s).' %
100 (backup_id, status, self.build_timeout))
101 raise exceptions.TimeoutException(message)
jun xieebc3da32014-11-18 14:34:56 +0800102
103
104class BackupsClientJSON(BaseBackupsClientJSON):
105 """Volume V1 Backups client"""