blob: 5d2dd469b2a5d08f2921794f62364a7618060bf7 [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
ghanshyamd201cd32018-04-23 09:10:11 +000026from tempest.lib.api_schema.response.compute.v2_61 import flavors \
27 as schemav261
Matthew Treinish9e26ca82016-02-23 11:43:20 -050028from tempest.lib.common import rest_client
Ghanshyamee9af302016-02-25 06:12:43 +090029from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050030
31
Ghanshyamee9af302016-02-25 06:12:43 +090032class FlavorsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050033
ghanshyam52c5d282018-04-23 08:43:25 +000034 schema_versions_info = [
35 {'min': None, 'max': '2.54', 'schema': schema},
ghanshyamd201cd32018-04-23 09:10:11 +000036 {'min': '2.55', 'max': '2.60', 'schema': schemav255},
37 {'min': '2.61', 'max': None, 'schema': schemav261}]
ghanshyam52c5d282018-04-23 08:43:25 +000038
Matthew Treinish9e26ca82016-02-23 11:43:20 -050039 def list_flavors(self, detail=False, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080040 """Lists flavors.
41
Dong Mad12c2332016-10-19 01:36:27 -070042 For a full list of available parameters, please refer to the official
43 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080044 https://developer.openstack.org/api-ref/compute/#list-flavors
45 https://developer.openstack.org/api-ref/compute/#list-flavors-with-details
Lv Fumei7e326332016-07-08 15:18:03 +080046 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050047 url = 'flavors'
ghanshyam52c5d282018-04-23 08:43:25 +000048 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050049 if detail:
50 url += '/detail'
51 _schema = schema.list_flavors_details
ghanshyam52c5d282018-04-23 08:43:25 +000052 else:
53 _schema = schema.list_flavors
Matthew Treinish9e26ca82016-02-23 11:43:20 -050054 if params:
55 url += '?%s' % urllib.urlencode(params)
56
57 resp, body = self.get(url)
58 body = json.loads(body)
59 self.validate_response(_schema, resp, body)
60 return rest_client.ResponseBody(resp, body)
61
62 def show_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +080063 """Shows details for a flavor.
64
Dong Mad12c2332016-10-19 01:36:27 -070065 For a full list of available parameters, please refer to the official
66 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080067 https://developer.openstack.org/api-ref/compute/#show-flavor-details
Lv Fumei7e326332016-07-08 15:18:03 +080068 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050069 resp, body = self.get("flavors/%s" % flavor_id)
70 body = json.loads(body)
ghanshyam52c5d282018-04-23 08:43:25 +000071 schema = self.get_schema(self.schema_versions_info)
72 self.validate_response(schema.create_update_get_flavor_details,
73 resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050074 return rest_client.ResponseBody(resp, body)
75
76 def create_flavor(self, **kwargs):
77 """Create a new flavor or instance type.
78
Dong Mad12c2332016-10-19 01:36:27 -070079 For a full list of available parameters, please refer to the official
80 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080081 https://developer.openstack.org/api-ref/compute/#create-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -050082 """
ghanshyam54129d52016-12-16 09:02:15 +090083 if 'ephemeral' in kwargs:
Matthew Treinish9e26ca82016-02-23 11:43:20 -050084 kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral')
ghanshyam54129d52016-12-16 09:02:15 +090085 if 'is_public' in kwargs:
Matthew Treinish9e26ca82016-02-23 11:43:20 -050086 kwargs['os-flavor-access:is_public'] = kwargs.pop('is_public')
87
88 post_body = json.dumps({'flavor': kwargs})
89 resp, body = self.post('flavors', post_body)
90
91 body = json.loads(body)
ghanshyam52c5d282018-04-23 08:43:25 +000092 schema = self.get_schema(self.schema_versions_info)
93 self.validate_response(schema.create_update_get_flavor_details,
94 resp, body)
95 return rest_client.ResponseBody(resp, body)
96
97 def update_flavor(self, flavor_id, **kwargs):
98 """Uodate the flavor or instance type.
99
100 For a full list of available parameters, please refer to the official
101 API reference:
102 https://developer.openstack.org/api-ref/compute/#update-flavor-description
103 """
104 put_body = json.dumps({'flavor': kwargs})
105 resp, body = self.put("flavors/%s" % flavor_id, put_body)
106
107 body = json.loads(body)
108 schema = self.get_schema(self.schema_versions_info)
109 self.validate_response(schema.create_update_get_flavor_details,
110 resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500111 return rest_client.ResponseBody(resp, body)
112
113 def delete_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800114 """Delete the given flavor.
115
Dong Mad12c2332016-10-19 01:36:27 -0700116 For a full list of available parameters, please refer to the official
117 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800118 https://developer.openstack.org/api-ref/compute/#delete-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800119 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500120 resp, body = self.delete("flavors/{0}".format(flavor_id))
121 self.validate_response(schema.delete_flavor, resp, body)
122 return rest_client.ResponseBody(resp, body)
123
124 def is_resource_deleted(self, id):
125 # Did not use show_flavor(id) for verification as it gives
126 # 200 ok even for deleted id. LP #981263
127 # we can remove the loop here and use get by ID when bug gets sortedout
128 flavors = self.list_flavors(detail=True)['flavors']
129 for flavor in flavors:
130 if flavor['id'] == id:
131 return False
132 return True
133
134 @property
135 def resource_type(self):
136 """Return the primary type of resource this client works with."""
137 return 'flavor'
138
139 def set_flavor_extra_spec(self, flavor_id, **kwargs):
140 """Set extra Specs to the mentioned flavor.
141
Dong Mad12c2332016-10-19 01:36:27 -0700142 For a full list of available parameters, please refer to the official
143 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800144 https://developer.openstack.org/api-ref/compute/#create-extra-specs-for-a-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500145 """
146 post_body = json.dumps({'extra_specs': kwargs})
147 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
148 post_body)
149 body = json.loads(body)
150 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
151 resp, body)
152 return rest_client.ResponseBody(resp, body)
153
154 def list_flavor_extra_specs(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800155 """Get extra Specs details of the mentioned flavor.
156
Dong Mad12c2332016-10-19 01:36:27 -0700157 For a full list of available parameters, please refer to the official
158 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800159 https://developer.openstack.org/api-ref/compute/#list-extra-specs-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800160 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500161 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
162 body = json.loads(body)
163 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
164 resp, body)
165 return rest_client.ResponseBody(resp, body)
166
167 def show_flavor_extra_spec(self, flavor_id, key):
Lv Fumei7e326332016-07-08 15:18:03 +0800168 """Get extra Specs key-value of the mentioned flavor and key.
169
Dong Mad12c2332016-10-19 01:36:27 -0700170 For a full list of available parameters, please refer to the official
171 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800172 https://developer.openstack.org/api-ref/compute/#show-an-extra-spec-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800173 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500174 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id,
afazekas40fcb9b2019-03-08 11:25:11 +0100175 key))
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500176 body = json.loads(body)
177 self.validate_response(
178 schema_extra_specs.set_get_flavor_extra_specs_key,
179 resp, body)
180 return rest_client.ResponseBody(resp, body)
181
182 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
183 """Update specified extra Specs of the mentioned flavor and key.
184
Dong Mad12c2332016-10-19 01:36:27 -0700185 For a full list of available parameters, please refer to the official
186 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800187 https://developer.openstack.org/api-ref/compute/#update-an-extra-spec-for-a-flavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500188 """
189 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
190 (flavor_id, key), json.dumps(kwargs))
191 body = json.loads(body)
192 self.validate_response(
193 schema_extra_specs.set_get_flavor_extra_specs_key,
194 resp, body)
195 return rest_client.ResponseBody(resp, body)
196
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -0700197 def unset_flavor_extra_spec(self, flavor_id, key): # noqa
198 # NOTE: This noqa is for passing T111 check and we cannot rename
199 # to keep backwards compatibility.
Lv Fumei7e326332016-07-08 15:18:03 +0800200 """Unset extra Specs from the mentioned flavor.
201
Dong Mad12c2332016-10-19 01:36:27 -0700202 For a full list of available parameters, please refer to the official
203 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800204 https://developer.openstack.org/api-ref/compute/#delete-an-extra-spec-for-a-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800205 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500206 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
207 (flavor_id, key))
ghanshyam10c28b82018-07-09 09:16:28 +0000208 self.validate_response(schema_extra_specs.unset_flavor_extra_specs,
209 resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500210 return rest_client.ResponseBody(resp, body)
211
212 def list_flavor_access(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800213 """Get flavor access information given the flavor id.
214
Dong Mad12c2332016-10-19 01:36:27 -0700215 For a full list of available parameters, please refer to the official
216 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800217 https://developer.openstack.org/api-ref/compute/#list-flavor-access-information-for-given-flavor
Lv Fumei7e326332016-07-08 15:18:03 +0800218 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500219 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
220 body = json.loads(body)
221 self.validate_response(schema_access.add_remove_list_flavor_access,
222 resp, body)
223 return rest_client.ResponseBody(resp, body)
224
225 def add_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800226 """Add flavor access for the specified tenant.
227
Dong Mad12c2332016-10-19 01:36:27 -0700228 For a full list of available parameters, please refer to the official
229 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800230 https://developer.openstack.org/api-ref/compute/#add-flavor-access-to-tenant-addtenantaccess-action
Lv Fumei7e326332016-07-08 15:18:03 +0800231 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500232 post_body = {
233 'addTenantAccess': {
234 'tenant': tenant_id
235 }
236 }
237 post_body = json.dumps(post_body)
238 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
239 body = json.loads(body)
240 self.validate_response(schema_access.add_remove_list_flavor_access,
241 resp, body)
242 return rest_client.ResponseBody(resp, body)
243
244 def remove_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800245 """Remove flavor access from the specified tenant.
246
Dong Mad12c2332016-10-19 01:36:27 -0700247 For a full list of available parameters, please refer to the official
248 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +0800249 https://developer.openstack.org/api-ref/compute/#remove-flavor-access-from-tenant-removetenantaccess-action
Lv Fumei7e326332016-07-08 15:18:03 +0800250 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500251 post_body = {
252 'removeTenantAccess': {
253 'tenant': tenant_id
254 }
255 }
256 post_body = json.dumps(post_body)
257 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
258 body = json.loads(body)
259 self.validate_response(schema_access.add_remove_list_flavor_access,
260 resp, body)
261 return rest_client.ResponseBody(resp, body)