blob: bf9af6ed4d72d6ad218e677a2a0032ec68771e86 [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 Ohmichia6287072015-07-02 02:43:15 +000023class BaseBackupsClient(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
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000059 def show_backup(self, backup_id):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010060 """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
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000067 def list_backups(self, detail=False):
raiesmh08f04da342014-02-28 17:14:43 +053068 """Information for all the tenant's backups."""
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000069 url = "backups"
70 if detail:
71 url += "/detail"
raiesmh08f04da342014-02-28 17:14:43 +053072 resp, body = self.get(url)
73 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000074 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000075 return service_client.ResponseBodyList(resp, body['backups'])
raiesmh08f04da342014-02-28 17:14:43 +053076
Giulio Fidente74b08ad2014-01-18 04:02:51 +010077 def wait_for_backup_status(self, backup_id, status):
78 """Waits for a Backup to reach a given status."""
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000079 body = self.show_backup(backup_id)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010080 backup_status = body['status']
81 start = int(time.time())
82
83 while backup_status != status:
84 time.sleep(self.build_interval)
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000085 body = self.show_backup(backup_id)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010086 backup_status = body['status']
87 if backup_status == 'error':
88 raise exceptions.VolumeBackupException(backup_id=backup_id)
89
90 if int(time.time()) - start >= self.build_timeout:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +020091 message = ('Volume backup %s failed to reach %s status '
92 '(current %s) within the required time (%s s).' %
93 (backup_id, status, backup_status,
94 self.build_timeout))
Giulio Fidente74b08ad2014-01-18 04:02:51 +010095 raise exceptions.TimeoutException(message)
jun xieebc3da32014-11-18 14:34:56 +080096
97
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000098class BackupsClient(BaseBackupsClient):
jun xieebc3da32014-11-18 14:34:56 +080099 """Volume V1 Backups client"""