blob: da7bb01e735f76c077f3067ad549458adc2d5e74 [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000013from oslo_log import log as logging
Matthew Treinish21905512015-07-13 10:33:35 -040014from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040015from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090016
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080017from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050018from tempest.lib import exceptions as lib_exc
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010019
Matthew Treinish684d8992014-01-30 16:27:40 +000020
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010021LOG = logging.getLogger(__name__)
22
23
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080024class BaseSnapshotsClient(rest_client.RestClient):
Zhi Kun Liu38641c62014-07-10 20:12:48 +080025 """Base Client class to send CRUD Volume API requests."""
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010026
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000027 create_resp = 200
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010028
Ghanshyam0b75b632015-12-11 15:08:28 +090029 def list_snapshots(self, detail=False, **params):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010030 """List all the snapshot."""
31 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):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010043 """Returns the details of a single snapshot."""
44 url = "snapshots/%s" % str(snapshot_id)
45 resp, body = self.get(url)
46 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000047 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080048 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010049
Ghanshyam0b75b632015-12-11 15:08:28 +090050 def create_snapshot(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000051 """Creates a new snapshot.
52
Ghanshyam0b75b632015-12-11 15:08:28 +090053 Available params: see http://developer.openstack.org/
54 api-ref-blockstorage-v2.html#createSnapshot
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010055 """
Ghanshyam0b75b632015-12-11 15:08:28 +090056 post_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020057 resp, body = self.post('snapshots', post_body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010058 body = json.loads(body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +080059 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080060 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010061
QingXin Mengdc95f5e2013-09-16 19:06:44 -070062 def update_snapshot(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +080063 """Updates a snapshot.
64
65 Available params: see http://developer.openstack.org/
66 api-ref-blockstorage-v2.html#updateSnapshot
67 """
QingXin Mengdc95f5e2013-09-16 19:06:44 -070068 put_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020069 resp, body = self.put('snapshots/%s' % snapshot_id, put_body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070070 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000071 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080072 return rest_client.ResponseBody(resp, body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070073
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010074 def delete_snapshot(self, snapshot_id):
75 """Delete Snapshot."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000076 resp, body = self.delete("snapshots/%s" % str(snapshot_id))
77 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080078 return rest_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010079
80 def is_resource_deleted(self, id):
81 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000082 self.show_snapshot(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090083 except lib_exc.NotFound:
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010084 return True
85 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +080086
Matt Riedemannd2b96512014-10-13 10:18:16 -070087 @property
88 def resource_type(self):
89 """Returns the primary type of resource this client works with."""
90 return 'volume-snapshot'
91
zhangyanzid4d3c6d2013-11-06 09:27:13 +080092 def reset_snapshot_status(self, snapshot_id, status):
93 """Reset the specified snapshot's status."""
94 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020095 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000096 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080097 return rest_client.ResponseBody(resp, body)
zhangyanzid4d3c6d2013-11-06 09:27:13 +080098
Ghanshyam0b75b632015-12-11 15:08:28 +090099 def update_snapshot_status(self, snapshot_id, **kwargs):
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800100 """Update the specified snapshot's status."""
Ghanshyam0b75b632015-12-11 15:08:28 +0900101 # TODO(gmann): api-site doesn't contain doc ref
102 # for this API. After fixing the api-site, we need to
103 # add the link here.
104 # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645
105
106 post_body = json.dumps({'os-update_snapshot_status': kwargs})
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800107 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200108 resp, body = self.post(url, 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)
huangtianhua1346d702013-12-09 18:42:35 +0800111
112 def create_snapshot_metadata(self, snapshot_id, metadata):
113 """Create metadata for the snapshot."""
114 put_body = json.dumps({'metadata': metadata})
115 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200116 resp, body = self.post(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800117 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000118 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800119 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800120
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000121 def show_snapshot_metadata(self, snapshot_id):
huangtianhua1346d702013-12-09 18:42:35 +0800122 """Get metadata of the snapshot."""
123 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200124 resp, body = self.get(url)
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
piyush11078661fd3f02015-12-24 17:09:45 +0530129 def update_snapshot_metadata(self, snapshot_id, **kwargs):
zhuflb9255a52016-06-24 10:29:43 +0800130 """Update metadata for the snapshot.
131
132 Available params: see http://developer.openstack.org/
133 api-ref-blockstorage-v2.html#updateSnapshotMetadata
134 """
piyush11078661fd3f02015-12-24 17:09:45 +0530135 put_body = json.dumps(kwargs)
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.put(url, put_body)
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_item(self, snapshot_id, id, **kwargs):
huangtianhua1346d702013-12-09 18:42:35 +0800143 """Update metadata item for the snapshot."""
piyush11078661fd3f02015-12-24 17:09:45 +0530144 # TODO(piyush): Current api-site doesn't contain this API description.
145 # After fixing the api-site, we need to fix here also for putting the
146 # link to api-site.
147 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064
148 put_body = json.dumps(kwargs)
huangtianhua1346d702013-12-09 18:42:35 +0800149 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200150 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800151 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000152 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800153 return rest_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800154
155 def delete_snapshot_metadata_item(self, snapshot_id, id):
156 """Delete metadata item for the snapshot."""
157 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200158 resp, body = self.delete(url)
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)
wanghaofa3908c2014-01-15 19:34:03 +0800161
162 def force_delete_snapshot(self, snapshot_id):
163 """Force Delete Snapshot."""
164 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200165 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000166 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800167 return rest_client.ResponseBody(resp, body)