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 | |
| 13 | import json |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 14 | import time |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 15 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 16 | from oslo_log import log as logging |
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 | f85e9bd | 2015-01-27 12:51:47 +0000 | [diff] [blame] | 27 | class BaseSnapshotsClientJSON(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 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 32 | def list_snapshots(self, detail=False, params=None): |
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) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 43 | return service_client.ResponseBodyList(resp, body['snapshots']) |
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) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 51 | return service_client.ResponseBody(resp, body['snapshot']) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 52 | |
| 53 | def create_snapshot(self, volume_id, **kwargs): |
| 54 | """ |
| 55 | Creates a new snapshot. |
| 56 | volume_id(Required): id of the volume. |
| 57 | force: Create a snapshot even if the volume attached (Default=False) |
| 58 | display_name: Optional snapshot Name. |
| 59 | display_description: User friendly snapshot description. |
| 60 | """ |
| 61 | post_body = {'volume_id': volume_id} |
| 62 | post_body.update(kwargs) |
| 63 | post_body = json.dumps({'snapshot': post_body}) |
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) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 67 | return service_client.ResponseBody(resp, body['snapshot']) |
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): |
| 70 | """Updates a snapshot.""" |
| 71 | put_body = json.dumps({'snapshot': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 72 | resp, body = self.put('snapshots/%s' % snapshot_id, put_body) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 73 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 74 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 75 | return service_client.ResponseBody(resp, body['snapshot']) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 76 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 77 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 78 | def _get_snapshot_status(self, snapshot_id): |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 79 | body = self.show_snapshot(snapshot_id) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 80 | status = body['status'] |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 81 | # NOTE(afazekas): snapshot can reach an "error" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 82 | # state in a "normal" lifecycle |
| 83 | if (status == 'error'): |
| 84 | raise exceptions.SnapshotBuildErrorException( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 85 | snapshot_id=snapshot_id) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 86 | |
| 87 | return status |
| 88 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 89 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 90 | def wait_for_snapshot_status(self, snapshot_id, status): |
| 91 | """Waits for a Snapshot to reach a given status.""" |
| 92 | start_time = time.time() |
| 93 | old_value = value = self._get_snapshot_status(snapshot_id) |
| 94 | while True: |
| 95 | dtime = time.time() - start_time |
| 96 | time.sleep(self.build_interval) |
| 97 | if value != old_value: |
| 98 | LOG.info('Value transition from "%s" to "%s"' |
| 99 | 'in %d second(s).', old_value, |
| 100 | value, dtime) |
| 101 | if (value == status): |
| 102 | return value |
| 103 | |
| 104 | if dtime > self.build_timeout: |
| 105 | message = ('Time Limit Exceeded! (%ds)' |
| 106 | 'while waiting for %s, ' |
| 107 | 'but we got %s.' % |
| 108 | (self.build_timeout, status, value)) |
| 109 | raise exceptions.TimeoutException(message) |
| 110 | time.sleep(self.build_interval) |
| 111 | old_value = value |
| 112 | value = self._get_snapshot_status(snapshot_id) |
| 113 | |
| 114 | def delete_snapshot(self, snapshot_id): |
| 115 | """Delete Snapshot.""" |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 116 | resp, body = self.delete("snapshots/%s" % str(snapshot_id)) |
| 117 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 118 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 119 | |
| 120 | def is_resource_deleted(self, id): |
| 121 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 122 | self.show_snapshot(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 123 | except lib_exc.NotFound: |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 124 | return True |
| 125 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 126 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 127 | @property |
| 128 | def resource_type(self): |
| 129 | """Returns the primary type of resource this client works with.""" |
| 130 | return 'volume-snapshot' |
| 131 | |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 132 | def reset_snapshot_status(self, snapshot_id, status): |
| 133 | """Reset the specified snapshot's status.""" |
| 134 | post_body = json.dumps({'os-reset_status': {"status": status}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 135 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 136 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 137 | return service_client.ResponseBody(resp, body) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 138 | |
| 139 | def update_snapshot_status(self, snapshot_id, status, progress): |
| 140 | """Update the specified snapshot's status.""" |
| 141 | post_body = { |
| 142 | 'status': status, |
| 143 | 'progress': progress |
| 144 | } |
| 145 | post_body = json.dumps({'os-update_snapshot_status': post_body}) |
| 146 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 147 | resp, body = self.post(url, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 148 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 149 | return service_client.ResponseBody(resp, body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 150 | |
| 151 | def create_snapshot_metadata(self, snapshot_id, metadata): |
| 152 | """Create metadata for the snapshot.""" |
| 153 | put_body = json.dumps({'metadata': metadata}) |
| 154 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 155 | resp, body = self.post(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 156 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 157 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 158 | return service_client.ResponseBody(resp, body['metadata']) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 159 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 160 | def show_snapshot_metadata(self, snapshot_id): |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 161 | """Get metadata of the snapshot.""" |
| 162 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 163 | resp, body = self.get(url) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 164 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 165 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 166 | return service_client.ResponseBody(resp, body['metadata']) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 167 | |
| 168 | def update_snapshot_metadata(self, snapshot_id, metadata): |
| 169 | """Update metadata for the snapshot.""" |
| 170 | put_body = json.dumps({'metadata': metadata}) |
| 171 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 172 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 173 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 174 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 175 | return service_client.ResponseBody(resp, body['metadata']) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 176 | |
| 177 | def update_snapshot_metadata_item(self, snapshot_id, id, meta_item): |
| 178 | """Update metadata item for the snapshot.""" |
| 179 | put_body = json.dumps({'meta': meta_item}) |
| 180 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 181 | resp, body = self.put(url, put_body) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 182 | body = json.loads(body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 183 | self.expected_success(200, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 184 | return service_client.ResponseBody(resp, body['meta']) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 185 | |
| 186 | def delete_snapshot_metadata_item(self, snapshot_id, id): |
| 187 | """Delete metadata item for the snapshot.""" |
| 188 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 189 | resp, body = self.delete(url) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 190 | self.expected_success(200, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 191 | return service_client.ResponseBody(resp, body) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 192 | |
| 193 | def force_delete_snapshot(self, snapshot_id): |
| 194 | """Force Delete Snapshot.""" |
| 195 | post_body = json.dumps({'os-force_delete': {}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 196 | resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body) |
Swapnil Kulkarni | d9df38c | 2014-08-16 18:06:52 +0000 | [diff] [blame] | 197 | self.expected_success(202, resp.status) |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 198 | return service_client.ResponseBody(resp, body) |
Zhi Kun Liu | 38641c6 | 2014-07-10 20:12:48 +0800 | [diff] [blame] | 199 | |
| 200 | |
| 201 | class SnapshotsClientJSON(BaseSnapshotsClientJSON): |
| 202 | """Client class to send CRUD Volume V1 API requests.""" |