blob: 7938d8ea504d228cc9e046bb73442401278f075d [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
Daryl Walleck1465d612011-11-02 02:22:15 -050016import json
Matthew Treinish89128142015-04-23 10:44:30 -040017
18from six.moves.urllib import parse as urllib
Daryl Walleck1465d612011-11-02 02:22:15 -050019
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090020from tempest.api_schema.response.compute.v2_1 import flavors as schema
ghanshyam59869d02015-04-22 17:23:08 +090021from tempest.api_schema.response.compute.v2_1 import flavors_access \
22 as schema_access
23from tempest.api_schema.response.compute.v2_1 import flavors_extra_specs \
24 as schema_extra_specs
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000025from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026
Daryl Walleck1465d612011-11-02 02:22:15 -050027
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000028class FlavorsClientJSON(service_client.ServiceClient):
Daryl Walleck1465d612011-11-02 02:22:15 -050029
30 def list_flavors(self, params=None):
31 url = 'flavors'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050032 if params:
33 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050034
chris fattarsi5098fa22012-04-17 13:27:00 -070035 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050036 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090037 self.validate_response(schema.list_flavors, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050038 return service_client.ResponseBodyList(resp, body['flavors'])
Daryl Walleck1465d612011-11-02 02:22:15 -050039
40 def list_flavors_with_detail(self, params=None):
41 url = 'flavors/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050042 if params:
43 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050044
chris fattarsi5098fa22012-04-17 13:27:00 -070045 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050046 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090047 self.validate_response(schema.list_flavors_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050048 return service_client.ResponseBodyList(resp, body['flavors'])
Daryl Walleck1465d612011-11-02 02:22:15 -050049
50 def get_flavor_details(self, flavor_id):
chris fattarsi5098fa22012-04-17 13:27:00 -070051 resp, body = self.get("flavors/%s" % str(flavor_id))
Daryl Walleck1465d612011-11-02 02:22:15 -050052 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090053 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050054 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070055
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053056 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """Creates a new flavor or instance type."""
Rohit Karajgi03530292012-04-24 17:00:50 -070058 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +090059 'name': name,
60 'ram': ram,
61 'vcpus': vcpus,
62 'disk': disk,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090063 'id': flavor_id,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090064 }
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053065 if kwargs.get('ephemeral'):
66 post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
67 if kwargs.get('swap'):
68 post_body['swap'] = kwargs.get('swap')
69 if kwargs.get('rxtx'):
70 post_body['rxtx_factor'] = kwargs.get('rxtx')
71 if kwargs.get('is_public'):
72 post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
Rohit Karajgi03530292012-04-24 17:00:50 -070073 post_body = json.dumps({'flavor': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020074 resp, body = self.post('flavors', post_body)
Rohit Karajgi03530292012-04-24 17:00:50 -070075
76 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090077 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050078 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070079
80 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050081 """Deletes the given flavor."""
Ghanshyame4c3fb22014-03-24 16:07:16 +090082 resp, body = self.delete("flavors/{0}".format(flavor_id))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090083 self.validate_response(schema.delete_flavor, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050084 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053085
86 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020087 # Did not use get_flavor_details(id) for verification as it gives
88 # 200 ok even for deleted id. LP #981263
89 # we can remove the loop here and use get by ID when bug gets sortedout
David Kranz2fa77b22015-02-09 11:39:50 -050090 flavors = self.list_flavors_with_detail()
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053091 for flavor in flavors:
92 if flavor['id'] == id:
93 return False
94 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053095
Matt Riedemannd2b96512014-10-13 10:18:16 -070096 @property
97 def resource_type(self):
98 """Returns the primary type of resource this client works with."""
99 return 'flavor'
100
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530101 def set_flavor_extra_spec(self, flavor_id, specs):
102 """Sets extra Specs to the mentioned flavor."""
103 post_body = json.dumps({'extra_specs': specs})
104 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200105 post_body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530106 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900107 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +0900108 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500109 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530110
111 def get_flavor_extra_spec(self, flavor_id):
112 """Gets extra Specs details of the mentioned flavor."""
113 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
114 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900115 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +0900116 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500117 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530118
lijunjbj9feaa212013-10-14 02:11:49 -0500119 def get_flavor_extra_spec_with_key(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500120 """Gets extra Specs key-value of the mentioned flavor and key."""
lijunjbj9feaa212013-10-14 02:11:49 -0500121 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
122 key))
123 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900124 self.validate_response(
125 schema_extra_specs.set_get_flavor_extra_specs_key,
126 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500127 return service_client.ResponseBody(resp, body)
lijunjbj9feaa212013-10-14 02:11:49 -0500128
ivan-zhuae7c7c52013-10-21 22:13:22 +0800129 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500130 """Update specified extra Specs of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800131 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200132 (flavor_id, key), json.dumps(kwargs))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800133 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900134 self.validate_response(
135 schema_extra_specs.set_get_flavor_extra_specs_key,
136 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500137 return service_client.ResponseBody(resp, body)
ivan-zhuae7c7c52013-10-21 22:13:22 +0800138
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530139 def unset_flavor_extra_spec(self, flavor_id, key):
140 """Unsets extra Specs from the mentioned flavor."""
Ghanshyam639d8422014-03-26 18:23:32 +0900141 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
142 (str(flavor_id), key))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +0900143 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500144 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900145
Zhu Zhu705f24a2013-10-11 05:52:54 -0500146 def list_flavor_access(self, flavor_id):
147 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200148 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500149 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900150 self.validate_response(schema_access.add_remove_list_flavor_access,
151 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500152 return service_client.ResponseBodyList(resp, body['flavor_access'])
Zhu Zhu705f24a2013-10-11 05:52:54 -0500153
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900154 def add_flavor_access(self, flavor_id, tenant_id):
155 """Add flavor access for the specified tenant."""
156 post_body = {
157 'addTenantAccess': {
158 'tenant': tenant_id
159 }
160 }
161 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200162 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900163 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900164 self.validate_response(schema_access.add_remove_list_flavor_access,
165 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500166 return service_client.ResponseBodyList(resp, body['flavor_access'])
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900167
168 def remove_flavor_access(self, flavor_id, tenant_id):
169 """Remove flavor access from the specified tenant."""
170 post_body = {
171 'removeTenantAccess': {
172 'tenant': tenant_id
173 }
174 }
175 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200176 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900177 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900178 self.validate_response(schema_access.add_remove_list_flavor_access,
179 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500180 return service_client.ResponseBody(resp, body['flavor_access'])