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