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