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 | |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 20 | class BaseSnapshotsClient(rest_client.RestClient): |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 21 | """Base Client class to send CRUD Volume 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 | |
| 28 | Available params: see http://developer.openstack.org/ |
| 29 | api-ref-blockstorage-v2.html#listSnapshots |
| 30 | """ |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 31 | url = 'snapshots' |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 32 | if detail: |
| 33 | url += '/detail' |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 34 | if params: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 35 | url += '?%s' % urllib.urlencode(params) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 36 | |
| 37 | resp, body = self.get(url) |
| 38 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 39 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 40 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 41 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 42 | def show_snapshot(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame^] | 43 | """Returns the details of a single snapshot. |
| 44 | |
| 45 | Available params: see http://developer.openstack.org/ |
| 46 | api-ref-blockstorage-v2.html#showSnapshot |
| 47 | """ |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 48 | url = "snapshots/%s" % str(snapshot_id) |
| 49 | resp, body = self.get(url) |
| 50 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 51 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 52 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 53 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 54 | def create_snapshot(self, **kwargs): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 55 | """Creates a new snapshot. |
| 56 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 57 | Available params: see http://developer.openstack.org/ |
| 58 | api-ref-blockstorage-v2.html#createSnapshot |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 59 | """ |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 60 | post_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 61 | resp, body = self.post('snapshots', post_body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 62 | body = json.loads(body) |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 63 | self.expected_success(self.create_resp, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 64 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 65 | |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 66 | def update_snapshot(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 67 | """Updates a snapshot. |
| 68 | |
| 69 | Available params: see http://developer.openstack.org/ |
| 70 | api-ref-blockstorage-v2.html#updateSnapshot |
| 71 | """ |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 72 | put_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 73 | resp, body = self.put('snapshots/%s' % snapshot_id, put_body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 74 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 75 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 76 | return rest_client.ResponseBody(resp, body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 77 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 78 | def delete_snapshot(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame^] | 79 | """Delete Snapshot. |
| 80 | |
| 81 | Available params: see http://developer.openstack.org/ |
| 82 | api-ref-blockstorage-v2.html#deleteSnapshot |
| 83 | """ |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 84 | resp, body = self.delete("snapshots/%s" % str(snapshot_id)) |
| 85 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 86 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 87 | |
| 88 | def is_resource_deleted(self, id): |
| 89 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 90 | self.show_snapshot(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 91 | except lib_exc.NotFound: |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 92 | return True |
| 93 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 94 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 95 | @property |
| 96 | def resource_type(self): |
| 97 | """Returns the primary type of resource this client works with.""" |
| 98 | return 'volume-snapshot' |
| 99 | |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 100 | def reset_snapshot_status(self, snapshot_id, status): |
| 101 | """Reset the specified snapshot's status.""" |
| 102 | post_body = json.dumps({'os-reset_status': {"status": status}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 103 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 104 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 105 | return rest_client.ResponseBody(resp, body) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 106 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 107 | def update_snapshot_status(self, snapshot_id, **kwargs): |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 108 | """Update the specified snapshot's status.""" |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 109 | # TODO(gmann): api-site doesn't contain doc ref |
| 110 | # for this API. After fixing the api-site, we need to |
| 111 | # add the link here. |
| 112 | # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645 |
| 113 | |
| 114 | post_body = json.dumps({'os-update_snapshot_status': kwargs}) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 115 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 116 | resp, body = self.post(url, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 117 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 118 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 119 | |
| 120 | def create_snapshot_metadata(self, snapshot_id, metadata): |
| 121 | """Create metadata for the snapshot.""" |
| 122 | put_body = json.dumps({'metadata': metadata}) |
| 123 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 124 | resp, body = self.post(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 125 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 126 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 127 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 128 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 129 | def show_snapshot_metadata(self, snapshot_id): |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame^] | 130 | """Get metadata of the snapshot. |
| 131 | |
| 132 | Available params: see http://developer.openstack.org/ |
| 133 | api-ref-blockstorage-v2.html# |
| 134 | showSnapshotMetadata |
| 135 | """ |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 136 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 137 | resp, body = self.get(url) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 138 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 139 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 140 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 141 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 142 | def update_snapshot_metadata(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 143 | """Update metadata for the snapshot. |
| 144 | |
| 145 | Available params: see http://developer.openstack.org/ |
Lv Fumei | 62ce773 | 2016-07-08 17:01:27 +0800 | [diff] [blame^] | 146 | api-ref-blockstorage-v2.html# |
| 147 | updateSnapshotMetadata |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame] | 148 | """ |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 149 | put_body = json.dumps(kwargs) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 150 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 151 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 152 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 153 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 154 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 155 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 156 | def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 157 | """Update metadata item for the snapshot.""" |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 158 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 159 | # After fixing the api-site, we need to fix here also for putting the |
| 160 | # link to api-site. |
| 161 | # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064 |
| 162 | put_body = json.dumps(kwargs) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 163 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 164 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 165 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 166 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 167 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 168 | |
| 169 | def delete_snapshot_metadata_item(self, snapshot_id, id): |
| 170 | """Delete metadata item for the snapshot.""" |
| 171 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 172 | resp, body = self.delete(url) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 173 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 174 | return rest_client.ResponseBody(resp, body) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 175 | |
| 176 | def force_delete_snapshot(self, snapshot_id): |
| 177 | """Force Delete Snapshot.""" |
| 178 | post_body = json.dumps({'os-force_delete': {}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 179 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 180 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 181 | return rest_client.ResponseBody(resp, body) |