blob: dad5aff224a0312f87d5e47ac052284bd08aae6b [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
Joseph Lanoux6809bab2014-12-18 14:57:18 +000019from tempest.common import service_client
Giulio Fidente74b08ad2014-01-18 04:02:51 +010020from tempest import exceptions
Giulio Fidente74b08ad2014-01-18 04:02:51 +010021
22
Ken'ichi Ohmichif85e9bd2015-01-27 12:51:47 +000023class BaseBackupsClientJSON(service_client.ServiceClient):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010024 """
25 Client class to send CRUD Volume backup API requests to a Cinder endpoint
26 """
27
Giulio Fidente74b08ad2014-01-18 04:02:51 +010028 def create_backup(self, volume_id, container=None, name=None,
29 description=None):
30 """Creates a backup of volume."""
31 post_body = {'volume_id': volume_id}
32 if container:
33 post_body['container'] = container
34 if name:
35 post_body['name'] = name
36 if description:
37 post_body['description'] = description
38 post_body = json.dumps({'backup': post_body})
39 resp, body = self.post('backups', post_body)
40 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000041 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000042 return service_client.ResponseBody(resp, body['backup'])
Giulio Fidente74b08ad2014-01-18 04:02:51 +010043
44 def restore_backup(self, backup_id, volume_id=None):
45 """Restore volume from backup."""
46 post_body = {'volume_id': volume_id}
47 post_body = json.dumps({'restore': post_body})
48 resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
49 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000050 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000051 return service_client.ResponseBody(resp, body['restore'])
Giulio Fidente74b08ad2014-01-18 04:02:51 +010052
53 def delete_backup(self, backup_id):
54 """Delete a backup of volume."""
55 resp, body = self.delete('backups/%s' % (str(backup_id)))
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000056 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000057 return service_client.ResponseBody(resp, body)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010058
59 def get_backup(self, backup_id):
60 """Returns the details of a single backup."""
61 url = "backups/%s" % str(backup_id)
62 resp, body = self.get(url)
63 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000064 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000065 return service_client.ResponseBody(resp, body['backup'])
Giulio Fidente74b08ad2014-01-18 04:02:51 +010066
raiesmh08f04da342014-02-28 17:14:43 +053067 def list_backups_with_detail(self):
68 """Information for all the tenant's backups."""
69 url = "backups/detail"
70 resp, body = self.get(url)
71 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000072 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000073 return service_client.ResponseBodyList(resp, body['backups'])
raiesmh08f04da342014-02-28 17:14:43 +053074
Giulio Fidente74b08ad2014-01-18 04:02:51 +010075 def wait_for_backup_status(self, backup_id, status):
76 """Waits for a Backup to reach a given status."""
Joseph Lanoux6809bab2014-12-18 14:57:18 +000077 body = self.get_backup(backup_id)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010078 backup_status = body['status']
79 start = int(time.time())
80
81 while backup_status != status:
82 time.sleep(self.build_interval)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000083 body = self.get_backup(backup_id)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010084 backup_status = body['status']
85 if backup_status == 'error':
86 raise exceptions.VolumeBackupException(backup_id=backup_id)
87
88 if int(time.time()) - start >= self.build_timeout:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +020089 message = ('Volume backup %s failed to reach %s status '
90 '(current %s) within the required time (%s s).' %
91 (backup_id, status, backup_status,
92 self.build_timeout))
Giulio Fidente74b08ad2014-01-18 04:02:51 +010093 raise exceptions.TimeoutException(message)
jun xieebc3da32014-11-18 14:34:56 +080094
95
96class BackupsClientJSON(BaseBackupsClientJSON):
97 """Volume V1 Backups client"""