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 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 13 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 14 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 15 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 16 | |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 17 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 18 | from tempest.lib import exceptions as lib_exc |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 19 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 24 | class BaseSnapshotsClient(rest_client.RestClient): |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 25 | """Base Client class to send CRUD Volume API requests.""" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 26 | |
Ken'ichi Ohmichi | a39d0be | 2014-12-17 08:46:11 +0000 | [diff] [blame] | 27 | create_resp = 200 |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 28 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 29 | def list_snapshots(self, detail=False, **params): |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 30 | """List all the snapshot.""" |
| 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): |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 43 | """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 Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 47 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 48 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 49 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 50 | def create_snapshot(self, **kwargs): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 51 | """Creates a new snapshot. |
| 52 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 53 | Available params: see http://developer.openstack.org/ |
| 54 | api-ref-blockstorage-v2.html#createSnapshot |
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 | post_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 57 | resp, body = self.post('snapshots', post_body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 58 | body = json.loads(body) |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 59 | self.expected_success(self.create_resp, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 60 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 61 | |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 62 | def update_snapshot(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame^] | 63 | """Updates a snapshot. |
| 64 | |
| 65 | Available params: see http://developer.openstack.org/ |
| 66 | api-ref-blockstorage-v2.html#updateSnapshot |
| 67 | """ |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 68 | put_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 69 | resp, body = self.put('snapshots/%s' % snapshot_id, put_body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 70 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 71 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 72 | return rest_client.ResponseBody(resp, body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 73 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 74 | def delete_snapshot(self, snapshot_id): |
| 75 | """Delete Snapshot.""" |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 76 | resp, body = self.delete("snapshots/%s" % str(snapshot_id)) |
| 77 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 78 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 79 | |
| 80 | def is_resource_deleted(self, id): |
| 81 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 82 | self.show_snapshot(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 83 | except lib_exc.NotFound: |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 84 | return True |
| 85 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 86 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 87 | @property |
| 88 | def resource_type(self): |
| 89 | """Returns the primary type of resource this client works with.""" |
| 90 | return 'volume-snapshot' |
| 91 | |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 92 | 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 95 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 96 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 97 | return rest_client.ResponseBody(resp, body) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 98 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 99 | def update_snapshot_status(self, snapshot_id, **kwargs): |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 100 | """Update the specified snapshot's status.""" |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 101 | # 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}) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 107 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 108 | resp, body = self.post(url, 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) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 111 | |
| 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 116 | resp, body = self.post(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 117 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 118 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 119 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 120 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 121 | def show_snapshot_metadata(self, snapshot_id): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 122 | """Get metadata of the snapshot.""" |
| 123 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 124 | resp, body = self.get(url) |
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 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 129 | def update_snapshot_metadata(self, snapshot_id, **kwargs): |
zhufl | b9255a5 | 2016-06-24 10:29:43 +0800 | [diff] [blame^] | 130 | """Update metadata for the snapshot. |
| 131 | |
| 132 | Available params: see http://developer.openstack.org/ |
| 133 | api-ref-blockstorage-v2.html#updateSnapshotMetadata |
| 134 | """ |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 135 | put_body = json.dumps(kwargs) |
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.put(url, put_body) |
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_item(self, snapshot_id, id, **kwargs): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 143 | """Update metadata item for the snapshot.""" |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 144 | # 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) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 149 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 150 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 151 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 152 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 153 | return rest_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 154 | |
| 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 158 | resp, body = self.delete(url) |
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) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 161 | |
| 162 | def force_delete_snapshot(self, snapshot_id): |
| 163 | """Force Delete Snapshot.""" |
| 164 | post_body = json.dumps({'os-force_delete': {}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 165 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 166 | self.expected_success(202, resp.status) |
Ken'ichi Ohmichi | 90d446a | 2016-03-02 10:17:38 -0800 | [diff] [blame] | 167 | return rest_client.ResponseBody(resp, body) |