blob: 80cbe4dfdc57c14b398318a6904878ab6af3b6e0 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020020from tempest.api_schema.response.compute import flavors_access as schema_access
21from tempest.api_schema.response.compute import flavors_extra_specs \
Ghanshyam639d8422014-03-26 18:23:32 +090022 as schema_extra_specs
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090023from tempest.api_schema.response.compute.v2_1 import flavors as schema
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 Ohmichi4771cbc2015-01-19 23:45:23 +000027class FlavorsClientJSON(service_client.ServiceClient):
Daryl Walleck1465d612011-11-02 02:22:15 -050028
29 def list_flavors(self, params=None):
30 url = 'flavors'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050031 if params:
32 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050033
chris fattarsi5098fa22012-04-17 13:27:00 -070034 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050035 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090036 self.validate_response(schema.list_flavors, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050037 return service_client.ResponseBodyList(resp, body['flavors'])
Daryl Walleck1465d612011-11-02 02:22:15 -050038
39 def list_flavors_with_detail(self, params=None):
40 url = 'flavors/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050041 if params:
42 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050043
chris fattarsi5098fa22012-04-17 13:27:00 -070044 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050045 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090046 self.validate_response(schema.list_flavors_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050047 return service_client.ResponseBodyList(resp, body['flavors'])
Daryl Walleck1465d612011-11-02 02:22:15 -050048
49 def get_flavor_details(self, flavor_id):
chris fattarsi5098fa22012-04-17 13:27:00 -070050 resp, body = self.get("flavors/%s" % str(flavor_id))
Daryl Walleck1465d612011-11-02 02:22:15 -050051 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090052 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050053 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070054
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053055 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050056 """Creates a new flavor or instance type."""
Rohit Karajgi03530292012-04-24 17:00:50 -070057 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +090058 'name': name,
59 'ram': ram,
60 'vcpus': vcpus,
61 'disk': disk,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090062 'id': flavor_id,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090063 }
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053064 if kwargs.get('ephemeral'):
65 post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
66 if kwargs.get('swap'):
67 post_body['swap'] = kwargs.get('swap')
68 if kwargs.get('rxtx'):
69 post_body['rxtx_factor'] = kwargs.get('rxtx')
70 if kwargs.get('is_public'):
71 post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
Rohit Karajgi03530292012-04-24 17:00:50 -070072 post_body = json.dumps({'flavor': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020073 resp, body = self.post('flavors', post_body)
Rohit Karajgi03530292012-04-24 17:00:50 -070074
75 body = json.loads(body)
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090076 self.validate_response(schema.create_get_flavor_details, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050077 return service_client.ResponseBody(resp, body['flavor'])
Rohit Karajgi03530292012-04-24 17:00:50 -070078
79 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050080 """Deletes the given flavor."""
Ghanshyame4c3fb22014-03-24 16:07:16 +090081 resp, body = self.delete("flavors/{0}".format(flavor_id))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +090082 self.validate_response(schema.delete_flavor, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -050083 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053084
85 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020086 # Did not use get_flavor_details(id) for verification as it gives
87 # 200 ok even for deleted id. LP #981263
88 # we can remove the loop here and use get by ID when bug gets sortedout
David Kranz2fa77b22015-02-09 11:39:50 -050089 flavors = self.list_flavors_with_detail()
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053090 for flavor in flavors:
91 if flavor['id'] == id:
92 return False
93 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053094
Matt Riedemannd2b96512014-10-13 10:18:16 -070095 @property
96 def resource_type(self):
97 """Returns the primary type of resource this client works with."""
98 return 'flavor'
99
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530100 def set_flavor_extra_spec(self, flavor_id, specs):
101 """Sets extra Specs to the mentioned flavor."""
102 post_body = json.dumps({'extra_specs': specs})
103 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200104 post_body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530105 body = json.loads(body)
Ghanshyam639d8422014-03-26 18:23:32 +0900106 self.validate_response(schema_extra_specs.flavor_extra_specs,
107 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500108 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530109
110 def get_flavor_extra_spec(self, flavor_id):
111 """Gets extra Specs details of the mentioned flavor."""
112 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
113 body = json.loads(body)
Ghanshyam639d8422014-03-26 18:23:32 +0900114 self.validate_response(schema_extra_specs.flavor_extra_specs,
115 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500116 return service_client.ResponseBody(resp, body['extra_specs'])
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530117
lijunjbj9feaa212013-10-14 02:11:49 -0500118 def get_flavor_extra_spec_with_key(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500119 """Gets extra Specs key-value of the mentioned flavor and key."""
lijunjbj9feaa212013-10-14 02:11:49 -0500120 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
121 key))
122 body = json.loads(body)
Ghanshyam639d8422014-03-26 18:23:32 +0900123 self.validate_response(schema_extra_specs.flavor_extra_specs_key,
124 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500125 return service_client.ResponseBody(resp, body)
lijunjbj9feaa212013-10-14 02:11:49 -0500126
ivan-zhuae7c7c52013-10-21 22:13:22 +0800127 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500128 """Update specified extra Specs of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800129 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200130 (flavor_id, key), json.dumps(kwargs))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800131 body = json.loads(body)
Ghanshyam639d8422014-03-26 18:23:32 +0900132 self.validate_response(schema_extra_specs.flavor_extra_specs_key,
133 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500134 return service_client.ResponseBody(resp, body)
ivan-zhuae7c7c52013-10-21 22:13:22 +0800135
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530136 def unset_flavor_extra_spec(self, flavor_id, key):
137 """Unsets extra Specs from the mentioned flavor."""
Ghanshyam639d8422014-03-26 18:23:32 +0900138 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
139 (str(flavor_id), key))
Yuiko Takadaf9f744f2015-03-31 12:57:52 +0900140 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500141 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900142
Zhu Zhu705f24a2013-10-11 05:52:54 -0500143 def list_flavor_access(self, flavor_id):
144 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200145 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500146 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900147 self.validate_response(schema_access.add_remove_list_flavor_access,
148 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500149 return service_client.ResponseBodyList(resp, body['flavor_access'])
Zhu Zhu705f24a2013-10-11 05:52:54 -0500150
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900151 def add_flavor_access(self, flavor_id, tenant_id):
152 """Add flavor access for the specified tenant."""
153 post_body = {
154 'addTenantAccess': {
155 'tenant': tenant_id
156 }
157 }
158 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200159 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900160 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900161 self.validate_response(schema_access.add_remove_list_flavor_access,
162 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500163 return service_client.ResponseBodyList(resp, body['flavor_access'])
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900164
165 def remove_flavor_access(self, flavor_id, tenant_id):
166 """Remove flavor access from the specified tenant."""
167 post_body = {
168 'removeTenantAccess': {
169 'tenant': tenant_id
170 }
171 }
172 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200173 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900174 body = json.loads(body)
Ghanshyam95951ed2014-03-31 11:57:22 +0900175 self.validate_response(schema_access.add_remove_list_flavor_access,
176 resp, body)
David Kranz2fa77b22015-02-09 11:39:50 -0500177 return service_client.ResponseBody(resp, body['flavor_access'])