blob: 554b25363916789a854abcc5c3c083f8734352f4 [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
20from tempest.common.rest_client import RestClientXML
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
36class FlavorsClientXML(RestClientXML):
37
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000038 def __init__(self, auth_provider):
39 super(FlavorsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000040 self.service = CONF.compute.catalog_type
Tiago Melloeda03b52012-08-22 23:47:29 -030041
42 def _format_flavor(self, f):
43 flavor = {'links': []}
44 for k, v in f.items():
Ken'ichi Ohmichi35772602013-11-14 15:03:27 +090045 if k == 'id':
46 flavor['id'] = v
47 continue
48
Tiago Melloeda03b52012-08-22 23:47:29 -030049 if k == 'link':
50 flavor['links'].append(v)
51 continue
52
53 if k == '{%s}ephemeral' % XMLNS_OS_FLV_EXT_DATA:
54 k = 'OS-FLV-EXT-DATA:ephemeral'
55
ivan-zhuae7c7c52013-10-21 22:13:22 +080056 if k == '{%s}is_public' % XMLNS_OS_FLV_ACCESS:
57 k = 'os-flavor-access:is_public'
58 v = True if v == 'True' else False
59
Aditi Raveeshccfa6532013-08-19 17:11:05 +053060 if k == 'extra_specs':
Aarti Kriplanic04a0fc2013-08-06 05:10:49 -050061 k = 'OS-FLV-WITH-EXT-SPECS:extra_specs'
62 flavor[k] = dict(v)
63 continue
64
Tiago Melloeda03b52012-08-22 23:47:29 -030065 try:
66 v = int(v)
67 except ValueError:
68 try:
69 v = float(v)
70 except ValueError:
71 pass
72
73 flavor[k] = v
74
75 return flavor
76
77 def _parse_array(self, node):
78 return [self._format_flavor(xml_to_json(x)) for x in node]
79
80 def _list_flavors(self, url, params):
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050081 if params:
Tiago Melloeda03b52012-08-22 23:47:29 -030082 url += "?%s" % urllib.urlencode(params)
83
vponomaryovf4c27f92014-02-18 10:56:42 +020084 resp, body = self.get(url)
Tiago Melloeda03b52012-08-22 23:47:29 -030085 flavors = self._parse_array(etree.fromstring(body))
86 return resp, flavors
87
88 def list_flavors(self, params=None):
89 url = 'flavors'
90 return self._list_flavors(url, params)
91
92 def list_flavors_with_detail(self, params=None):
93 url = 'flavors/detail'
94 return self._list_flavors(url, params)
95
96 def get_flavor_details(self, flavor_id):
vponomaryovf4c27f92014-02-18 10:56:42 +020097 resp, body = self.get("flavors/%s" % str(flavor_id))
Tiago Melloeda03b52012-08-22 23:47:29 -030098 body = xml_to_json(etree.fromstring(body))
99 flavor = self._format_flavor(body)
100 return resp, flavor
101
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530102 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500103 """Creates a new flavor or instance type."""
Tiago Melloeda03b52012-08-22 23:47:29 -0300104 flavor = Element("flavor",
105 xmlns=XMLNS_11,
106 ram=ram,
107 vcpus=vcpus,
108 disk=disk,
109 id=flavor_id,
Tiago Melloeda03b52012-08-22 23:47:29 -0300110 name=name)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530111 if kwargs.get('rxtx'):
112 flavor.add_attr('rxtx_factor', kwargs.get('rxtx'))
113 if kwargs.get('swap'):
114 flavor.add_attr('swap', kwargs.get('swap'))
115 if kwargs.get('ephemeral'):
116 flavor.add_attr('OS-FLV-EXT-DATA:ephemeral',
117 kwargs.get('ephemeral'))
118 if kwargs.get('is_public'):
119 flavor.add_attr('os-flavor-access:is_public',
120 kwargs.get('is_public'))
Tiago Melloeda03b52012-08-22 23:47:29 -0300121 flavor.add_attr('xmlns:OS-FLV-EXT-DATA', XMLNS_OS_FLV_EXT_DATA)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530122 flavor.add_attr('xmlns:os-flavor-access', XMLNS_OS_FLV_ACCESS)
vponomaryovf4c27f92014-02-18 10:56:42 +0200123 resp, body = self.post('flavors', str(Document(flavor)))
Tiago Melloeda03b52012-08-22 23:47:29 -0300124 body = xml_to_json(etree.fromstring(body))
125 flavor = self._format_flavor(body)
126 return resp, flavor
127
128 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500129 """Deletes the given flavor."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200130 return self.delete("flavors/%s" % str(flavor_id))
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530131
132 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200133 # Did not use get_flavor_details(id) for verification as it gives
134 # 200 ok even for deleted id. LP #981263
135 # we can remove the loop here and use get by ID when bug gets sortedout
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530136 resp, flavors = self.list_flavors_with_detail()
137 for flavor in flavors:
138 if flavor['id'] == id:
139 return False
140 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530141
142 def set_flavor_extra_spec(self, flavor_id, specs):
143 """Sets extra Specs to the mentioned flavor."""
144 extra_specs = Element("extra_specs")
145 for key in specs.keys():
146 extra_specs.add_attr(key, specs[key])
147 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200148 str(Document(extra_specs)))
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530149 body = xml_to_json(etree.fromstring(body))
150 return resp, body
151
152 def get_flavor_extra_spec(self, flavor_id):
153 """Gets extra Specs of the mentioned flavor."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200154 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530155 body = xml_to_json(etree.fromstring(body))
156 return resp, body
157
lijunjbj9feaa212013-10-14 02:11:49 -0500158 def get_flavor_extra_spec_with_key(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500159 """Gets extra Specs key-value of the mentioned flavor and key."""
160 resp, xml_body = self.get('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200161 (str(flavor_id), key))
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500162 body = {}
163 element = etree.fromstring(xml_body)
164 key = element.get('key')
165 body[key] = xml_to_json(element)
lijunjbj9feaa212013-10-14 02:11:49 -0500166 return resp, body
167
ivan-zhuae7c7c52013-10-21 22:13:22 +0800168 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500169 """Update extra Specs details of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800170 doc = Document()
171 for (k, v) in kwargs.items():
172 element = Element(k)
173 doc.append(element)
174 value = Text(v)
175 element.append(value)
176
177 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200178 (flavor_id, key), str(doc))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800179 body = xml_to_json(etree.fromstring(body))
180 return resp, {key: body}
181
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530182 def unset_flavor_extra_spec(self, flavor_id, key):
183 """Unsets an extra spec based on the mentioned flavor and key."""
184 return self.delete('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
185 key))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900186
187 def _parse_array_access(self, node):
188 return [xml_to_json(x) for x in node]
189
Zhu Zhu705f24a2013-10-11 05:52:54 -0500190 def list_flavor_access(self, flavor_id):
191 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200192 resp, body = self.get('flavors/%s/os-flavor-access' % str(flavor_id))
Zhu Zhu705f24a2013-10-11 05:52:54 -0500193 body = self._parse_array(etree.fromstring(body))
194 return resp, body
195
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900196 def add_flavor_access(self, flavor_id, tenant_id):
197 """Add flavor access for the specified tenant."""
198 doc = Document()
199 server = Element("addTenantAccess")
200 doc.append(server)
201 server.add_attr("tenant", tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +0200202 resp, body = self.post('flavors/%s/action' % str(flavor_id), str(doc))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900203 body = self._parse_array_access(etree.fromstring(body))
204 return resp, body
205
206 def remove_flavor_access(self, flavor_id, tenant_id):
207 """Remove flavor access from the specified tenant."""
208 doc = Document()
209 server = Element("removeTenantAccess")
210 doc.append(server)
211 server.add_attr("tenant", tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +0200212 resp, body = self.post('flavors/%s/action' % str(flavor_id), str(doc))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900213 body = self._parse_array_access(etree.fromstring(body))
214 return resp, body