blob: 7a8e12b9ccf8e8f0bdbb6f389c684df253743636 [file] [log] [blame]
Attila Fazekas36b1fcf2013-01-31 16:41:04 +01001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Matthew Treinish21905512015-07-13 10:33:35 -040013from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040014from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090015
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080016from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050017from tempest.lib import exceptions as lib_exc
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010018
Matthew Treinish684d8992014-01-30 16:27:40 +000019
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080020class BaseSnapshotsClient(rest_client.RestClient):
Zhi Kun Liu38641c62014-07-10 20:12:48 +080021 """Base Client class to send CRUD Volume API requests."""
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010022
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000023 create_resp = 200
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010024
Ghanshyam0b75b632015-12-11 15:08:28 +090025 def list_snapshots(self, detail=False, **params):
Lv Fumei62ce7732016-07-08 17:01:27 +080026 """List all the snapshot.
27
28 Available params: see http://developer.openstack.org/
29 api-ref-blockstorage-v2.html#listSnapshots
30 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010031 url = 'snapshots'
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000032 if detail:
33 url += '/detail'
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010034 if params:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000035 url += '?%s' % urllib.urlencode(params)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010036
37 resp, body = self.get(url)
38 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000039 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080040 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010041
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000042 def show_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080043 """Returns the details of a single snapshot.
44
45 Available params: see http://developer.openstack.org/
46 api-ref-blockstorage-v2.html#showSnapshot
47 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010048 url = "snapshots/%s" % str(snapshot_id)
49 resp, body = self.get(url)
50 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000051 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080052 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010053
Ghanshyam0b75b632015-12-11 15:08:28 +090054 def create_snapshot(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000055 """Creates a new snapshot.
56
Ghanshyam0b75b632015-12-11 15:08:28 +090057 Available params: see http://developer.openstack.org/
58 api-ref-blockstorage-v2.html#createSnapshot
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010059 """
Ghanshyam0b75b632015-12-11 15:08:28 +090060 post_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020061 resp, body = self.post('snapshots', post_body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010062 body = json.loads(body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +080063 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080064 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010065
QingXin Mengdc95f5e2013-09-16 19:06:44 -070066 def update_snapshot(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +080067 """Updates a snapshot.
68
69 Available params: see http://developer.openstack.org/
70 api-ref-blockstorage-v2.html#updateSnapshot
71 """
QingXin Mengdc95f5e2013-09-16 19:06:44 -070072 put_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020073 resp, body = self.put('snapshots/%s' % snapshot_id, put_body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070074 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000075 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080076 return rest_client.ResponseBody(resp, body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070077
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010078 def delete_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080079 """Delete Snapshot.
80
81 Available params: see http://developer.openstack.org/
82 api-ref-blockstorage-v2.html#deleteSnapshot
83 """
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000084 resp, body = self.delete("snapshots/%s" % str(snapshot_id))
85 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080086 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010087
88 def is_resource_deleted(self, id):
89 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000090 self.show_snapshot(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090091 except lib_exc.NotFound:
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010092 return True
93 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +080094
Matt Riedemannd2b96512014-10-13 10:18:16 -070095 @property
96 def resource_type(self):
97 """Returns the primary type of resource this client works with."""
98 return 'volume-snapshot'
99
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800100 def reset_snapshot_status(self, snapshot_id, status):
101 """Reset the specified snapshot's status."""
102 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200103 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000104 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800105 return rest_client.ResponseBody(resp, body)
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800106
Ghanshyam0b75b632015-12-11 15:08:28 +0900107 def update_snapshot_status(self, snapshot_id, **kwargs):
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800108 """Update the specified snapshot's status."""
Ghanshyam0b75b632015-12-11 15:08:28 +0900109 # TODO(gmann): api-site doesn't contain doc ref
110 # for this API. After fixing the api-site, we need to
111 # add the link here.
112 # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645
113
114 post_body = json.dumps({'os-update_snapshot_status': kwargs})
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800115 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200116 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000117 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800118 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800119
120 def create_snapshot_metadata(self, snapshot_id, metadata):
121 """Create metadata for the snapshot."""
122 put_body = json.dumps({'metadata': metadata})
123 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200124 resp, body = self.post(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800125 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000126 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800127 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800128
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000129 def show_snapshot_metadata(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +0800130 """Get metadata of the snapshot.
131
132 Available params: see http://developer.openstack.org/
133 api-ref-blockstorage-v2.html#
134 showSnapshotMetadata
135 """
huangtianhua1346d702013-12-09 18:42:35 +0800136 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200137 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800138 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000139 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800140 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800141
piyush11078661fd3f02015-12-24 17:09:45 +0530142 def update_snapshot_metadata(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +0800143 """Update metadata for the snapshot.
144
145 Available params: see http://developer.openstack.org/
Lv Fumei62ce7732016-07-08 17:01:27 +0800146 api-ref-blockstorage-v2.html#
147 updateSnapshotMetadata
zhuflb9255a52016-06-24 10:29:43 +0800148 """
piyush11078661fd3f02015-12-24 17:09:45 +0530149 put_body = json.dumps(kwargs)
huangtianhua1346d702013-12-09 18:42:35 +0800150 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200151 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800152 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000153 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800154 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800155
piyush11078661fd3f02015-12-24 17:09:45 +0530156 def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs):
huangtianhua1346d702013-12-09 18:42:35 +0800157 """Update metadata item for the snapshot."""
piyush11078661fd3f02015-12-24 17:09:45 +0530158 # TODO(piyush): Current api-site doesn't contain this API description.
159 # After fixing the api-site, we need to fix here also for putting the
160 # link to api-site.
161 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064
162 put_body = json.dumps(kwargs)
huangtianhua1346d702013-12-09 18:42:35 +0800163 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200164 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800165 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000166 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800167 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800168
169 def delete_snapshot_metadata_item(self, snapshot_id, id):
170 """Delete metadata item for the snapshot."""
171 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200172 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000173 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800174 return rest_client.ResponseBody(resp, body)
wanghaofa3908c2014-01-15 19:34:03 +0800175
176 def force_delete_snapshot(self, snapshot_id):
177 """Force Delete Snapshot."""
178 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200179 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000180 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800181 return rest_client.ResponseBody(resp, body)