blob: dd0f407da9ebae0312b91ef7e29fca0f9c7f1e7d [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
Daniel Mellado8ea47c22016-09-19 10:36:19 +000020class SnapshotsClient(rest_client.RestClient):
21 """Client class to send CRUD Volume V2 API requests."""
22 api_version = "v2"
23 create_resp = 202
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
Dong Ma127887a2016-10-19 09:09:11 -070028 For a full list of available parameters, please refer to the official
29 API reference:
30 http://developer.openstack.org/api-ref-blockstorage-v2.html#listSnapshots
Lv Fumei62ce7732016-07-08 17:01:27 +080031 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010032 url = 'snapshots'
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000033 if detail:
34 url += '/detail'
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010035 if params:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000036 url += '?%s' % urllib.urlencode(params)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010037
38 resp, body = self.get(url)
39 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000040 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080041 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010042
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000043 def show_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080044 """Returns the details of a single snapshot.
45
Dong Ma127887a2016-10-19 09:09:11 -070046 For a full list of available parameters, please refer to the official
47 API reference:
48 http://developer.openstack.org/api-ref-blockstorage-v2.html#showSnapshot
Lv Fumei62ce7732016-07-08 17:01:27 +080049 """
guo yunxian6cdf0562016-08-17 16:21:52 +080050 url = "snapshots/%s" % snapshot_id
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010051 resp, body = self.get(url)
52 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000053 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080054 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010055
Ghanshyam0b75b632015-12-11 15:08:28 +090056 def create_snapshot(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000057 """Creates a new snapshot.
58
Dong Ma127887a2016-10-19 09:09:11 -070059 For a full list of available parameters, please refer to the official
60 API reference:
61 http://developer.openstack.org/api-ref-blockstorage-v2.html#createSnapshot
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010062 """
Ghanshyam0b75b632015-12-11 15:08:28 +090063 post_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020064 resp, body = self.post('snapshots', post_body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010065 body = json.loads(body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +080066 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080067 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010068
QingXin Mengdc95f5e2013-09-16 19:06:44 -070069 def update_snapshot(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +080070 """Updates a snapshot.
71
Dong Ma127887a2016-10-19 09:09:11 -070072 For a full list of available parameters, please refer to the official
73 API reference:
74 http://developer.openstack.org/api-ref-blockstorage-v2.html#updateSnapshot
zhuflb9255a52016-06-24 10:29:43 +080075 """
QingXin Mengdc95f5e2013-09-16 19:06:44 -070076 put_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020077 resp, body = self.put('snapshots/%s' % snapshot_id, put_body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070078 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000079 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080080 return rest_client.ResponseBody(resp, body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070081
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010082 def delete_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080083 """Delete Snapshot.
84
Dong Ma127887a2016-10-19 09:09:11 -070085 For a full list of available parameters, please refer to the official
86 API reference:
87 http://developer.openstack.org/api-ref-blockstorage-v2.html#deleteSnapshot
Lv Fumei62ce7732016-07-08 17:01:27 +080088 """
guo yunxian6cdf0562016-08-17 16:21:52 +080089 resp, body = self.delete("snapshots/%s" % snapshot_id)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000090 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080091 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010092
93 def is_resource_deleted(self, id):
94 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000095 self.show_snapshot(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090096 except lib_exc.NotFound:
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010097 return True
98 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +080099
Matt Riedemannd2b96512014-10-13 10:18:16 -0700100 @property
101 def resource_type(self):
102 """Returns the primary type of resource this client works with."""
103 return 'volume-snapshot'
104
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800105 def reset_snapshot_status(self, snapshot_id, status):
106 """Reset the specified snapshot's status."""
107 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200108 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000109 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800110 return rest_client.ResponseBody(resp, body)
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800111
Ghanshyam0b75b632015-12-11 15:08:28 +0900112 def update_snapshot_status(self, snapshot_id, **kwargs):
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800113 """Update the specified snapshot's status."""
Ghanshyam0b75b632015-12-11 15:08:28 +0900114 # TODO(gmann): api-site doesn't contain doc ref
115 # for this API. After fixing the api-site, we need to
116 # add the link here.
117 # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645
118
119 post_body = json.dumps({'os-update_snapshot_status': kwargs})
guo yunxian6cdf0562016-08-17 16:21:52 +0800120 url = 'snapshots/%s/action' % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200121 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000122 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800123 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800124
125 def create_snapshot_metadata(self, snapshot_id, metadata):
126 """Create metadata for the snapshot."""
127 put_body = json.dumps({'metadata': metadata})
guo yunxian6cdf0562016-08-17 16:21:52 +0800128 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200129 resp, body = self.post(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800130 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000131 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800132 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800133
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000134 def show_snapshot_metadata(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +0800135 """Get metadata of the snapshot.
136
Dong Ma127887a2016-10-19 09:09:11 -0700137 For a full list of available parameters, please refer to the official
138 API reference:
139 http://developer.openstack.org/api-ref-blockstorage-v2.html#showSnapshotMetadata
Lv Fumei62ce7732016-07-08 17:01:27 +0800140 """
guo yunxian6cdf0562016-08-17 16:21:52 +0800141 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200142 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800143 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000144 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800145 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800146
piyush11078661fd3f02015-12-24 17:09:45 +0530147 def update_snapshot_metadata(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +0800148 """Update metadata for the snapshot.
149
Dong Ma127887a2016-10-19 09:09:11 -0700150 For a full list of available parameters, please refer to the official
151 API reference:
152 http://developer.openstack.org/api-ref-blockstorage-v2.html#updateSnapshotMetadata
zhuflb9255a52016-06-24 10:29:43 +0800153 """
piyush11078661fd3f02015-12-24 17:09:45 +0530154 put_body = json.dumps(kwargs)
guo yunxian6cdf0562016-08-17 16:21:52 +0800155 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200156 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800157 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000158 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800159 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800160
piyush11078661fd3f02015-12-24 17:09:45 +0530161 def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs):
huangtianhua1346d702013-12-09 18:42:35 +0800162 """Update metadata item for the snapshot."""
piyush11078661fd3f02015-12-24 17:09:45 +0530163 # TODO(piyush): Current api-site doesn't contain this API description.
164 # After fixing the api-site, we need to fix here also for putting the
165 # link to api-site.
166 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064
167 put_body = json.dumps(kwargs)
guo yunxian6cdf0562016-08-17 16:21:52 +0800168 url = "snapshots/%s/metadata/%s" % (snapshot_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200169 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800170 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000171 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800172 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800173
174 def delete_snapshot_metadata_item(self, snapshot_id, id):
175 """Delete metadata item for the snapshot."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800176 url = "snapshots/%s/metadata/%s" % (snapshot_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200177 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000178 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800179 return rest_client.ResponseBody(resp, body)
wanghaofa3908c2014-01-15 19:34:03 +0800180
181 def force_delete_snapshot(self, snapshot_id):
182 """Force Delete Snapshot."""
183 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200184 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000185 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800186 return rest_client.ResponseBody(resp, body)