blob: b7e63e6e9bddf80467ba1268ad82809e7ac0e99c [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
dwallecke62b9f02012-10-10 23:34:42 -050021from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
ivan-zhuae7c7c52013-10-21 22:13:22 +080023from tempest.services.compute.xml.common import Text
dwallecke62b9f02012-10-10 23:34:42 -050024from tempest.services.compute.xml.common import xml_to_json
25from tempest.services.compute.xml.common import XMLNS_11
Tiago Melloeda03b52012-08-22 23:47:29 -030026
27
28XMLNS_OS_FLV_EXT_DATA = \
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053029 "http://docs.openstack.org/compute/ext/flavor_extra_data/api/v1.1"
30XMLNS_OS_FLV_ACCESS = \
ivan-zhuae7c7c52013-10-21 22:13:22 +080031 "http://docs.openstack.org/compute/ext/flavor_access/api/v2"
Tiago Melloeda03b52012-08-22 23:47:29 -030032
33
34class FlavorsClientXML(RestClientXML):
35
36 def __init__(self, config, username, password, auth_url, tenant_name=None):
37 super(FlavorsClientXML, self).__init__(config, username, password,
38 auth_url, tenant_name)
39 self.service = self.config.compute.catalog_type
40
41 def _format_flavor(self, f):
42 flavor = {'links': []}
43 for k, v in f.items():
Ken'ichi Ohmichi35772602013-11-14 15:03:27 +090044 if k == 'id':
45 flavor['id'] = v
46 continue
47
Tiago Melloeda03b52012-08-22 23:47:29 -030048 if k == 'link':
49 flavor['links'].append(v)
50 continue
51
52 if k == '{%s}ephemeral' % XMLNS_OS_FLV_EXT_DATA:
53 k = 'OS-FLV-EXT-DATA:ephemeral'
54
ivan-zhuae7c7c52013-10-21 22:13:22 +080055 if k == '{%s}is_public' % XMLNS_OS_FLV_ACCESS:
56 k = 'os-flavor-access:is_public'
57 v = True if v == 'True' else False
58
Aditi Raveeshccfa6532013-08-19 17:11:05 +053059 if k == 'extra_specs':
Aarti Kriplanic04a0fc2013-08-06 05:10:49 -050060 k = 'OS-FLV-WITH-EXT-SPECS:extra_specs'
61 flavor[k] = dict(v)
62 continue
63
Tiago Melloeda03b52012-08-22 23:47:29 -030064 try:
65 v = int(v)
66 except ValueError:
67 try:
68 v = float(v)
69 except ValueError:
70 pass
71
72 flavor[k] = v
73
74 return flavor
75
76 def _parse_array(self, node):
77 return [self._format_flavor(xml_to_json(x)) for x in node]
78
79 def _list_flavors(self, url, params):
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050080 if params:
Tiago Melloeda03b52012-08-22 23:47:29 -030081 url += "?%s" % urllib.urlencode(params)
82
83 resp, body = self.get(url, self.headers)
84 flavors = self._parse_array(etree.fromstring(body))
85 return resp, flavors
86
87 def list_flavors(self, params=None):
88 url = 'flavors'
89 return self._list_flavors(url, params)
90
91 def list_flavors_with_detail(self, params=None):
92 url = 'flavors/detail'
93 return self._list_flavors(url, params)
94
95 def get_flavor_details(self, flavor_id):
96 resp, body = self.get("flavors/%s" % str(flavor_id), self.headers)
97 body = xml_to_json(etree.fromstring(body))
98 flavor = self._format_flavor(body)
99 return resp, flavor
100
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530101 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """Creates a new flavor or instance type."""
Tiago Melloeda03b52012-08-22 23:47:29 -0300103 flavor = Element("flavor",
104 xmlns=XMLNS_11,
105 ram=ram,
106 vcpus=vcpus,
107 disk=disk,
108 id=flavor_id,
Tiago Melloeda03b52012-08-22 23:47:29 -0300109 name=name)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530110 if kwargs.get('rxtx'):
111 flavor.add_attr('rxtx_factor', kwargs.get('rxtx'))
112 if kwargs.get('swap'):
113 flavor.add_attr('swap', kwargs.get('swap'))
114 if kwargs.get('ephemeral'):
115 flavor.add_attr('OS-FLV-EXT-DATA:ephemeral',
116 kwargs.get('ephemeral'))
117 if kwargs.get('is_public'):
118 flavor.add_attr('os-flavor-access:is_public',
119 kwargs.get('is_public'))
Tiago Melloeda03b52012-08-22 23:47:29 -0300120 flavor.add_attr('xmlns:OS-FLV-EXT-DATA', XMLNS_OS_FLV_EXT_DATA)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530121 flavor.add_attr('xmlns:os-flavor-access', XMLNS_OS_FLV_ACCESS)
Tiago Melloeda03b52012-08-22 23:47:29 -0300122 resp, body = self.post('flavors', str(Document(flavor)), self.headers)
123 body = xml_to_json(etree.fromstring(body))
124 flavor = self._format_flavor(body)
125 return resp, flavor
126
127 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500128 """Deletes the given flavor."""
Tiago Melloeda03b52012-08-22 23:47:29 -0300129 return self.delete("flavors/%s" % str(flavor_id), self.headers)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530130
131 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200132 # Did not use get_flavor_details(id) for verification as it gives
133 # 200 ok even for deleted id. LP #981263
134 # we can remove the loop here and use get by ID when bug gets sortedout
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +0530135 resp, flavors = self.list_flavors_with_detail()
136 for flavor in flavors:
137 if flavor['id'] == id:
138 return False
139 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530140
141 def set_flavor_extra_spec(self, flavor_id, specs):
142 """Sets extra Specs to the mentioned flavor."""
143 extra_specs = Element("extra_specs")
144 for key in specs.keys():
145 extra_specs.add_attr(key, specs[key])
146 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
147 str(Document(extra_specs)), self.headers)
148 body = xml_to_json(etree.fromstring(body))
149 return resp, body
150
151 def get_flavor_extra_spec(self, flavor_id):
152 """Gets extra Specs of the mentioned flavor."""
153 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id,
154 self.headers)
155 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' %
161 (str(flavor_id), key), self.headers)
162 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' %
178 (flavor_id, key),
179 str(doc), self.headers)
180 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."""
193 resp, body = self.get('flavors/%s/os-flavor-access' % str(flavor_id),
194 self.headers)
195 body = self._parse_array(etree.fromstring(body))
196 return resp, body
197
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900198 def add_flavor_access(self, flavor_id, tenant_id):
199 """Add flavor access for the specified tenant."""
200 doc = Document()
201 server = Element("addTenantAccess")
202 doc.append(server)
203 server.add_attr("tenant", tenant_id)
204 resp, body = self.post('flavors/%s/action' % str(flavor_id),
205 str(doc), self.headers)
206 body = self._parse_array_access(etree.fromstring(body))
207 return resp, body
208
209 def remove_flavor_access(self, flavor_id, tenant_id):
210 """Remove flavor access from the specified tenant."""
211 doc = Document()
212 server = Element("removeTenantAccess")
213 doc.append(server)
214 server.add_attr("tenant", tenant_id)
215 resp, body = self.post('flavors/%s/action' % str(flavor_id),
216 str(doc), self.headers)
217 body = self._parse_array_access(etree.fromstring(body))
218 return resp, body