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