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 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 13 | import time |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 14 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 15 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 16 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 18 | from tempest_lib import exceptions as lib_exc |
| 19 | |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 20 | from tempest.common import service_client |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 21 | from tempest import exceptions |
| 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 24 | LOG = logging.getLogger(__name__) |
| 25 | |
| 26 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 27 | class BaseSnapshotsClient(service_client.ServiceClient): |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 28 | """Base Client class to send CRUD Volume API requests.""" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 29 | |
Ken'ichi Ohmichi | a39d0be | 2014-12-17 08:46:11 +0000 | [diff] [blame] | 30 | create_resp = 200 |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 31 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 32 | def list_snapshots(self, detail=False, **params): |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 33 | """List all the snapshot.""" |
| 34 | url = 'snapshots' |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 35 | if detail: |
| 36 | url += '/detail' |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 37 | if params: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 38 | url += '?%s' % urllib.urlencode(params) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 39 | |
| 40 | resp, body = self.get(url) |
| 41 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 42 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 43 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 44 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 45 | def show_snapshot(self, snapshot_id): |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 46 | """Returns the details of a single snapshot.""" |
| 47 | url = "snapshots/%s" % str(snapshot_id) |
| 48 | resp, body = self.get(url) |
| 49 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 50 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 51 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 52 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 53 | def create_snapshot(self, **kwargs): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 54 | """Creates a new snapshot. |
| 55 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 56 | Available params: see http://developer.openstack.org/ |
| 57 | api-ref-blockstorage-v2.html#createSnapshot |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 58 | """ |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 59 | post_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 60 | resp, body = self.post('snapshots', post_body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 61 | body = json.loads(body) |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 62 | self.expected_success(self.create_resp, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 63 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 64 | |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 65 | def update_snapshot(self, snapshot_id, **kwargs): |
| 66 | """Updates a snapshot.""" |
| 67 | put_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 68 | resp, body = self.put('snapshots/%s' % snapshot_id, put_body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 69 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 70 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 71 | return service_client.ResponseBody(resp, body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 72 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 73 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 74 | def _get_snapshot_status(self, snapshot_id): |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 75 | body = self.show_snapshot(snapshot_id)['snapshot'] |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 76 | status = body['status'] |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 77 | # NOTE(afazekas): snapshot can reach an "error" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 78 | # state in a "normal" lifecycle |
| 79 | if (status == 'error'): |
| 80 | raise exceptions.SnapshotBuildErrorException( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 81 | snapshot_id=snapshot_id) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 82 | |
| 83 | return status |
| 84 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 85 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 86 | def wait_for_snapshot_status(self, snapshot_id, status): |
| 87 | """Waits for a Snapshot to reach a given status.""" |
| 88 | start_time = time.time() |
| 89 | old_value = value = self._get_snapshot_status(snapshot_id) |
| 90 | while True: |
| 91 | dtime = time.time() - start_time |
| 92 | time.sleep(self.build_interval) |
| 93 | if value != old_value: |
| 94 | LOG.info('Value transition from "%s" to "%s"' |
| 95 | 'in %d second(s).', old_value, |
| 96 | value, dtime) |
| 97 | if (value == status): |
| 98 | return value |
| 99 | |
| 100 | if dtime > self.build_timeout: |
| 101 | message = ('Time Limit Exceeded! (%ds)' |
| 102 | 'while waiting for %s, ' |
| 103 | 'but we got %s.' % |
| 104 | (self.build_timeout, status, value)) |
| 105 | raise exceptions.TimeoutException(message) |
| 106 | time.sleep(self.build_interval) |
| 107 | old_value = value |
| 108 | value = self._get_snapshot_status(snapshot_id) |
| 109 | |
| 110 | def delete_snapshot(self, snapshot_id): |
| 111 | """Delete Snapshot.""" |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 112 | resp, body = self.delete("snapshots/%s" % str(snapshot_id)) |
| 113 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 114 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 115 | |
| 116 | def is_resource_deleted(self, id): |
| 117 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 118 | self.show_snapshot(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 119 | except lib_exc.NotFound: |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 120 | return True |
| 121 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 122 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 123 | @property |
| 124 | def resource_type(self): |
| 125 | """Returns the primary type of resource this client works with.""" |
| 126 | return 'volume-snapshot' |
| 127 | |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 128 | def reset_snapshot_status(self, snapshot_id, status): |
| 129 | """Reset the specified snapshot's status.""" |
| 130 | post_body = json.dumps({'os-reset_status': {"status": status}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 131 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 132 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 133 | return service_client.ResponseBody(resp, body) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 134 | |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 135 | def update_snapshot_status(self, snapshot_id, **kwargs): |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 136 | """Update the specified snapshot's status.""" |
Ghanshyam | 0b75b63 | 2015-12-11 15:08:28 +0900 | [diff] [blame] | 137 | # TODO(gmann): api-site doesn't contain doc ref |
| 138 | # for this API. After fixing the api-site, we need to |
| 139 | # add the link here. |
| 140 | # Bug https://bugs.launchpad.net/openstack-api-site/+bug/1532645 |
| 141 | |
| 142 | post_body = json.dumps({'os-update_snapshot_status': kwargs}) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 143 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 144 | resp, body = self.post(url, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 145 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 146 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 147 | |
| 148 | def create_snapshot_metadata(self, snapshot_id, metadata): |
| 149 | """Create metadata for the snapshot.""" |
| 150 | put_body = json.dumps({'metadata': metadata}) |
| 151 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 152 | resp, body = self.post(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 153 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 154 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 155 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 156 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 157 | def show_snapshot_metadata(self, snapshot_id): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 158 | """Get metadata of the snapshot.""" |
| 159 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 160 | resp, body = self.get(url) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 161 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 162 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 163 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 164 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 165 | def update_snapshot_metadata(self, snapshot_id, **kwargs): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 166 | """Update metadata for the snapshot.""" |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 167 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 168 | # After fixing the api-site, we need to fix here also for putting the |
| 169 | # link to api-site. |
| 170 | # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529063 |
| 171 | put_body = json.dumps(kwargs) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 172 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 173 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 174 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 175 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 176 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 177 | |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 178 | def update_snapshot_metadata_item(self, snapshot_id, id, **kwargs): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 179 | """Update metadata item for the snapshot.""" |
piyush110786 | 61fd3f0 | 2015-12-24 17:09:45 +0530 | [diff] [blame] | 180 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 181 | # After fixing the api-site, we need to fix here also for putting the |
| 182 | # link to api-site. |
| 183 | # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1529064 |
| 184 | put_body = json.dumps(kwargs) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 185 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 186 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 187 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 188 | self.expected_success(200, resp.status) |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 189 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 190 | |
| 191 | def delete_snapshot_metadata_item(self, snapshot_id, id): |
| 192 | """Delete metadata item for the snapshot.""" |
| 193 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 194 | resp, body = self.delete(url) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 195 | self.expected_success(200, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 196 | return service_client.ResponseBody(resp, body) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 197 | |
| 198 | def force_delete_snapshot(self, snapshot_id): |
| 199 | """Force Delete Snapshot.""" |
| 200 | post_body = json.dumps({'os-force_delete': {}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 201 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 202 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 203 | return service_client.ResponseBody(resp, body) |