blob: 51f7b9bc8e9f3e76a715100940957f5ff034bc02 [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 V1 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
Dong Ma127887a2016-10-19 09:09:11 -070028 For a full list of available parameters, please refer to the official
29 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +000030 https://developer.openstack.org/api-ref/block-storage/v2/#list-snapshots
31 https://developer.openstack.org/api-ref/block-storage/v2/#list-snapshots-with-details
Lv Fumei62ce7732016-07-08 17:01:27 +080032 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010033 url = 'snapshots'
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000034 if detail:
35 url += '/detail'
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010036 if params:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000037 url += '?%s' % urllib.urlencode(params)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010038
39 resp, body = self.get(url)
40 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000041 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080042 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010043
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000044 def show_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080045 """Returns the details of a single snapshot.
46
Dong Ma127887a2016-10-19 09:09:11 -070047 For a full list of available parameters, please refer to the official
48 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +000049 https://developer.openstack.org/api-ref/block-storage/v2/#show-snapshot-details
Lv Fumei62ce7732016-07-08 17:01:27 +080050 """
guo yunxian6cdf0562016-08-17 16:21:52 +080051 url = "snapshots/%s" % snapshot_id
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010052 resp, body = self.get(url)
53 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000054 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080055 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010056
Ghanshyam0b75b632015-12-11 15:08:28 +090057 def create_snapshot(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000058 """Creates a new snapshot.
59
Dong Ma127887a2016-10-19 09:09:11 -070060 For a full list of available parameters, please refer to the official
61 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +000062 https://developer.openstack.org/api-ref/block-storage/v2/#create-snapshot
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010063 """
Ghanshyam0b75b632015-12-11 15:08:28 +090064 post_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020065 resp, body = self.post('snapshots', post_body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010066 body = json.loads(body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +080067 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080068 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010069
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010070 def delete_snapshot(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +080071 """Delete Snapshot.
72
Dong Ma127887a2016-10-19 09:09:11 -070073 For a full list of available parameters, please refer to the official
74 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +000075 https://developer.openstack.org/api-ref/block-storage/v2/#delete-snapshot
Lv Fumei62ce7732016-07-08 17:01:27 +080076 """
guo yunxian6cdf0562016-08-17 16:21:52 +080077 resp, body = self.delete("snapshots/%s" % snapshot_id)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000078 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080079 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010080
81 def is_resource_deleted(self, id):
82 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000083 self.show_snapshot(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090084 except lib_exc.NotFound:
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010085 return True
86 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +080087
Matt Riedemannd2b96512014-10-13 10:18:16 -070088 @property
89 def resource_type(self):
90 """Returns the primary type of resource this client works with."""
91 return 'volume-snapshot'
92
zhangyanzid4d3c6d2013-11-06 09:27:13 +080093 def reset_snapshot_status(self, snapshot_id, status):
94 """Reset the specified snapshot's status."""
95 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020096 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000097 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080098 return rest_client.ResponseBody(resp, body)
zhangyanzid4d3c6d2013-11-06 09:27:13 +080099
Ghanshyam0b75b632015-12-11 15:08:28 +0900100 def update_snapshot_status(self, snapshot_id, **kwargs):
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800101 """Update the specified snapshot's status."""
Ghanshyam0b75b632015-12-11 15:08:28 +0900102 # TODO(gmann): api-site doesn't contain doc ref
103 # for this API. After fixing the api-site, we need to
104 # add the link here.
105 # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645
106
107 post_body = json.dumps({'os-update_snapshot_status': kwargs})
guo yunxian6cdf0562016-08-17 16:21:52 +0800108 url = 'snapshots/%s/action' % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200109 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000110 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800111 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800112
113 def create_snapshot_metadata(self, snapshot_id, metadata):
114 """Create metadata for the snapshot."""
115 put_body = json.dumps({'metadata': metadata})
guo yunxian6cdf0562016-08-17 16:21:52 +0800116 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200117 resp, body = self.post(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800118 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000119 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800120 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800121
Daniel Mellado8ea47c22016-09-19 10:36:19 +0000122 def update_snapshot(self, snapshot_id, **kwargs):
123 """Updates a snapshot.
124
Dong Ma127887a2016-10-19 09:09:11 -0700125 For a full list of available parameters, please refer to the official
126 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +0000127 https://developer.openstack.org/api-ref/block-storage/v2/#update-snapshot
Daniel Mellado8ea47c22016-09-19 10:36:19 +0000128 """
129 put_body = json.dumps({'snapshot': kwargs})
130 resp, body = self.put('snapshots/%s' % snapshot_id, put_body)
131 body = json.loads(body)
132 self.expected_success(200, resp.status)
133 return rest_client.ResponseBody(resp, body)
134
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000135 def show_snapshot_metadata(self, snapshot_id):
Lv Fumei62ce7732016-07-08 17:01:27 +0800136 """Get metadata of the snapshot.
137
Dong Ma127887a2016-10-19 09:09:11 -0700138 For a full list of available parameters, please refer to the official
139 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +0000140 https://developer.openstack.org/api-ref/block-storage/v2/#show-snapshot-metadata
Lv Fumei62ce7732016-07-08 17:01:27 +0800141 """
guo yunxian6cdf0562016-08-17 16:21:52 +0800142 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200143 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800144 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000145 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800146 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800147
piyush11078661fd3f02015-12-24 17:09:45 +0530148 def update_snapshot_metadata(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +0800149 """Update metadata for the snapshot.
150
Dong Ma127887a2016-10-19 09:09:11 -0700151 For a full list of available parameters, please refer to the official
152 API reference:
Felipe Monteiro25084512017-11-14 22:07:31 +0000153 https://developer.openstack.org/api-ref/block-storage/v2/#update-snapshot-metadata
zhuflb9255a52016-06-24 10:29:43 +0800154 """
piyush11078661fd3f02015-12-24 17:09:45 +0530155 put_body = json.dumps(kwargs)
guo yunxian6cdf0562016-08-17 16:21:52 +0800156 url = "snapshots/%s/metadata" % snapshot_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200157 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800158 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000159 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800160 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800161
piyush11078661fd3f02015-12-24 17:09:45 +0530162 def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs):
huangtianhua1346d702013-12-09 18:42:35 +0800163 """Update metadata item for the snapshot."""
piyush11078661fd3f02015-12-24 17:09:45 +0530164 # TODO(piyush): Current api-site doesn't contain this API description.
165 # After fixing the api-site, we need to fix here also for putting the
166 # link to api-site.
167 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064
168 put_body = json.dumps(kwargs)
guo yunxian6cdf0562016-08-17 16:21:52 +0800169 url = "snapshots/%s/metadata/%s" % (snapshot_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200170 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800171 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000172 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800173 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800174
175 def delete_snapshot_metadata_item(self, snapshot_id, id):
176 """Delete metadata item for the snapshot."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800177 url = "snapshots/%s/metadata/%s" % (snapshot_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200178 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000179 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800180 return rest_client.ResponseBody(resp, body)
wanghaofa3908c2014-01-15 19:34:03 +0800181
182 def force_delete_snapshot(self, snapshot_id):
183 """Force Delete Snapshot."""
184 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200185 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000186 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800187 return rest_client.ResponseBody(resp, body)