blob: 4923d7ef0857245499a9215ddf64237316e07943 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2012 OpenStack Foundation
2# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import flavors as schema
20from tempest.lib.api_schema.response.compute.v2_1 import flavors_access \
21 as schema_access
22from tempest.lib.api_schema.response.compute.v2_1 import flavors_extra_specs \
23 as schema_extra_specs
ghanshyam52c5d282018-04-23 08:43:25 +000024from tempest.lib.api_schema.response.compute.v2_55 import flavors \
25 as schemav255
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026from tempest.lib.common import rest_client
Ghanshyamee9af302016-02-25 06:12:43 +090027from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050028
29
Ghanshyamee9af302016-02-25 06:12:43 +090030class FlavorsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050031
ghanshyam52c5d282018-04-23 08:43:25 +000032 schema_versions_info = [
33 {'min': None, 'max': '2.54', 'schema': schema},
34 {'min': '2.55', 'max': None, 'schema': schemav255}]
35
Matthew Treinish9e26ca82016-02-23 11:43:20 -050036 def list_flavors(self, detail=False, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080037 """Lists flavors.
38
Dong Mad12c2332016-10-19 01:36:27 -070039 For a full list of available parameters, please refer to the official
40 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080041 https://developer.openstack.org/api-ref/compute/#list-flavors
42 https://developer.openstack.org/api-ref/compute/#list-flavors-with-details
Lv Fumei7e326332016-07-08 15:18:03 +080043 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050044 url = 'flavors'
ghanshyam52c5d282018-04-23 08:43:25 +000045 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050046 if detail:
47 url += '/detail'
48 _schema = schema.list_flavors_details
ghanshyam52c5d282018-04-23 08:43:25 +000049 else:
50 _schema = schema.list_flavors
Matthew Treinish9e26ca82016-02-23 11:43:20 -050051 if params:
52 url += '?%s' % urllib.urlencode(params)
53
54 resp, body = self.get(url)
55 body = json.loads(body)
56 self.validate_response(_schema, resp, body)
57 return rest_client.ResponseBody(resp, body)
58
59 def show_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +080060 """Shows details for a flavor.
61
Dong Mad12c2332016-10-19 01:36:27 -070062 For a full list of available parameters, please refer to the official
63 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080064 https://developer.openstack.org/api-ref/compute/#show-flavor-details
Lv Fumei7e326332016-07-08 15:18:03 +080065 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050066 resp, body = self.get("flavors/%s" % flavor_id)
67 body = json.loads(body)
ghanshyam52c5d282018-04-23 08:43:25 +000068 schema = self.get_schema(self.schema_versions_info)
69 self.validate_response(schema.create_update_get_flavor_details,
70 resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050071 return rest_client.ResponseBody(resp, body)
72
73 def create_flavor(self, **kwargs):
74 """Create a new flavor or instance type.
75
Dong Mad12c2332016-10-19 01:36:27 -070076 For a full list of available parameters, please refer to the official
77 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080078 https://developer.openstack.org/api-ref/compute/#create-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -050079 """
ghanshyam54129d52016-12-16 09:02:15 +090080 if 'ephemeral' in kwargs:
Matthew Treinish9e26ca82016-02-23 11:43:20 -050081 kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral')
ghanshyam54129d52016-12-16 09:02:15 +090082 if 'is_public' in kwargs:
Matthew Treinish9e26ca82016-02-23 11:43:20 -050083 kwargs['os-flavor-access:is_public'] = kwargs.pop('is_public')
84
85 post_body = json.dumps({'flavor': kwargs})
86 resp, body = self.post('flavors', post_body)
87
88 body = json.loads(body)
ghanshyam52c5d282018-04-23 08:43:25 +000089 schema = self.get_schema(self.schema_versions_info)
90 self.validate_response(schema.create_update_get_flavor_details,
91 resp, body)
92 return rest_client.ResponseBody(resp, body)
93
94 def update_flavor(self, flavor_id, **kwargs):
95 """Uodate the flavor or instance type.
96
97 For a full list of available parameters, please refer to the official
98 API reference:
99 https://developer.openstack.org/api-ref/compute/#update-flavor-description
100 """
101 put_body = json.dumps({'flavor': kwargs})
102 resp, body = self.put("flavors/%s" % flavor_id, put_body)
103
104 body = json.loads(body)
105 schema = self.get_schema(self.schema_versions_info)
106 self.validate_response(schema.create_update_get_flavor_details,
107 resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500108 return rest_client.ResponseBody(resp, body)
109
110 def delete_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800111 """Delete the given flavor.
112
Dong Mad12c2332016-10-19 01:36:27 -0700113 For a full list of available parameters, please refer to the official
114 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800115 https://developer.openstack.org/api-ref/compute/#delete-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800116 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500117 resp, body = self.delete("flavors/{0}".format(flavor_id))
118 self.validate_response(schema.delete_flavor, resp, body)
119 return rest_client.ResponseBody(resp, body)
120
121 def is_resource_deleted(self, id):
122 # Did not use show_flavor(id) for verification as it gives
123 # 200 ok even for deleted id. LP #981263
124 # we can remove the loop here and use get by ID when bug gets sortedout
125 flavors = self.list_flavors(detail=True)['flavors']
126 for flavor in flavors:
127 if flavor['id'] == id:
128 return False
129 return True
130
131 @property
132 def resource_type(self):
133 """Return the primary type of resource this client works with."""
134 return 'flavor'
135
136 def set_flavor_extra_spec(self, flavor_id, **kwargs):
137 """Set extra Specs to the mentioned flavor.
138
Dong Mad12c2332016-10-19 01:36:27 -0700139 For a full list of available parameters, please refer to the official
140 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800141 https://developer.openstack.org/api-ref/compute/#create-extra-specs-for-a-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500142 """
143 post_body = json.dumps({'extra_specs': kwargs})
144 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
145 post_body)
146 body = json.loads(body)
147 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
148 resp, body)
149 return rest_client.ResponseBody(resp, body)
150
151 def list_flavor_extra_specs(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800152 """Get extra Specs details of the mentioned flavor.
153
Dong Mad12c2332016-10-19 01:36:27 -0700154 For a full list of available parameters, please refer to the official
155 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800156 https://developer.openstack.org/api-ref/compute/#list-extra-specs-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800157 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500158 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
159 body = json.loads(body)
160 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
161 resp, body)
162 return rest_client.ResponseBody(resp, body)
163
164 def show_flavor_extra_spec(self, flavor_id, key):
Lv Fumei7e326332016-07-08 15:18:03 +0800165 """Get extra Specs key-value of the mentioned flavor and key.
166
Dong Mad12c2332016-10-19 01:36:27 -0700167 For a full list of available parameters, please refer to the official
168 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800169 https://developer.openstack.org/api-ref/compute/#show-an-extra-spec-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800170 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500171 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id,
172 key))
173 body = json.loads(body)
174 self.validate_response(
175 schema_extra_specs.set_get_flavor_extra_specs_key,
176 resp, body)
177 return rest_client.ResponseBody(resp, body)
178
179 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
180 """Update specified extra Specs of the mentioned flavor and key.
181
Dong Mad12c2332016-10-19 01:36:27 -0700182 For a full list of available parameters, please refer to the official
183 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800184 https://developer.openstack.org/api-ref/compute/#update-an-extra-spec-for-a-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500185 """
186 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
187 (flavor_id, key), json.dumps(kwargs))
188 body = json.loads(body)
189 self.validate_response(
190 schema_extra_specs.set_get_flavor_extra_specs_key,
191 resp, body)
192 return rest_client.ResponseBody(resp, body)
193
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -0700194 def unset_flavor_extra_spec(self, flavor_id, key): # noqa
195 # NOTE: This noqa is for passing T111 check and we cannot rename
196 # to keep backwards compatibility.
Lv Fumei7e326332016-07-08 15:18:03 +0800197 """Unset extra Specs from the mentioned flavor.
198
Dong Mad12c2332016-10-19 01:36:27 -0700199 For a full list of available parameters, please refer to the official
200 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800201 https://developer.openstack.org/api-ref/compute/#delete-an-extra-spec-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800202 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500203 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
204 (flavor_id, key))
205 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
206 return rest_client.ResponseBody(resp, body)
207
208 def list_flavor_access(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800209 """Get flavor access information given the flavor id.
210
Dong Mad12c2332016-10-19 01:36:27 -0700211 For a full list of available parameters, please refer to the official
212 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800213 https://developer.openstack.org/api-ref/compute/#list-flavor-access-information-for-given-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800214 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500215 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
216 body = json.loads(body)
217 self.validate_response(schema_access.add_remove_list_flavor_access,
218 resp, body)
219 return rest_client.ResponseBody(resp, body)
220
221 def add_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800222 """Add flavor access for the specified tenant.
223
Dong Mad12c2332016-10-19 01:36:27 -0700224 For a full list of available parameters, please refer to the official
225 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800226 https://developer.openstack.org/api-ref/compute/#add-flavor-access-to-tenant-addtenantaccess-action
Lv Fumei7e326332016-07-08 15:18:03 +0800227 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500228 post_body = {
229 'addTenantAccess': {
230 'tenant': tenant_id
231 }
232 }
233 post_body = json.dumps(post_body)
234 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
235 body = json.loads(body)
236 self.validate_response(schema_access.add_remove_list_flavor_access,
237 resp, body)
238 return rest_client.ResponseBody(resp, body)
239
240 def remove_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800241 """Remove flavor access from the specified tenant.
242
Dong Mad12c2332016-10-19 01:36:27 -0700243 For a full list of available parameters, please refer to the official
244 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800245 https://developer.openstack.org/api-ref/compute/#remove-flavor-access-from-tenant-removetenantaccess-action
Lv Fumei7e326332016-07-08 15:18:03 +0800246 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500247 post_body = {
248 'removeTenantAccess': {
249 'tenant': tenant_id
250 }
251 }
252 post_body = json.dumps(post_body)
253 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
254 body = json.loads(body)
255 self.validate_response(schema_access.add_remove_list_flavor_access,
256 resp, body)
257 return rest_client.ResponseBody(resp, body)