| dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
| dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 18 | import urllib |
| 19 | |
| 20 | from lxml import etree |
| 21 | |
| 22 | from tempest.common.rest_client import RestClientXML |
| dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import Document |
| 24 | from tempest.services.compute.xml.common import Element |
| ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import Text |
| dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 26 | from tempest.services.compute.xml.common import xml_to_json |
| 27 | from tempest.services.compute.xml.common import XMLNS_11 |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 28 | |
| 29 | |
| 30 | XMLNS_OS_FLV_EXT_DATA = \ |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 31 | "http://docs.openstack.org/compute/ext/flavor_extra_data/api/v1.1" |
| 32 | XMLNS_OS_FLV_ACCESS = \ |
| ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 33 | "http://docs.openstack.org/compute/ext/flavor_access/api/v2" |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 34 | |
| 35 | |
| 36 | class FlavorsClientXML(RestClientXML): |
| 37 | |
| 38 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 39 | super(FlavorsClientXML, self).__init__(config, username, password, |
| 40 | auth_url, tenant_name) |
| 41 | self.service = self.config.compute.catalog_type |
| 42 | |
| 43 | def _format_flavor(self, f): |
| 44 | flavor = {'links': []} |
| 45 | for k, v in f.items(): |
| Ken'ichi Ohmichi | 3577260 | 2013-11-14 15:03:27 +0900 | [diff] [blame] | 46 | if k == 'id': |
| 47 | flavor['id'] = v |
| 48 | continue |
| 49 | |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 50 | if k == 'link': |
| 51 | flavor['links'].append(v) |
| 52 | continue |
| 53 | |
| 54 | if k == '{%s}ephemeral' % XMLNS_OS_FLV_EXT_DATA: |
| 55 | k = 'OS-FLV-EXT-DATA:ephemeral' |
| 56 | |
| ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 57 | if k == '{%s}is_public' % XMLNS_OS_FLV_ACCESS: |
| 58 | k = 'os-flavor-access:is_public' |
| 59 | v = True if v == 'True' else False |
| 60 | |
| Aditi Raveesh | ccfa653 | 2013-08-19 17:11:05 +0530 | [diff] [blame] | 61 | if k == 'extra_specs': |
| Aarti Kriplani | c04a0fc | 2013-08-06 05:10:49 -0500 | [diff] [blame] | 62 | k = 'OS-FLV-WITH-EXT-SPECS:extra_specs' |
| 63 | flavor[k] = dict(v) |
| 64 | continue |
| 65 | |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 66 | try: |
| 67 | v = int(v) |
| 68 | except ValueError: |
| 69 | try: |
| 70 | v = float(v) |
| 71 | except ValueError: |
| 72 | pass |
| 73 | |
| 74 | flavor[k] = v |
| 75 | |
| 76 | return flavor |
| 77 | |
| 78 | def _parse_array(self, node): |
| 79 | return [self._format_flavor(xml_to_json(x)) for x in node] |
| 80 | |
| 81 | def _list_flavors(self, url, params): |
| Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 82 | if params: |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 83 | url += "?%s" % urllib.urlencode(params) |
| 84 | |
| 85 | resp, body = self.get(url, self.headers) |
| 86 | flavors = self._parse_array(etree.fromstring(body)) |
| 87 | return resp, flavors |
| 88 | |
| 89 | def list_flavors(self, params=None): |
| 90 | url = 'flavors' |
| 91 | return self._list_flavors(url, params) |
| 92 | |
| 93 | def list_flavors_with_detail(self, params=None): |
| 94 | url = 'flavors/detail' |
| 95 | return self._list_flavors(url, params) |
| 96 | |
| 97 | def get_flavor_details(self, flavor_id): |
| 98 | resp, body = self.get("flavors/%s" % str(flavor_id), self.headers) |
| 99 | body = xml_to_json(etree.fromstring(body)) |
| 100 | flavor = self._format_flavor(body) |
| 101 | return resp, flavor |
| 102 | |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 103 | def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs): |
| Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 104 | """Creates a new flavor or instance type.""" |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 105 | flavor = Element("flavor", |
| 106 | xmlns=XMLNS_11, |
| 107 | ram=ram, |
| 108 | vcpus=vcpus, |
| 109 | disk=disk, |
| 110 | id=flavor_id, |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 111 | name=name) |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 112 | if kwargs.get('rxtx'): |
| 113 | flavor.add_attr('rxtx_factor', kwargs.get('rxtx')) |
| 114 | if kwargs.get('swap'): |
| 115 | flavor.add_attr('swap', kwargs.get('swap')) |
| 116 | if kwargs.get('ephemeral'): |
| 117 | flavor.add_attr('OS-FLV-EXT-DATA:ephemeral', |
| 118 | kwargs.get('ephemeral')) |
| 119 | if kwargs.get('is_public'): |
| 120 | flavor.add_attr('os-flavor-access:is_public', |
| 121 | kwargs.get('is_public')) |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 122 | flavor.add_attr('xmlns:OS-FLV-EXT-DATA', XMLNS_OS_FLV_EXT_DATA) |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 123 | flavor.add_attr('xmlns:os-flavor-access', XMLNS_OS_FLV_ACCESS) |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 124 | resp, body = self.post('flavors', str(Document(flavor)), self.headers) |
| 125 | body = xml_to_json(etree.fromstring(body)) |
| 126 | flavor = self._format_flavor(body) |
| 127 | return resp, flavor |
| 128 | |
| 129 | def delete_flavor(self, flavor_id): |
| Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 130 | """Deletes the given flavor.""" |
| Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 131 | return self.delete("flavors/%s" % str(flavor_id), self.headers) |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 132 | |
| 133 | def is_resource_deleted(self, id): |
| Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 134 | # Did not use get_flavor_details(id) for verification as it gives |
| 135 | # 200 ok even for deleted id. LP #981263 |
| 136 | # we can remove the loop here and use get by ID when bug gets sortedout |
| rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 137 | resp, flavors = self.list_flavors_with_detail() |
| 138 | for flavor in flavors: |
| 139 | if flavor['id'] == id: |
| 140 | return False |
| 141 | return True |
| rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 142 | |
| 143 | def set_flavor_extra_spec(self, flavor_id, specs): |
| 144 | """Sets extra Specs to the mentioned flavor.""" |
| 145 | extra_specs = Element("extra_specs") |
| 146 | for key in specs.keys(): |
| 147 | extra_specs.add_attr(key, specs[key]) |
| 148 | resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id, |
| 149 | str(Document(extra_specs)), self.headers) |
| 150 | body = xml_to_json(etree.fromstring(body)) |
| 151 | return resp, body |
| 152 | |
| 153 | def get_flavor_extra_spec(self, flavor_id): |
| 154 | """Gets extra Specs of the mentioned flavor.""" |
| 155 | resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id, |
| 156 | self.headers) |
| 157 | body = xml_to_json(etree.fromstring(body)) |
| 158 | return resp, body |
| 159 | |
| lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 160 | def get_flavor_extra_spec_with_key(self, flavor_id, key): |
| Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 161 | """Gets extra Specs key-value of the mentioned flavor and key.""" |
| 162 | resp, xml_body = self.get('flavors/%s/os-extra_specs/%s' % |
| 163 | (str(flavor_id), key), self.headers) |
| 164 | body = {} |
| 165 | element = etree.fromstring(xml_body) |
| 166 | key = element.get('key') |
| 167 | body[key] = xml_to_json(element) |
| lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 168 | return resp, body |
| 169 | |
| ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 170 | def update_flavor_extra_spec(self, flavor_id, key, **kwargs): |
| Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 171 | """Update extra Specs details of the mentioned flavor and key.""" |
| ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 172 | doc = Document() |
| 173 | for (k, v) in kwargs.items(): |
| 174 | element = Element(k) |
| 175 | doc.append(element) |
| 176 | value = Text(v) |
| 177 | element.append(value) |
| 178 | |
| 179 | resp, body = self.put('flavors/%s/os-extra_specs/%s' % |
| 180 | (flavor_id, key), |
| 181 | str(doc), self.headers) |
| 182 | body = xml_to_json(etree.fromstring(body)) |
| 183 | return resp, {key: body} |
| 184 | |
| rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 185 | def unset_flavor_extra_spec(self, flavor_id, key): |
| 186 | """Unsets an extra spec based on the mentioned flavor and key.""" |
| 187 | return self.delete('flavors/%s/os-extra_specs/%s' % (str(flavor_id), |
| 188 | key)) |
| Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 189 | |
| 190 | def _parse_array_access(self, node): |
| 191 | return [xml_to_json(x) for x in node] |
| 192 | |
| Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 193 | def list_flavor_access(self, flavor_id): |
| 194 | """Gets flavor access information given the flavor id.""" |
| 195 | resp, body = self.get('flavors/%s/os-flavor-access' % str(flavor_id), |
| 196 | self.headers) |
| 197 | body = self._parse_array(etree.fromstring(body)) |
| 198 | return resp, body |
| 199 | |
| Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 200 | def add_flavor_access(self, flavor_id, tenant_id): |
| 201 | """Add flavor access for the specified tenant.""" |
| 202 | doc = Document() |
| 203 | server = Element("addTenantAccess") |
| 204 | doc.append(server) |
| 205 | server.add_attr("tenant", tenant_id) |
| 206 | resp, body = self.post('flavors/%s/action' % str(flavor_id), |
| 207 | str(doc), self.headers) |
| 208 | body = self._parse_array_access(etree.fromstring(body)) |
| 209 | return resp, body |
| 210 | |
| 211 | def remove_flavor_access(self, flavor_id, tenant_id): |
| 212 | """Remove flavor access from the specified tenant.""" |
| 213 | doc = Document() |
| 214 | server = Element("removeTenantAccess") |
| 215 | doc.append(server) |
| 216 | server.add_attr("tenant", tenant_id) |
| 217 | resp, body = self.post('flavors/%s/action' % str(flavor_id), |
| 218 | str(doc), self.headers) |
| 219 | body = self._parse_array_access(etree.fromstring(body)) |
| 220 | return resp, body |