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