Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 1 | # 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 Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 13 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 14 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 15 | |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 16 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 17 | from tempest.lib import exceptions as lib_exc |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 18 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | |
Daniel Mellado | 8ea47c2 | 2016-09-19 10:36:19 +0000 | [diff] [blame] | 20 | class SnapshotsClient(rest_client.RestClient): |
| 21 | """Client class to send CRUD Volume V2 API requests.""" |
| 22 | api_version = "v2" |
| 23 | create_resp = 202 |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 24 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 25 | def list_snapshots(self, detail=False, **params): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 26 | """List all the snapshot. |
| 27 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 28 | For a full list of available parameters, please refer to the official |
| 29 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 30 | http://developer.openstack.org/api-ref/block-storage/v2/#list-snapshots-v2 |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 31 | """ |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 32 | url = 'snapshots' |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 33 | if detail: |
| 34 | url += '/detail' |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 35 | if params: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 36 | url += '?%s' % urllib.urlencode(params) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 37 | |
| 38 | resp, body = self.get(url) |
| 39 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 40 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 41 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 42 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 43 | def show_snapshot(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 44 | """Returns the details of a single snapshot. |
| 45 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 46 | For a full list of available parameters, please refer to the official |
| 47 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 48 | http://developer.openstack.org/api-ref/block-storage/v2/#show-snapshot-v2 |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 49 | """ |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 50 | url = "snapshots/%s" % snapshot_id |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 51 | resp, body = self.get(url) |
| 52 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 53 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 54 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 55 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 56 | def create_snapshot(self, **kwargs): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 57 | """Creates a new snapshot. |
| 58 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 59 | For a full list of available parameters, please refer to the official |
| 60 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 61 | http://developer.openstack.org/api-ref/block-storage/v2/#create-snapshot |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 62 | """ |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 63 | post_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 64 | resp, body = self.post('snapshots', post_body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 65 | body = json.loads(body) |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 66 | self.expected_success(self.create_resp, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 67 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 68 | |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 69 | def update_snapshot(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 70 | """Updates a snapshot. |
| 71 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 72 | For a full list of available parameters, please refer to the official |
| 73 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 74 | http://developer.openstack.org/api-ref/block-storage/v2/#update-snapshot-v2 |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 75 | """ |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 76 | put_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 77 | resp, body = self.put('snapshots/%s' % snapshot_id, put_body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 78 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 79 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 80 | return rest_client.ResponseBody(resp, body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 81 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 82 | def delete_snapshot(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 83 | """Delete Snapshot. |
| 84 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 85 | For a full list of available parameters, please refer to the official |
| 86 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 87 | http://developer.openstack.org/api-ref/block-storage/v2/#delete-snapshot-v2 |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 88 | """ |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 89 | resp, body = self.delete("snapshots/%s" % snapshot_id) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 90 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 91 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 92 | |
| 93 | def is_resource_deleted(self, id): |
| 94 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 95 | self.show_snapshot(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 96 | except lib_exc.NotFound: |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 97 | return True |
| 98 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 99 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 100 | @property |
| 101 | def resource_type(self): |
| 102 | """Returns the primary type of resource this client works with.""" |
| 103 | return 'volume-snapshot' |
| 104 | |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 105 | 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 108 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 109 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 110 | return rest_client.ResponseBody(resp, body) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 111 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 112 | def update_snapshot_status(self, snapshot_id, **kwargs): |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 113 | """Update the specified snapshot's status.""" |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 114 | # 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 yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 120 | url = 'snapshots/%s/action' % snapshot_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 121 | resp, body = self.post(url, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 122 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 123 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 124 | |
| 125 | def create_snapshot_metadata(self, snapshot_id, metadata): |
| 126 | """Create metadata for the snapshot.""" |
| 127 | put_body = json.dumps({'metadata': metadata}) |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 128 | url = "snapshots/%s/metadata" % snapshot_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 129 | resp, body = self.post(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 130 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 131 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 132 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 133 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 134 | def show_snapshot_metadata(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 135 | """Get metadata of the snapshot. |
| 136 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 137 | For a full list of available parameters, please refer to the official |
| 138 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 139 | http://developer.openstack.org/api-ref/block-storage/v2/#show-snapshot-metadata-v2 |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame] | 140 | """ |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 141 | url = "snapshots/%s/metadata" % snapshot_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 142 | resp, body = self.get(url) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 143 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 144 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 145 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 146 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 147 | def update_snapshot_metadata(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 148 | """Update metadata for the snapshot. |
| 149 | |
Dong Ma | 127887a | 2016-10-19 09:09:11 -0700 | [diff] [blame] | 150 | For a full list of available parameters, please refer to the official |
| 151 | API reference: |
Surya Prakash Singh | 36bea05 | 2016-11-22 14:16:00 +0530 | [diff] [blame] | 152 | http://developer.openstack.org/api-ref/block-storage/v2/#update-snapshot-metadata-v2 |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 153 | """ |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 154 | put_body = json.dumps(kwargs) |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 155 | url = "snapshots/%s/metadata" % snapshot_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 156 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 157 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 158 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 159 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 160 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 161 | def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 162 | """Update metadata item for the snapshot.""" |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 163 | # 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 yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 168 | url = "snapshots/%s/metadata/%s" % (snapshot_id, id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 169 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 170 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 171 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 172 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 173 | |
| 174 | def delete_snapshot_metadata_item(self, snapshot_id, id): |
| 175 | """Delete metadata item for the snapshot.""" |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 176 | url = "snapshots/%s/metadata/%s" % (snapshot_id, id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 177 | resp, body = self.delete(url) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 178 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 179 | return rest_client.ResponseBody(resp, body) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 180 | |
| 181 | def force_delete_snapshot(self, snapshot_id): |
| 182 | """Force Delete Snapshot.""" |
| 183 | post_body = json.dumps({'os-force_delete': {}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 184 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 185 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 186 | return rest_client.ResponseBody(resp, body) |
lkuchlan | cb2f859 | 2016-07-17 15:18:01 +0300 | [diff] [blame] | 187 | |
| 188 | def unmanage_snapshot(self, snapshot_id): |
| 189 | """Unmanage a snapshot.""" |
| 190 | post_body = json.dumps({'os-unmanage': {}}) |
| 191 | url = 'snapshots/%s/action' % (snapshot_id) |
| 192 | resp, body = self.post(url, post_body) |
| 193 | self.expected_success(202, resp.status) |
| 194 | return rest_client.ResponseBody(resp, body) |