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