Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2012 IBM Corp. |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 16 | import urllib |
| 17 | |
| 18 | from lxml import etree |
| 19 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 21 | from tempest.common import waiters |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 22 | from tempest.common import xml_utils |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | from tempest import config |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | from tempest import exceptions |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | CONF = config.CONF |
| 27 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 28 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 29 | class ImagesClientXML(rest_client.RestClient): |
| 30 | TYPE = "xml" |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 31 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 32 | def __init__(self, auth_provider): |
| 33 | super(ImagesClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 34 | self.service = CONF.compute.catalog_type |
| 35 | self.build_interval = CONF.compute.build_interval |
| 36 | self.build_timeout = CONF.compute.build_timeout |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 37 | |
| 38 | def _parse_server(self, node): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 39 | data = xml_utils.xml_to_json(node) |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 40 | return self._parse_links(node, data) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 41 | |
| 42 | def _parse_image(self, node): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 43 | """Parses detailed XML image information into dictionary.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 44 | data = xml_utils.xml_to_json(node) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 45 | |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 46 | self._parse_links(node, data) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 47 | |
| 48 | # parse all metadata |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 49 | if 'metadata' in data: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 50 | tag = node.find('{%s}metadata' % xml_utils.XMLNS_11) |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 51 | data['metadata'] = dict((x.get('key'), x.text) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 52 | for x in tag.getchildren()) |
| 53 | |
| 54 | # parse server information |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 55 | if 'server' in data: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 56 | tag = node.find('{%s}server' % xml_utils.XMLNS_11) |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 57 | data['server'] = self._parse_server(tag) |
| 58 | return data |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 59 | |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 60 | def _parse_links(self, node, data): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 61 | """Append multiple links under a list.""" |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 62 | # look for links |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 63 | if 'link' in data: |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 64 | # remove single link element |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 65 | del data['link'] |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 66 | data['links'] = [xml_utils.xml_to_json(x) for x in |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 67 | node.findall('{http://www.w3.org/2005/Atom}link')] |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 68 | return data |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 69 | |
Matthew Treinish | 4e1b3e6 | 2013-01-10 11:30:49 -0500 | [diff] [blame] | 70 | def _parse_images(self, xml): |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 71 | data = {'images': []} |
Matthew Treinish | 4e1b3e6 | 2013-01-10 11:30:49 -0500 | [diff] [blame] | 72 | images = xml.getchildren() |
| 73 | for image in images: |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 74 | data['images'].append(self._parse_image(image)) |
| 75 | return data |
Matthew Treinish | 4e1b3e6 | 2013-01-10 11:30:49 -0500 | [diff] [blame] | 76 | |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 77 | def _parse_key_value(self, node): |
| 78 | """Parse <foo key='key'>value</foo> data into {'key': 'value'}.""" |
| 79 | data = {} |
| 80 | for node in node.getchildren(): |
| 81 | data[node.get('key')] = node.text |
| 82 | return data |
| 83 | |
| 84 | def _parse_metadata(self, node): |
| 85 | """Parse the response body without children.""" |
| 86 | data = {} |
| 87 | data[node.get('key')] = node.text |
| 88 | return data |
| 89 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 90 | def create_image(self, server_id, name, meta=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 91 | """Creates an image of the original server.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 92 | post_body = xml_utils.Element('createImage', name=name) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 93 | |
| 94 | if meta: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 95 | metadata = xml_utils.Element('metadata') |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 96 | post_body.append(metadata) |
| 97 | for k, v in meta.items(): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 98 | data = xml_utils.Element('meta', key=k) |
| 99 | data.append(xml_utils.Text(v)) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 100 | metadata.append(data) |
| 101 | resp, body = self.post('servers/%s/action' % str(server_id), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 102 | str(xml_utils.Document(post_body))) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 103 | return resp, body |
| 104 | |
| 105 | def list_images(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 106 | """Returns a list of all images filtered by any parameters.""" |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 107 | url = 'images' |
| 108 | if params: |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 109 | url += '?%s' % urllib.urlencode(params) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 110 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 111 | resp, body = self.get(url) |
Matthew Treinish | 4e1b3e6 | 2013-01-10 11:30:49 -0500 | [diff] [blame] | 112 | body = self._parse_images(etree.fromstring(body)) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 113 | return resp, body['images'] |
| 114 | |
| 115 | def list_images_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 116 | """Returns a detailed list of images filtered by any parameters.""" |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 117 | url = 'images/detail' |
| 118 | if params: |
| 119 | param_list = urllib.urlencode(params) |
| 120 | |
| 121 | url = "images/detail?" + param_list |
| 122 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 123 | resp, body = self.get(url) |
Matthew Treinish | 4e1b3e6 | 2013-01-10 11:30:49 -0500 | [diff] [blame] | 124 | body = self._parse_images(etree.fromstring(body)) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 125 | return resp, body['images'] |
| 126 | |
| 127 | def get_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 128 | """Returns the details of a single image.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 129 | resp, body = self.get("images/%s" % str(image_id)) |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 130 | self.expected_success(200, resp) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 131 | body = self._parse_image(etree.fromstring(body)) |
| 132 | return resp, body |
| 133 | |
| 134 | def delete_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 135 | """Deletes the provided image.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 136 | return self.delete("images/%s" % str(image_id)) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 137 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 138 | def wait_for_image_status(self, image_id, status): |
| 139 | """Waits for an image to reach a given status.""" |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 140 | waiters.wait_for_image_status(self, image_id, status) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 141 | |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 142 | def _metadata_body(self, meta): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 143 | post_body = xml_utils.Element('metadata') |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 144 | for k, v in meta.items(): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 145 | data = xml_utils.Element('meta', key=k) |
| 146 | data.append(xml_utils.Text(v)) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 147 | post_body.append(data) |
| 148 | return post_body |
| 149 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 150 | def list_image_metadata(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 151 | """Lists all metadata items for an image.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 152 | resp, body = self.get("images/%s/metadata" % str(image_id)) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 153 | body = self._parse_key_value(etree.fromstring(body)) |
| 154 | return resp, body |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 155 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 156 | def set_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 157 | """Sets the metadata for an image.""" |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 158 | post_body = self._metadata_body(meta) |
| 159 | resp, body = self.put('images/%s/metadata' % image_id, |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 160 | str(xml_utils.Document(post_body))) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 161 | body = self._parse_key_value(etree.fromstring(body)) |
| 162 | return resp, body |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 163 | |
| 164 | def update_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 165 | """Updates the metadata for an image.""" |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 166 | post_body = self._metadata_body(meta) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 167 | resp, body = self.post('images/%s/metadata' % str(image_id), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 168 | str(xml_utils.Document(post_body))) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 169 | body = self._parse_key_value(etree.fromstring(body)) |
| 170 | return resp, body |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 171 | |
| 172 | def get_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 173 | """Returns the value for a specific image metadata key.""" |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 174 | resp, body = self.get("images/%s/metadata/%s.xml" % |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 175 | (str(image_id), key)) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 176 | body = self._parse_metadata(etree.fromstring(body)) |
| 177 | return resp, body |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 178 | |
| 179 | def set_image_metadata_item(self, image_id, key, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 180 | """Sets the value for a specific image metadata key.""" |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 181 | for k, v in meta.items(): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 182 | post_body = xml_utils.Element('meta', key=key) |
| 183 | post_body.append(xml_utils.Text(v)) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 184 | resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 185 | str(xml_utils.Document(post_body))) |
| 186 | body = xml_utils.xml_to_json(etree.fromstring(body)) |
nayna-patel | eda1d12 | 2013-03-20 14:44:31 +0000 | [diff] [blame] | 187 | return resp, body |
Attila Fazekas | 7b487be | 2013-02-12 11:14:41 +0100 | [diff] [blame] | 188 | |
| 189 | def update_image_metadata_item(self, image_id, key, meta): |
| 190 | """Sets the value for a specific image metadata key.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 191 | post_body = xml_utils.Document('meta', xml_utils.Text(meta), key=key) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 192 | resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 193 | post_body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 194 | body = xml_utils.xml_to_json(etree.fromstring(body)) |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 195 | return resp, body['meta'] |
| 196 | |
| 197 | def delete_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 198 | """Deletes a single image metadata key/value pair.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 199 | return self.delete("images/%s/metadata/%s" % (str(image_id), key)) |
Matthew Treinish | 0d66049 | 2013-06-04 17:26:09 -0400 | [diff] [blame] | 200 | |
| 201 | def is_resource_deleted(self, id): |
| 202 | try: |
| 203 | self.get_image(id) |
| 204 | except exceptions.NotFound: |
| 205 | return True |
| 206 | return False |