blob: fb643330c835c94b151febc97858f6ab4cef645d [file] [log] [blame]
jeremy.zhangf4ce4172017-07-05 12:27:35 +08001# Copyright 2017 FiberHome Telecommunication Technologies CO.,LTD
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
16from oslo_serialization import jsonutils as json
ghanshyamde676ba2018-02-19 06:20:00 +000017from six.moves.urllib import parse as urllib
jeremy.zhangf4ce4172017-07-05 12:27:35 +080018
19from tempest.lib.common import rest_client
ghanshyamde676ba2018-02-19 06:20:00 +000020from tempest.lib import exceptions as lib_exc
21from tempest.lib.services.volume import base_client
jeremy.zhangf4ce4172017-07-05 12:27:35 +080022
23
ghanshyamde676ba2018-02-19 06:20:00 +000024class BackupsClient(base_client.BaseClient):
jeremy.zhangf4ce4172017-07-05 12:27:35 +080025 """Volume V3 Backups client"""
jeremy.zhangf4ce4172017-07-05 12:27:35 +080026
ghanshyamde676ba2018-02-19 06:20:00 +000027 def create_backup(self, **kwargs):
28 """Creates a backup of volume.
29
30 For a full list of available parameters, please refer to the official
31 API reference:
32 https://developer.openstack.org/api-ref/block-storage/v3/index.html#create-a-backup
33 """
34 post_body = json.dumps({'backup': kwargs})
35 resp, body = self.post('backups', post_body)
36 body = json.loads(body)
37 self.expected_success(202, resp.status)
38 return rest_client.ResponseBody(resp, body)
39
jeremy.zhangf4ce4172017-07-05 12:27:35 +080040 def update_backup(self, backup_id, **kwargs):
41 """Updates the specified volume backup.
42
43 For a full list of available parameters, please refer to the official
44 API reference:
45 https://developer.openstack.org/api-ref/block-storage/v3/#update-a-backup
46 """
47 put_body = json.dumps({'backup': kwargs})
48 resp, body = self.put('backups/%s' % backup_id, put_body)
49 body = json.loads(body)
50 self.expected_success(200, resp.status)
51 return rest_client.ResponseBody(resp, body)
ghanshyamde676ba2018-02-19 06:20:00 +000052
53 def restore_backup(self, backup_id, **kwargs):
54 """Restore volume from backup.
55
56 For a full list of available parameters, please refer to the official
57 API reference:
58 https://developer.openstack.org/api-ref/block-storage/v3/index.html#restore-a-backup
59 """
60 post_body = json.dumps({'restore': kwargs})
61 resp, body = self.post('backups/%s/restore' % (backup_id), post_body)
62 body = json.loads(body)
63 self.expected_success(202, resp.status)
64 return rest_client.ResponseBody(resp, body)
65
66 def delete_backup(self, backup_id):
67 """Delete a backup of volume."""
68 resp, body = self.delete('backups/%s' % backup_id)
69 self.expected_success(202, resp.status)
70 return rest_client.ResponseBody(resp, body)
71
72 def show_backup(self, backup_id):
73 """Returns the details of a single backup."""
74 url = "backups/%s" % backup_id
75 resp, body = self.get(url)
76 body = json.loads(body)
77 self.expected_success(200, resp.status)
78 return rest_client.ResponseBody(resp, body)
79
80 def list_backups(self, detail=False, **params):
81 """List all the tenant's backups.
82
83 For a full list of available parameters, please refer to the official
84 API reference:
85 https://developer.openstack.org/api-ref/block-storage/v3/index.html#list-backups-for-project
86 https://developer.openstack.org/api-ref/block-storage/v3/index.html#list-backups-with-detail
87 """
88 url = "backups"
89 if detail:
90 url += "/detail"
91 if params:
92 url += '?%s' % urllib.urlencode(params)
93 resp, body = self.get(url)
94 body = json.loads(body)
95 self.expected_success(200, resp.status)
96 return rest_client.ResponseBody(resp, body)
97
98 def export_backup(self, backup_id):
99 """Export backup metadata record."""
100 url = "backups/%s/export_record" % backup_id
101 resp, body = self.get(url)
102 body = json.loads(body)
103 self.expected_success(200, resp.status)
104 return rest_client.ResponseBody(resp, body)
105
106 def import_backup(self, **kwargs):
zhufle80266a2018-11-08 16:12:53 +0800107 """Import backup metadata record.
108
109 For a full list of available parameters, please refer to the official
110 API reference:
111 https://developer.openstack.org/api-ref/block-storage/v3/index.html#import-a-backup
112 """
ghanshyamde676ba2018-02-19 06:20:00 +0000113 post_body = json.dumps({'backup-record': kwargs})
114 resp, body = self.post("backups/import_record", post_body)
115 body = json.loads(body)
116 self.expected_success(201, resp.status)
117 return rest_client.ResponseBody(resp, body)
118
119 def reset_backup_status(self, backup_id, status):
120 """Reset the specified backup's status."""
121 post_body = json.dumps({'os-reset_status': {"status": status}})
122 resp, body = self.post('backups/%s/action' % backup_id, post_body)
123 self.expected_success(202, resp.status)
124 return rest_client.ResponseBody(resp, body)
125
126 def is_resource_deleted(self, id):
127 try:
128 self.show_backup(id)
129 except lib_exc.NotFound:
130 return True
131 return False
132
133 @property
134 def resource_type(self):
135 """Returns the primary type of resource this client works with."""
136 return 'backup'