Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | from oslo_serialization import jsonutils as json |
| 17 | from six.moves.urllib import parse as urllib |
| 18 | |
| 19 | from tempest.lib.api_schema.response.compute.v2_1 import flavors as schema |
| 20 | from tempest.lib.api_schema.response.compute.v2_1 import flavors_access \ |
| 21 | as schema_access |
| 22 | from tempest.lib.api_schema.response.compute.v2_1 import flavors_extra_specs \ |
| 23 | as schema_extra_specs |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 24 | from tempest.lib.api_schema.response.compute.v2_55 import flavors \ |
| 25 | as schemav255 |
ghanshyam | d201cd3 | 2018-04-23 09:10:11 +0000 | [diff] [blame] | 26 | from tempest.lib.api_schema.response.compute.v2_61 import flavors \ |
| 27 | as schemav261 |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 28 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 29 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 30 | |
| 31 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 32 | class FlavorsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 33 | |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 34 | schema_versions_info = [ |
| 35 | {'min': None, 'max': '2.54', 'schema': schema}, |
ghanshyam | d201cd3 | 2018-04-23 09:10:11 +0000 | [diff] [blame] | 36 | {'min': '2.55', 'max': '2.60', 'schema': schemav255}, |
| 37 | {'min': '2.61', 'max': None, 'schema': schemav261}] |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 38 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 39 | def list_flavors(self, detail=False, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 40 | """Lists flavors. |
| 41 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 42 | For a full list of available parameters, please refer to the official |
| 43 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 44 | https://developer.openstack.org/api-ref/compute/#list-flavors |
| 45 | https://developer.openstack.org/api-ref/compute/#list-flavors-with-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 46 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 47 | url = 'flavors' |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 48 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 49 | if detail: |
| 50 | url += '/detail' |
| 51 | _schema = schema.list_flavors_details |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 52 | else: |
| 53 | _schema = schema.list_flavors |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 54 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 63 | """Shows details for a flavor. |
| 64 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 65 | For a full list of available parameters, please refer to the official |
| 66 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 67 | https://developer.openstack.org/api-ref/compute/#show-flavor-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 68 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 69 | resp, body = self.get("flavors/%s" % flavor_id) |
| 70 | body = json.loads(body) |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 71 | schema = self.get_schema(self.schema_versions_info) |
| 72 | self.validate_response(schema.create_update_get_flavor_details, |
| 73 | resp, body) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 74 | return rest_client.ResponseBody(resp, body) |
| 75 | |
| 76 | def create_flavor(self, **kwargs): |
| 77 | """Create a new flavor or instance type. |
| 78 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 79 | For a full list of available parameters, please refer to the official |
| 80 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 81 | https://developer.openstack.org/api-ref/compute/#create-flavor |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 82 | """ |
ghanshyam | 54129d5 | 2016-12-16 09:02:15 +0900 | [diff] [blame] | 83 | if 'ephemeral' in kwargs: |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 84 | kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral') |
ghanshyam | 54129d5 | 2016-12-16 09:02:15 +0900 | [diff] [blame] | 85 | if 'is_public' in kwargs: |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 86 | 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) |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame] | 92 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 111 | return rest_client.ResponseBody(resp, body) |
| 112 | |
| 113 | def delete_flavor(self, flavor_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 114 | """Delete the given flavor. |
| 115 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 116 | For a full list of available parameters, please refer to the official |
| 117 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 118 | https://developer.openstack.org/api-ref/compute/#delete-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 119 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 120 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 142 | For a full list of available parameters, please refer to the official |
| 143 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 144 | https://developer.openstack.org/api-ref/compute/#create-extra-specs-for-a-flavor |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 145 | """ |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 155 | """Get extra Specs details of the mentioned flavor. |
| 156 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 157 | For a full list of available parameters, please refer to the official |
| 158 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 159 | https://developer.openstack.org/api-ref/compute/#list-extra-specs-for-a-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 160 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 161 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 168 | """Get extra Specs key-value of the mentioned flavor and key. |
| 169 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 170 | For a full list of available parameters, please refer to the official |
| 171 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 172 | https://developer.openstack.org/api-ref/compute/#show-an-extra-spec-for-a-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 173 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 174 | resp, body = self.get('flavors/%s/os-extra_specs/%s' % (flavor_id, |
afazekas | 40fcb9b | 2019-03-08 11:25:11 +0100 | [diff] [blame] | 175 | key)) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 176 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 185 | For a full list of available parameters, please refer to the official |
| 186 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 187 | https://developer.openstack.org/api-ref/compute/#update-an-extra-spec-for-a-flavor |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 188 | """ |
| 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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 197 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 200 | """Unset extra Specs from the mentioned flavor. |
| 201 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 202 | For a full list of available parameters, please refer to the official |
| 203 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 204 | https://developer.openstack.org/api-ref/compute/#delete-an-extra-spec-for-a-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 205 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 206 | resp, body = self.delete('flavors/%s/os-extra_specs/%s' % |
| 207 | (flavor_id, key)) |
ghanshyam | 10c28b8 | 2018-07-09 09:16:28 +0000 | [diff] [blame] | 208 | self.validate_response(schema_extra_specs.unset_flavor_extra_specs, |
| 209 | resp, body) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 210 | return rest_client.ResponseBody(resp, body) |
| 211 | |
| 212 | def list_flavor_access(self, flavor_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 213 | """Get flavor access information given the flavor id. |
| 214 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 215 | For a full list of available parameters, please refer to the official |
| 216 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 217 | https://developer.openstack.org/api-ref/compute/#list-flavor-access-information-for-given-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 218 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 219 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 226 | """Add flavor access for the specified tenant. |
| 227 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 228 | For a full list of available parameters, please refer to the official |
| 229 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 230 | https://developer.openstack.org/api-ref/compute/#add-flavor-access-to-tenant-addtenantaccess-action |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 231 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 232 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 245 | """Remove flavor access from the specified tenant. |
| 246 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 247 | For a full list of available parameters, please refer to the official |
| 248 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 249 | https://developer.openstack.org/api-ref/compute/#remove-flavor-access-from-tenant-removetenantaccess-action |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 250 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 251 | 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) |