blob: 68a27c93287f402569b360e16844ff37adc9b859 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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
Tiago Melloeda03b52012-08-22 23:47:29 -030016import urllib
17
18from lxml import etree
19
vponomaryov960eeb42014-02-22 18:25:25 +020020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
dwallecke62b9f02012-10-10 23:34:42 -050022from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
ivan-zhuae7c7c52013-10-21 22:13:22 +080024from tempest.services.compute.xml.common import Text
dwallecke62b9f02012-10-10 23:34:42 -050025from tempest.services.compute.xml.common import xml_to_json
26from tempest.services.compute.xml.common import XMLNS_11
Tiago Melloeda03b52012-08-22 23:47:29 -030027
Matthew Treinish684d8992014-01-30 16:27:40 +000028CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030029
30XMLNS_OS_FLV_EXT_DATA = \
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053031 "http://docs.openstack.org/compute/ext/flavor_extra_data/api/v1.1"
32XMLNS_OS_FLV_ACCESS = \
ivan-zhuae7c7c52013-10-21 22:13:22 +080033 "http://docs.openstack.org/compute/ext/flavor_access/api/v2"
Tiago Melloeda03b52012-08-22 23:47:29 -030034
35
vponomaryov960eeb42014-02-22 18:25:25 +020036class FlavorsClientXML(rest_client.RestClient):
37 TYPE = "xml"
Tiago Melloeda03b52012-08-22 23:47:29 -030038
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000039 def __init__(self, auth_provider):
40 super(FlavorsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000041 self.service = CONF.compute.catalog_type
Tiago Melloeda03b52012-08-22 23:47:29 -030042
43 def _format_flavor(self, f):
44 flavor = {'links': []}
45 for k, v in f.items():
Ken'ichi Ohmichi35772602013-11-14 15:03:27 +090046 if k == 'id':
47 flavor['id'] = v
48 continue
49
Tiago Melloeda03b52012-08-22 23:47:29 -030050 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-zhuae7c7c52013-10-21 22:13:22 +080057 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 Raveeshccfa6532013-08-19 17:11:05 +053061 if k == 'extra_specs':
Aarti Kriplanic04a0fc2013-08-06 05:10:49 -050062 k = 'OS-FLV-WITH-EXT-SPECS:extra_specs'
63 flavor[k] = dict(v)
64 continue
65
Tiago Melloeda03b52012-08-22 23:47:29 -030066 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 Treinish26dd0fa2012-12-04 17:14:37 -050082 if params:
Tiago Melloeda03b52012-08-22 23:47:29 -030083 url += "?%s" % urllib.urlencode(params)
84
vponomaryovf4c27f92014-02-18 10:56:42 +020085 resp, body = self.get(url)
Tiago Melloeda03b52012-08-22 23:47:29 -030086 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):
vponomaryovf4c27f92014-02-18 10:56:42 +020098 resp, body = self.get("flavors/%s" % str(flavor_id))
Tiago Melloeda03b52012-08-22 23:47:29 -030099 body = xml_to_json(etree.fromstring(body))
100 flavor = self._format_flavor(body)
101 return resp, flavor
102
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530103 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500104 """Creates a new flavor or instance type."""
Tiago Melloeda03b52012-08-22 23:47:29 -0300105 flavor = Element("flavor",
106 xmlns=XMLNS_11,
107 ram=ram,
108 vcpus=vcpus,
109 disk=disk,
110 id=flavor_id,
Tiago Melloeda03b52012-08-22 23:47:29 -0300111 name=name)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530112 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 Melloeda03b52012-08-22 23:47:29 -0300122 flavor.add_attr('xmlns:OS-FLV-EXT-DATA', XMLNS_OS_FLV_EXT_DATA)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530123 flavor.add_attr('xmlns:os-flavor-access', XMLNS_OS_FLV_ACCESS)
vponomaryovf4c27f92014-02-18 10:56:42 +0200124 resp, body = self.post('flavors', str(Document(flavor)))
Tiago Melloeda03b52012-08-22 23:47:29 -0300125 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 Daguef237ccb2013-01-04 15:19:14 -0500130 """Deletes the given flavor."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200131 return self.delete("flavors/%s" % str(flavor_id))
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530132
133 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200134 # 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-ganesanf344cc32012-12-31 20:02:27 +0530137 resp, flavors = self.list_flavors_with_detail()
138 for flavor in flavors:
139 if flavor['id'] == id:
140 return False
141 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530142
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,
vponomaryovf4c27f92014-02-18 10:56:42 +0200149 str(Document(extra_specs)))
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530150 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."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200155 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530156 body = xml_to_json(etree.fromstring(body))
157 return resp, body
158
lijunjbj9feaa212013-10-14 02:11:49 -0500159 def get_flavor_extra_spec_with_key(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500160 """Gets extra Specs key-value of the mentioned flavor and key."""
161 resp, xml_body = self.get('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200162 (str(flavor_id), key))
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500163 body = {}
164 element = etree.fromstring(xml_body)
165 key = element.get('key')
166 body[key] = xml_to_json(element)
lijunjbj9feaa212013-10-14 02:11:49 -0500167 return resp, body
168
ivan-zhuae7c7c52013-10-21 22:13:22 +0800169 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500170 """Update extra Specs details of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800171 doc = Document()
172 for (k, v) in kwargs.items():
173 element = Element(k)
174 doc.append(element)
175 value = Text(v)
176 element.append(value)
177
178 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200179 (flavor_id, key), str(doc))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800180 body = xml_to_json(etree.fromstring(body))
181 return resp, {key: body}
182
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530183 def unset_flavor_extra_spec(self, flavor_id, key):
184 """Unsets an extra spec based on the mentioned flavor and key."""
185 return self.delete('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
186 key))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900187
188 def _parse_array_access(self, node):
189 return [xml_to_json(x) for x in node]
190
Zhu Zhu705f24a2013-10-11 05:52:54 -0500191 def list_flavor_access(self, flavor_id):
192 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200193 resp, body = self.get('flavors/%s/os-flavor-access' % str(flavor_id))
Zhu Zhu705f24a2013-10-11 05:52:54 -0500194 body = self._parse_array(etree.fromstring(body))
195 return resp, body
196
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900197 def add_flavor_access(self, flavor_id, tenant_id):
198 """Add flavor access for the specified tenant."""
199 doc = Document()
200 server = Element("addTenantAccess")
201 doc.append(server)
202 server.add_attr("tenant", tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +0200203 resp, body = self.post('flavors/%s/action' % str(flavor_id), str(doc))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900204 body = self._parse_array_access(etree.fromstring(body))
205 return resp, body
206
207 def remove_flavor_access(self, flavor_id, tenant_id):
208 """Remove flavor access from the specified tenant."""
209 doc = Document()
210 server = Element("removeTenantAccess")
211 doc.append(server)
212 server.add_attr("tenant", tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +0200213 resp, body = self.post('flavors/%s/action' % str(flavor_id), str(doc))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900214 body = self._parse_array_access(etree.fromstring(body))
215 return resp, body