Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 1 | # 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 | |
| 16 | import json |
| 17 | import time |
| 18 | |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 19 | from tempest.common import service_client |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 20 | from tempest import exceptions |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 21 | |
| 22 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame^] | 23 | class BaseBackupsClient(service_client.ServiceClient): |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 24 | """ |
| 25 | Client class to send CRUD Volume backup API requests to a Cinder endpoint |
| 26 | """ |
| 27 | |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 28 | 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 Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 41 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 42 | return service_client.ResponseBody(resp, body['backup']) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 43 | |
| 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 Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 50 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 51 | return service_client.ResponseBody(resp, body['restore']) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 52 | |
| 53 | def delete_backup(self, backup_id): |
| 54 | """Delete a backup of volume.""" |
| 55 | resp, body = self.delete('backups/%s' % (str(backup_id))) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 56 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 57 | return service_client.ResponseBody(resp, body) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 58 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 59 | def show_backup(self, backup_id): |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 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 Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 64 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 65 | return service_client.ResponseBody(resp, body['backup']) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 66 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 67 | def list_backups(self, detail=False): |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 68 | """Information for all the tenant's backups.""" |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 69 | url = "backups" |
| 70 | if detail: |
| 71 | url += "/detail" |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 72 | resp, body = self.get(url) |
| 73 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 74 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 75 | return service_client.ResponseBodyList(resp, body['backups']) |
raiesmh08 | f04da34 | 2014-02-28 17:14:43 +0530 | [diff] [blame] | 76 | |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 77 | def wait_for_backup_status(self, backup_id, status): |
| 78 | """Waits for a Backup to reach a given status.""" |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 79 | body = self.show_backup(backup_id) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 80 | backup_status = body['status'] |
| 81 | start = int(time.time()) |
| 82 | |
| 83 | while backup_status != status: |
| 84 | time.sleep(self.build_interval) |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 85 | body = self.show_backup(backup_id) |
Giulio Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 86 | 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 Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 91 | 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 Fidente | 74b08ad | 2014-01-18 04:02:51 +0100 | [diff] [blame] | 95 | raise exceptions.TimeoutException(message) |
jun xie | ebc3da3 | 2014-11-18 14:34:56 +0800 | [diff] [blame] | 96 | |
| 97 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame^] | 98 | class BackupsClient(BaseBackupsClient): |
jun xie | ebc3da3 | 2014-11-18 14:34:56 +0800 | [diff] [blame] | 99 | """Volume V1 Backups client""" |