blob: 4d1044b9cfa6854ad2d17018c889035bdafb0995 [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
24from tempest.lib.common import rest_client
Ghanshyamee9af302016-02-25 06:12:43 +090025from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
27
Ghanshyamee9af302016-02-25 06:12:43 +090028class FlavorsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050029
30 def list_flavors(self, detail=False, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080031 """Lists flavors.
32
Dong Mad12c2332016-10-19 01:36:27 -070033 For a full list of available parameters, please refer to the official
34 API reference:
35 http://developer.openstack.org/api-ref-compute-v2.1.html#listFlavors
Lv Fumei7e326332016-07-08 15:18:03 +080036 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050037 url = 'flavors'
38 _schema = schema.list_flavors
39
40 if detail:
41 url += '/detail'
42 _schema = schema.list_flavors_details
43 if params:
44 url += '?%s' % urllib.urlencode(params)
45
46 resp, body = self.get(url)
47 body = json.loads(body)
48 self.validate_response(_schema, resp, body)
49 return rest_client.ResponseBody(resp, body)
50
51 def show_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +080052 """Shows details for a flavor.
53
Dong Mad12c2332016-10-19 01:36:27 -070054 For a full list of available parameters, please refer to the official
55 API reference:
56 http://developer.openstack.org/api-ref-compute-v2.1.html#showFlavor
Lv Fumei7e326332016-07-08 15:18:03 +080057 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050058 resp, body = self.get("flavors/%s" % flavor_id)
59 body = json.loads(body)
60 self.validate_response(schema.create_get_flavor_details, resp, body)
61 return rest_client.ResponseBody(resp, body)
62
63 def create_flavor(self, **kwargs):
64 """Create a new flavor or instance type.
65
Dong Mad12c2332016-10-19 01:36:27 -070066 For a full list of available parameters, please refer to the official
67 API reference:
68 http://developer.openstack.org/api-ref-compute-v2.1.html#createFlavor
Matthew Treinish9e26ca82016-02-23 11:43:20 -050069 """
70 if kwargs.get('ephemeral'):
71 kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral')
72 if kwargs.get('is_public'):
73 kwargs['os-flavor-access:is_public'] = kwargs.pop('is_public')
74
75 post_body = json.dumps({'flavor': kwargs})
76 resp, body = self.post('flavors', post_body)
77
78 body = json.loads(body)
79 self.validate_response(schema.create_get_flavor_details, resp, body)
80 return rest_client.ResponseBody(resp, body)
81
82 def delete_flavor(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +080083 """Delete the given flavor.
84
Dong Mad12c2332016-10-19 01:36:27 -070085 For a full list of available parameters, please refer to the official
86 API reference:
87 http://developer.openstack.org/api-ref-compute-v2.1.html#deleteFlavor
Lv Fumei7e326332016-07-08 15:18:03 +080088 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050089 resp, body = self.delete("flavors/{0}".format(flavor_id))
90 self.validate_response(schema.delete_flavor, resp, body)
91 return rest_client.ResponseBody(resp, body)
92
93 def is_resource_deleted(self, id):
94 # Did not use show_flavor(id) for verification as it gives
95 # 200 ok even for deleted id. LP #981263
96 # we can remove the loop here and use get by ID when bug gets sortedout
97 flavors = self.list_flavors(detail=True)['flavors']
98 for flavor in flavors:
99 if flavor['id'] == id:
100 return False
101 return True
102
103 @property
104 def resource_type(self):
105 """Return the primary type of resource this client works with."""
106 return 'flavor'
107
108 def set_flavor_extra_spec(self, flavor_id, **kwargs):
109 """Set extra Specs to the mentioned flavor.
110
Dong Mad12c2332016-10-19 01:36:27 -0700111 For a full list of available parameters, please refer to the official
112 API reference:
113 http://developer.openstack.org/api-ref-compute-v2.1.html#createFlavorExtraSpec
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500114 """
115 post_body = json.dumps({'extra_specs': kwargs})
116 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
117 post_body)
118 body = json.loads(body)
119 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
120 resp, body)
121 return rest_client.ResponseBody(resp, body)
122
123 def list_flavor_extra_specs(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800124 """Get extra Specs details of the mentioned flavor.
125
Dong Mad12c2332016-10-19 01:36:27 -0700126 For a full list of available parameters, please refer to the official
127 API reference:
128 http://developer.openstack.org/api-ref-compute-v2.1.html#listFlavorExtraSpecs
Lv Fumei7e326332016-07-08 15:18:03 +0800129 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500130 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
131 body = json.loads(body)
132 self.validate_response(schema_extra_specs.set_get_flavor_extra_specs,
133 resp, body)
134 return rest_client.ResponseBody(resp, body)
135
136 def show_flavor_extra_spec(self, flavor_id, key):
Lv Fumei7e326332016-07-08 15:18:03 +0800137 """Get extra Specs key-value of the mentioned flavor and key.
138
Dong Mad12c2332016-10-19 01:36:27 -0700139 For a full list of available parameters, please refer to the official
140 API reference:
141 http://developer.openstack.org/api-ref-compute-v2.1.html#showFlavorExtraSpec
Lv Fumei7e326332016-07-08 15:18:03 +0800142 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500143 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id,
144 key))
145 body = json.loads(body)
146 self.validate_response(
147 schema_extra_specs.set_get_flavor_extra_specs_key,
148 resp, body)
149 return rest_client.ResponseBody(resp, body)
150
151 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
152 """Update specified extra Specs of the mentioned flavor and key.
153
Dong Mad12c2332016-10-19 01:36:27 -0700154 For a full list of available parameters, please refer to the official
155 API reference:
156 http://developer.openstack.org/api-ref-compute-v2.1.html#updateFlavorExtraSpec
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500157 """
158 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
159 (flavor_id, key), json.dumps(kwargs))
160 body = json.loads(body)
161 self.validate_response(
162 schema_extra_specs.set_get_flavor_extra_specs_key,
163 resp, body)
164 return rest_client.ResponseBody(resp, body)
165
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -0700166 def unset_flavor_extra_spec(self, flavor_id, key): # noqa
167 # NOTE: This noqa is for passing T111 check and we cannot rename
168 # to keep backwards compatibility.
Lv Fumei7e326332016-07-08 15:18:03 +0800169 """Unset extra Specs from the mentioned flavor.
170
Dong Mad12c2332016-10-19 01:36:27 -0700171 For a full list of available parameters, please refer to the official
172 API reference:
173 http://developer.openstack.org/api-ref-compute-v2.1.html#deleteFlavorExtraSpec
Lv Fumei7e326332016-07-08 15:18:03 +0800174 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500175 resp, body = self.delete('flavors/%s/os-extra_specs/%s' %
176 (flavor_id, key))
177 self.validate_response(schema.unset_flavor_extra_specs, resp, body)
178 return rest_client.ResponseBody(resp, body)
179
180 def list_flavor_access(self, flavor_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800181 """Get flavor access information given the flavor id.
182
Dong Mad12c2332016-10-19 01:36:27 -0700183 For a full list of available parameters, please refer to the official
184 API reference:
185 http://developer.openstack.org/api-ref-compute-v2.1.html#listFlavorAccess
Lv Fumei7e326332016-07-08 15:18:03 +0800186 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500187 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
188 body = json.loads(body)
189 self.validate_response(schema_access.add_remove_list_flavor_access,
190 resp, body)
191 return rest_client.ResponseBody(resp, body)
192
193 def add_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800194 """Add flavor access for the specified tenant.
195
Dong Mad12c2332016-10-19 01:36:27 -0700196 For a full list of available parameters, please refer to the official
197 API reference:
198 http://developer.openstack.org/api-ref-compute-v2.1.html#addFlavorAccess
Lv Fumei7e326332016-07-08 15:18:03 +0800199 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500200 post_body = {
201 'addTenantAccess': {
202 'tenant': tenant_id
203 }
204 }
205 post_body = json.dumps(post_body)
206 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
207 body = json.loads(body)
208 self.validate_response(schema_access.add_remove_list_flavor_access,
209 resp, body)
210 return rest_client.ResponseBody(resp, body)
211
212 def remove_flavor_access(self, flavor_id, tenant_id):
Lv Fumei7e326332016-07-08 15:18:03 +0800213 """Remove flavor access from the specified tenant.
214
Dong Mad12c2332016-10-19 01:36:27 -0700215 For a full list of available parameters, please refer to the official
216 API reference:
217 http://developer.openstack.org/api-ref-compute-v2.1.html#removeFlavorAccess
Lv Fumei7e326332016-07-08 15:18:03 +0800218 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500219 post_body = {
220 'removeTenantAccess': {
221 'tenant': tenant_id
222 }
223 }
224 post_body = json.dumps(post_body)
225 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
226 body = json.loads(body)
227 self.validate_response(schema_access.add_remove_list_flavor_access,
228 resp, body)
229 return rest_client.ResponseBody(resp, body)