blob: b928f9f665c089b6610bcc39aabc694f15a0b019 [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
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
Daryl Walleck1465d612011-11-02 02:22:15 -050018
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090019from tempest.api_schema.response.compute.v2_1 import flavors as schema
ghanshyam59869d02015-04-22 17:23:08 +090020from tempest.api_schema.response.compute.v2_1 import flavors_access \
21 as schema_access
22from tempest.api_schema.response.compute.v2_1 import flavors_extra_specs \
23 as schema_extra_specs
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000024from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025
Daryl Walleck1465d612011-11-02 02:22:15 -050026
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000027class FlavorsClient(service_client.ServiceClient):
Daryl Walleck1465d612011-11-02 02:22:15 -050028
Ken'ichi Ohmichi91509682015-06-17 03:05:07 +000029 def list_flavors(self, detail=False, **params):
Daryl Walleck1465d612011-11-02 02:22:15 -050030 url = 'flavors'
Ken'ichi Ohmichi91509682015-06-17 03:05:07 +000031 _schema = schema.list_flavors
32
33 if detail:
34 url += '/detail'
35 _schema = schema.list_flavors_details
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050036 if params:
37 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050038
chris fattarsi5098fa22012-04-17 13:27:00 -070039 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050040 body = json.loads(body)
Ken'ichi Ohmichi91509682015-06-17 03:05:07 +000041 self.validate_response(_schema, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050042 return service_client.ResponseBodyList(resp, body['flavors'])
Daryl Walleck1465d612011-11-02 02:22:15 -050043
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +000044 def show_flavor(self, flavor_id):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000045 resp, body = self.get("flavors/%s" % flavor_id)
Daryl Walleck1465d612011-11-02 02:22:15 -050046 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090047 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050048 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070049
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053050 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050051 """Creates a new flavor or instance type."""
Rohit Karajgi03530292012-04-24 17:00:50 -070052 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +090053 'name': name,
54 'ram': ram,
55 'vcpus': vcpus,
56 'disk': disk,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090057 'id': flavor_id,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090058 }
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053059 if kwargs.get('ephemeral'):
60 post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
61 if kwargs.get('swap'):
62 post_body['swap'] = kwargs.get('swap')
63 if kwargs.get('rxtx'):
64 post_body['rxtx_factor'] = kwargs.get('rxtx')
65 if kwargs.get('is_public'):
66 post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
Rohit Karajgi03530292012-04-24 17:00:50 -070067 post_body = json.dumps({'flavor': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020068 resp, body = self.post('flavors', post_body)
Rohit Karajgi03530292012-04-24 17:00:50 -070069
70 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090071 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050072 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070073
74 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050075 """Deletes the given flavor."""
Ghanshyame4c3fb22014-03-24 16:07:16 +090076 resp, body = self.delete("flavors/{0}".format(flavor_id))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090077 self.validate_response(schema.delete_flavor, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050078 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053079
80 def is_resource_deleted(self, id):
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +000081 # Did not use show_flavor(id) for verification as it gives
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020082 # 200 ok even for deleted id. LP #981263
83 # we can remove the loop here and use get by ID when bug gets sortedout
Ken'ichi Ohmichi91509682015-06-17 03:05:07 +000084 flavors = self.list_flavors(detail=True)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053085 for flavor in flavors:
86 if flavor['id'] == id:
87 return False
88 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053089
Matt Riedemannd2b96512014-10-13 10:18:16 -070090 @property
91 def resource_type(self):
92 """Returns the primary type of resource this client works with."""
93 return 'flavor'
94
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053095 def set_flavor_extra_spec(self, flavor_id, specs):
96 """Sets extra Specs to the mentioned flavor."""
97 post_body = json.dumps({'extra_specs': specs})
98 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020099 post_body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530100 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900101 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +0900102 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500103 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530104
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +0000105 def list_flavor_extra_specs(self, flavor_id):
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530106 """Gets extra Specs details of the mentioned flavor."""
107 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
108 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900109 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +0900110 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500111 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530112
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +0000113 def show_flavor_extra_spec(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500114 """Gets extra Specs key-value of the mentioned flavor and key."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000115 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id,
lijunjbj9feaa212013-10-14 02:11:49 -0500116 key))
117 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900118 self.validate_response(
119 schema_extra_specs.set_get_flavor_extra_specs_key,
120 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500121 return service_client.ResponseBody(resp, body)
lijunjbj9feaa212013-10-14 02:11:49 -0500122
ivan-zhuae7c7c52013-10-21 22:13:22 +0800123 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500124 """Update specified extra Specs of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800125 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200126 (flavor_id, key), json.dumps(kwargs))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800127 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900128 self.validate_response(
129 schema_extra_specs.set_get_flavor_extra_specs_key,
130 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500131 return service_client.ResponseBody(resp, body)
ivan-zhuae7c7c52013-10-21 22:13:22 +0800132
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530133 def unset_flavor_extra_spec(self, flavor_id, key):
134 """Unsets extra Specs from the mentioned flavor."""
Ghanshyam639d8422014-03-26 18:23:32 +0900135 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000136 (flavor_id, key))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +0900137 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500138 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900139
Zhu Zhu705f24a2013-10-11 05:52:54 -0500140 def list_flavor_access(self, flavor_id):
141 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200142 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500143 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900144 self.validate_response(schema_access.add_remove_list_flavor_access,
145 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500146 return service_client.ResponseBodyList(resp, body['flavor_access'])
Zhu Zhu705f24a2013-10-11 05:52:54 -0500147
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900148 def add_flavor_access(self, flavor_id, tenant_id):
149 """Add flavor access for the specified tenant."""
150 post_body = {
151 'addTenantAccess': {
152 'tenant': tenant_id
153 }
154 }
155 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200156 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900157 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900158 self.validate_response(schema_access.add_remove_list_flavor_access,
159 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500160 return service_client.ResponseBodyList(resp, body['flavor_access'])
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900161
162 def remove_flavor_access(self, flavor_id, tenant_id):
163 """Remove flavor access from the specified tenant."""
164 post_body = {
165 'removeTenantAccess': {
166 'tenant': tenant_id
167 }
168 }
169 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200170 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900171 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900172 self.validate_response(schema_access.add_remove_list_flavor_access,
173 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500174 return service_client.ResponseBody(resp, body['flavor_access'])