blob: 2c32d30cc33a0ec104615d3f2b5ebf1a18fddd0a [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)
ghanshyam19973be2015-08-18 15:46:42 +090042 return service_client.ResponseBody(resp, body)
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)
ghanshyam19973be2015-08-18 15:46:42 +090048 return service_client.ResponseBody(resp, body)
Rohit Karajgi03530292012-04-24 17:00:50 -070049
Ken'ichi Ohmichi96338c42015-07-17 06:25:14 +000050 def create_flavor(self, **kwargs):
51 """Creates a new flavor or instance type.
52 Most parameters except the following are passed to the API without
53 any changes.
54 :param ephemeral: The name is changed to OS-FLV-EXT-DATA:ephemeral
55 :param is_public: The name is changed to os-flavor-access:is_public
56 """
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053057 if kwargs.get('ephemeral'):
Ken'ichi Ohmichi96338c42015-07-17 06:25:14 +000058 kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral')
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053059 if kwargs.get('is_public'):
Ken'ichi Ohmichi96338c42015-07-17 06:25:14 +000060 kwargs['os-flavor-access:is_public'] = kwargs.pop('is_public')
61
62 post_body = json.dumps({'flavor': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020063 resp, body = self.post('flavors', post_body)
Rohit Karajgi03530292012-04-24 17:00:50 -070064
65 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090066 self.validate_response(schema.create_get_flavor_details, resp, body)
ghanshyam19973be2015-08-18 15:46:42 +090067 return service_client.ResponseBody(resp, body)
Rohit Karajgi03530292012-04-24 17:00:50 -070068
69 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050070 """Deletes the given flavor."""
Ghanshyame4c3fb22014-03-24 16:07:16 +090071 resp, body = self.delete("flavors/{0}".format(flavor_id))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090072 self.validate_response(schema.delete_flavor, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050073 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053074
75 def is_resource_deleted(self, id):
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +000076 # Did not use show_flavor(id) for verification as it gives
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020077 # 200 ok even for deleted id. LP #981263
78 # we can remove the loop here and use get by ID when bug gets sortedout
ghanshyam19973be2015-08-18 15:46:42 +090079 flavors = self.list_flavors(detail=True)['flavors']
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053080 for flavor in flavors:
81 if flavor['id'] == id:
82 return False
83 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053084
Matt Riedemannd2b96512014-10-13 10:18:16 -070085 @property
86 def resource_type(self):
87 """Returns the primary type of resource this client works with."""
88 return 'flavor'
89
Ken'ichi Ohmichi6670d082015-07-17 06:56:50 +000090 def set_flavor_extra_spec(self, flavor_id, **kwargs):
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053091 """Sets extra Specs to the mentioned flavor."""
Ken'ichi Ohmichi6670d082015-07-17 06:56:50 +000092 post_body = json.dumps({'extra_specs': kwargs})
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053093 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020094 post_body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053095 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +090096 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +090097 resp, body)
ghanshyam87e74942015-08-18 16:03:30 +090098 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053099
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +0000100 def list_flavor_extra_specs(self, flavor_id):
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530101 """Gets extra Specs details of the mentioned flavor."""
102 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
103 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900104 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
Ghanshyam639d8422014-03-26 18:23:32 +0900105 resp, body)
ghanshyam87e74942015-08-18 16:03:30 +0900106 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530107
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +0000108 def show_flavor_extra_spec(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500109 """Gets extra Specs key-value of the mentioned flavor and key."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000110 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id,
lijunjbj9feaa212013-10-14 02:11:49 -0500111 key))
112 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900113 self.validate_response(
114 schema_extra_specs.set_get_flavor_extra_specs_key,
115 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500116 return service_client.ResponseBody(resp, body)
lijunjbj9feaa212013-10-14 02:11:49 -0500117
ivan-zhuae7c7c52013-10-21 22:13:22 +0800118 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500119 """Update specified extra Specs of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800120 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200121 (flavor_id, key), json.dumps(kwargs))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800122 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +0900123 self.validate_response(
124 schema_extra_specs.set_get_flavor_extra_specs_key,
125 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500126 return service_client.ResponseBody(resp, body)
ivan-zhuae7c7c52013-10-21 22:13:22 +0800127
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530128 def unset_flavor_extra_spec(self, flavor_id, key):
129 """Unsets extra Specs from the mentioned flavor."""
Ghanshyam639d8422014-03-26 18:23:32 +0900130 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000131 (flavor_id, key))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +0900132 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500133 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900134
Zhu Zhu705f24a2013-10-11 05:52:54 -0500135 def list_flavor_access(self, flavor_id):
136 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200137 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500138 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900139 self.validate_response(schema_access.add_remove_list_flavor_access,
140 resp, body)
ghanshyam87e74942015-08-18 16:03:30 +0900141 return service_client.ResponseBody(resp, body)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500142
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900143 def add_flavor_access(self, flavor_id, tenant_id):
144 """Add flavor access for the specified tenant."""
145 post_body = {
146 'addTenantAccess': {
147 'tenant': tenant_id
148 }
149 }
150 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200151 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900152 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900153 self.validate_response(schema_access.add_remove_list_flavor_access,
154 resp, body)
ghanshyam87e74942015-08-18 16:03:30 +0900155 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900156
157 def remove_flavor_access(self, flavor_id, tenant_id):
158 """Remove flavor access from the specified tenant."""
159 post_body = {
160 'removeTenantAccess': {
161 'tenant': tenant_id
162 }
163 }
164 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200165 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900166 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900167 self.validate_response(schema_access.add_remove_list_flavor_access,
168 resp, body)
ghanshyam87e74942015-08-18 16:03:30 +0900169 return service_client.ResponseBody(resp, body)