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 |
| 14 | import urllib |
| 15 | |
| 16 | from lxml import etree |
| 17 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 18 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 20 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 21 | from tempest.openstack.common import log as logging |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 22 | from tempest.services.compute.xml.common import Document |
| 23 | from tempest.services.compute.xml.common import Element |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 24 | from tempest.services.compute.xml.common import Text |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import xml_to_json |
| 26 | from tempest.services.compute.xml.common import XMLNS_11 |
| 27 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | CONF = config.CONF |
| 29 | |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 30 | LOG = logging.getLogger(__name__) |
| 31 | |
| 32 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 33 | class SnapshotsClientXML(rest_client.RestClient): |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 34 | """Client class to send CRUD Volume API requests.""" |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 35 | TYPE = "xml" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 36 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 37 | def __init__(self, auth_provider): |
| 38 | super(SnapshotsClientXML, self).__init__(auth_provider) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 39 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 40 | self.service = CONF.volume.catalog_type |
| 41 | self.build_interval = CONF.volume.build_interval |
| 42 | self.build_timeout = CONF.volume.build_timeout |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 43 | |
| 44 | def list_snapshots(self, params=None): |
| 45 | """List all snapshot.""" |
| 46 | url = 'snapshots' |
| 47 | |
| 48 | if params: |
| 49 | url += '?%s' % urllib.urlencode(params) |
| 50 | |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 51 | resp, body = self.get(url) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 52 | body = etree.fromstring(body) |
Giulio Fidente | e92956b | 2013-06-06 18:38:48 +0200 | [diff] [blame] | 53 | snapshots = [] |
| 54 | for snap in body: |
| 55 | snapshots.append(xml_to_json(snap)) |
| 56 | return resp, snapshots |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 57 | |
| 58 | def list_snapshots_with_detail(self, params=None): |
| 59 | """List all the details of snapshot.""" |
| 60 | url = 'snapshots/detail' |
| 61 | |
| 62 | if params: |
| 63 | url += '?%s' % urllib.urlencode(params) |
| 64 | |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 65 | resp, body = self.get(url) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 66 | body = etree.fromstring(body) |
| 67 | snapshots = [] |
Giulio Fidente | e92956b | 2013-06-06 18:38:48 +0200 | [diff] [blame] | 68 | for snap in body: |
| 69 | snapshots.append(xml_to_json(snap)) |
| 70 | return resp, snapshots |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 71 | |
| 72 | def get_snapshot(self, snapshot_id): |
| 73 | """Returns the details of a single snapshot.""" |
| 74 | url = "snapshots/%s" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 75 | resp, body = self.get(url) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 76 | body = etree.fromstring(body) |
| 77 | return resp, xml_to_json(body) |
| 78 | |
| 79 | def create_snapshot(self, volume_id, **kwargs): |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 80 | """Creates a new snapshot. |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 81 | volume_id(Required): id of the volume. |
| 82 | force: Create a snapshot even if the volume attached (Default=False) |
| 83 | display_name: Optional snapshot Name. |
| 84 | display_description: User friendly snapshot description. |
| 85 | """ |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 86 | # NOTE(afazekas): it should use the volume namespace |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 87 | snapshot = Element("snapshot", xmlns=XMLNS_11, volume_id=volume_id) |
| 88 | for key, value in kwargs.items(): |
| 89 | snapshot.add_attr(key, value) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 90 | resp, body = self.post('snapshots', str(Document(snapshot))) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 91 | body = xml_to_json(etree.fromstring(body)) |
| 92 | return resp, body |
| 93 | |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 94 | def update_snapshot(self, snapshot_id, **kwargs): |
| 95 | """Updates a snapshot.""" |
| 96 | put_body = Element("snapshot", xmlns=XMLNS_11, **kwargs) |
| 97 | |
| 98 | resp, body = self.put('snapshots/%s' % snapshot_id, |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 99 | str(Document(put_body))) |
QingXin Meng | dc95f5e | 2013-09-16 19:06:44 -0700 | [diff] [blame] | 100 | body = xml_to_json(etree.fromstring(body)) |
| 101 | return resp, body |
| 102 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 103 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 104 | def _get_snapshot_status(self, snapshot_id): |
| 105 | resp, body = self.get_snapshot(snapshot_id) |
| 106 | status = body['status'] |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 107 | # NOTE(afazekas): snapshot can reach an "error" |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 108 | # state in a "normal" lifecycle |
| 109 | if (status == 'error'): |
| 110 | raise exceptions.SnapshotBuildErrorException( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 111 | snapshot_id=snapshot_id) |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 112 | |
| 113 | return status |
| 114 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 115 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 116 | def wait_for_snapshot_status(self, snapshot_id, status): |
| 117 | """Waits for a Snapshot to reach a given status.""" |
| 118 | start_time = time.time() |
| 119 | old_value = value = self._get_snapshot_status(snapshot_id) |
| 120 | while True: |
| 121 | dtime = time.time() - start_time |
| 122 | time.sleep(self.build_interval) |
| 123 | if value != old_value: |
| 124 | LOG.info('Value transition from "%s" to "%s"' |
| 125 | 'in %d second(s).', old_value, |
| 126 | value, dtime) |
| 127 | if (value == status): |
| 128 | return value |
| 129 | |
| 130 | if dtime > self.build_timeout: |
| 131 | message = ('Time Limit Exceeded! (%ds)' |
| 132 | 'while waiting for %s, ' |
| 133 | 'but we got %s.' % |
| 134 | (self.build_timeout, status, value)) |
| 135 | raise exceptions.TimeoutException(message) |
| 136 | time.sleep(self.build_interval) |
| 137 | old_value = value |
| 138 | value = self._get_snapshot_status(snapshot_id) |
| 139 | |
| 140 | def delete_snapshot(self, snapshot_id): |
| 141 | """Delete Snapshot.""" |
| 142 | return self.delete("snapshots/%s" % str(snapshot_id)) |
| 143 | |
| 144 | def is_resource_deleted(self, id): |
| 145 | try: |
| 146 | self.get_snapshot(id) |
| 147 | except exceptions.NotFound: |
| 148 | return True |
| 149 | return False |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 150 | |
| 151 | def reset_snapshot_status(self, snapshot_id, status): |
| 152 | """Reset the specified snapshot's status.""" |
| 153 | post_body = Element("os-reset_status", |
| 154 | status=status |
| 155 | ) |
| 156 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 157 | resp, body = self.post(url, str(Document(post_body))) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 158 | if body: |
| 159 | body = xml_to_json(etree.fromstring(body)) |
| 160 | return resp, body |
| 161 | |
| 162 | def update_snapshot_status(self, snapshot_id, status, progress): |
| 163 | """Update the specified snapshot's status.""" |
| 164 | post_body = Element("os-update_snapshot_status", |
| 165 | status=status, |
| 166 | progress=progress |
| 167 | ) |
| 168 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 169 | resp, body = self.post(url, str(Document(post_body))) |
zhangyanzi | d4d3c6d | 2013-11-06 09:27:13 +0800 | [diff] [blame] | 170 | if body: |
| 171 | body = xml_to_json(etree.fromstring(body)) |
| 172 | return resp, body |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 173 | |
| 174 | def _metadata_body(self, meta): |
| 175 | post_body = Element('metadata') |
| 176 | for k, v in meta.items(): |
| 177 | data = Element('meta', key=k) |
| 178 | data.append(Text(v)) |
| 179 | post_body.append(data) |
| 180 | return post_body |
| 181 | |
| 182 | def _parse_key_value(self, node): |
| 183 | """Parse <foo key='key'>value</foo> data into {'key': 'value'}.""" |
| 184 | data = {} |
| 185 | for node in node.getchildren(): |
| 186 | data[node.get('key')] = node.text |
| 187 | return data |
| 188 | |
| 189 | def create_snapshot_metadata(self, snapshot_id, metadata): |
| 190 | """Create metadata for the snapshot.""" |
| 191 | post_body = self._metadata_body(metadata) |
| 192 | resp, body = self.post('snapshots/%s/metadata' % snapshot_id, |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 193 | str(Document(post_body))) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 194 | body = self._parse_key_value(etree.fromstring(body)) |
| 195 | return resp, body |
| 196 | |
| 197 | def get_snapshot_metadata(self, snapshot_id): |
| 198 | """Get metadata of the snapshot.""" |
| 199 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 200 | resp, body = self.get(url) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 201 | body = self._parse_key_value(etree.fromstring(body)) |
| 202 | return resp, body |
| 203 | |
| 204 | def update_snapshot_metadata(self, snapshot_id, metadata): |
| 205 | """Update metadata for the snapshot.""" |
| 206 | put_body = self._metadata_body(metadata) |
| 207 | url = "snapshots/%s/metadata" % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 208 | resp, body = self.put(url, str(Document(put_body))) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 209 | body = self._parse_key_value(etree.fromstring(body)) |
| 210 | return resp, body |
| 211 | |
| 212 | def update_snapshot_metadata_item(self, snapshot_id, id, meta_item): |
| 213 | """Update metadata item for the snapshot.""" |
| 214 | for k, v in meta_item.items(): |
| 215 | put_body = Element('meta', key=k) |
| 216 | put_body.append(Text(v)) |
| 217 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 218 | resp, body = self.put(url, str(Document(put_body))) |
huangtianhua | 1346d70 | 2013-12-09 18:42:35 +0800 | [diff] [blame] | 219 | body = xml_to_json(etree.fromstring(body)) |
| 220 | return resp, body |
| 221 | |
| 222 | def delete_snapshot_metadata_item(self, snapshot_id, id): |
| 223 | """Delete metadata item for the snapshot.""" |
| 224 | url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) |
| 225 | return self.delete(url) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 226 | |
| 227 | def force_delete_snapshot(self, snapshot_id): |
| 228 | """Force Delete Snapshot.""" |
| 229 | post_body = Element("os-force_delete") |
| 230 | url = 'snapshots/%s/action' % str(snapshot_id) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 231 | resp, body = self.post(url, str(Document(post_body))) |
wanghao | fa3908c | 2014-01-15 19:34:03 +0800 | [diff] [blame] | 232 | if body: |
| 233 | body = xml_to_json(etree.fromstring(body)) |
| 234 | return resp, body |