blob: 780da7b9518800e7f5f93e8d165b3f58ed546715 [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
Giulio Fidente74b08ad2014-01-18 04:02:51 +010016import time
17
Matthew Treinish21905512015-07-13 10:33:35 -040018from oslo_serialization import jsonutils as json
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000019
Giulio Fidente74b08ad2014-01-18 04:02:51 +010020from tempest import exceptions
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080021from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Giulio Fidente74b08ad2014-01-18 04:02:51 +010023
24
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080025class BaseBackupsClient(rest_client.RestClient):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000026 """Client class to send CRUD Volume backup API requests"""
Giulio Fidente74b08ad2014-01-18 04:02:51 +010027
Ghanshyam4a9e3032015-12-10 17:50:54 +090028 def create_backup(self, **kwargs):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010029 """Creates a backup of volume."""
Ghanshyam4a9e3032015-12-10 17:50:54 +090030 post_body = json.dumps({'backup': kwargs})
Giulio Fidente74b08ad2014-01-18 04:02:51 +010031 resp, body = self.post('backups', post_body)
32 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000033 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080034 return rest_client.ResponseBody(resp, body)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010035
Ghanshyam4a9e3032015-12-10 17:50:54 +090036 def restore_backup(self, backup_id, **kwargs):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010037 """Restore volume from backup."""
Ghanshyam4a9e3032015-12-10 17:50:54 +090038 post_body = json.dumps({'restore': kwargs})
Giulio Fidente74b08ad2014-01-18 04:02:51 +010039 resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
40 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000041 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080042 return rest_client.ResponseBody(resp, body)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010043
44 def delete_backup(self, backup_id):
45 """Delete a backup of volume."""
46 resp, body = self.delete('backups/%s' % (str(backup_id)))
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000047 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080048 return rest_client.ResponseBody(resp, body)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010049
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000050 def show_backup(self, backup_id):
Giulio Fidente74b08ad2014-01-18 04:02:51 +010051 """Returns the details of a single backup."""
52 url = "backups/%s" % str(backup_id)
53 resp, body = self.get(url)
54 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000055 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080056 return rest_client.ResponseBody(resp, body)
Giulio Fidente74b08ad2014-01-18 04:02:51 +010057
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000058 def list_backups(self, detail=False):
raiesmh08f04da342014-02-28 17:14:43 +053059 """Information for all the tenant's backups."""
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000060 url = "backups"
61 if detail:
62 url += "/detail"
raiesmh08f04da342014-02-28 17:14:43 +053063 resp, body = self.get(url)
64 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000065 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080066 return rest_client.ResponseBody(resp, body)
raiesmh08f04da342014-02-28 17:14:43 +053067
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000068 def export_backup(self, backup_id):
69 """Export backup metadata record."""
70 url = "backups/%s/export_record" % backup_id
71 resp, body = self.get(url)
72 body = json.loads(body)
73 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080074 return rest_client.ResponseBody(resp, body)
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000075
Ghanshyam4a9e3032015-12-10 17:50:54 +090076 def import_backup(self, **kwargs):
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000077 """Import backup metadata record."""
Ghanshyam4a9e3032015-12-10 17:50:54 +090078 post_body = json.dumps({'backup-record': kwargs})
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000079 resp, body = self.post("backups/import_record", post_body)
80 body = json.loads(body)
81 self.expected_success(201, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080082 return rest_client.ResponseBody(resp, body)
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +000083
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."""
John Warren6cadeee2015-08-13 17:00:56 +000086 body = self.show_backup(backup_id)['backup']
Giulio Fidente74b08ad2014-01-18 04:02:51 +010087 backup_status = body['status']
88 start = int(time.time())
89
90 while backup_status != status:
91 time.sleep(self.build_interval)
John Warren6cadeee2015-08-13 17:00:56 +000092 body = self.show_backup(backup_id)['backup']
Giulio Fidente74b08ad2014-01-18 04:02:51 +010093 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:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +020098 message = ('Volume backup %s failed to reach %s status '
99 '(current %s) within the required time (%s s).' %
100 (backup_id, status, backup_status,
101 self.build_timeout))
Giulio Fidente74b08ad2014-01-18 04:02:51 +0100102 raise exceptions.TimeoutException(message)
jun xieebc3da32014-11-18 14:34:56 +0800103
ravikumar-venkatesan18c1d0e2015-03-18 11:28:37 +0000104 def wait_for_backup_deletion(self, backup_id):
105 """Waits for backup deletion"""
106 start_time = int(time.time())
107 while True:
108 try:
109 self.show_backup(backup_id)
110 except lib_exc.NotFound:
111 return
112 if int(time.time()) - start_time >= self.build_timeout:
113 raise exceptions.TimeoutException
114 time.sleep(self.build_interval)