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 |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 27 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 28 | |
| 29 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 30 | class FlavorsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 31 | |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame^] | 32 | schema_versions_info = [ |
| 33 | {'min': None, 'max': '2.54', 'schema': schema}, |
| 34 | {'min': '2.55', 'max': None, 'schema': schemav255}] |
| 35 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 36 | def list_flavors(self, detail=False, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 37 | """Lists flavors. |
| 38 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 39 | For a full list of available parameters, please refer to the official |
| 40 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 41 | https://developer.openstack.org/api-ref/compute/#list-flavors |
| 42 | https://developer.openstack.org/api-ref/compute/#list-flavors-with-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 43 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 44 | url = 'flavors' |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame^] | 45 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 46 | if detail: |
| 47 | url += '/detail' |
| 48 | _schema = schema.list_flavors_details |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame^] | 49 | else: |
| 50 | _schema = schema.list_flavors |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 51 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 60 | """Shows details for a flavor. |
| 61 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 62 | For a full list of available parameters, please refer to the official |
| 63 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 64 | https://developer.openstack.org/api-ref/compute/#show-flavor-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 65 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 66 | resp, body = self.get("flavors/%s" % flavor_id) |
| 67 | body = json.loads(body) |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame^] | 68 | schema = self.get_schema(self.schema_versions_info) |
| 69 | self.validate_response(schema.create_update_get_flavor_details, |
| 70 | resp, body) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 71 | return rest_client.ResponseBody(resp, body) |
| 72 | |
| 73 | def create_flavor(self, **kwargs): |
| 74 | """Create a new flavor or instance type. |
| 75 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 76 | For a full list of available parameters, please refer to the official |
| 77 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 78 | https://developer.openstack.org/api-ref/compute/#create-flavor |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 79 | """ |
ghanshyam | 54129d5 | 2016-12-16 09:02:15 +0900 | [diff] [blame] | 80 | if 'ephemeral' in kwargs: |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 81 | kwargs['OS-FLV-EXT-DATA:ephemeral'] = kwargs.pop('ephemeral') |
ghanshyam | 54129d5 | 2016-12-16 09:02:15 +0900 | [diff] [blame] | 82 | if 'is_public' in kwargs: |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 83 | 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) |
ghanshyam | 52c5d28 | 2018-04-23 08:43:25 +0000 | [diff] [blame^] | 89 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 108 | return rest_client.ResponseBody(resp, body) |
| 109 | |
| 110 | def delete_flavor(self, flavor_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 111 | """Delete the given flavor. |
| 112 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 113 | For a full list of available parameters, please refer to the official |
| 114 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 115 | https://developer.openstack.org/api-ref/compute/#delete-flavor |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 116 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 117 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 139 | For a full list of available parameters, please refer to the official |
| 140 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 141 | 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] | 142 | """ |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 152 | """Get extra Specs details of the mentioned flavor. |
| 153 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 154 | For a full list of available parameters, please refer to the official |
| 155 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 156 | 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] | 157 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 158 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 165 | """Get extra Specs key-value of the mentioned flavor and key. |
| 166 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 167 | For a full list of available parameters, please refer to the official |
| 168 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 169 | 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] | 170 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 171 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 182 | For a full list of available parameters, please refer to the official |
| 183 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 184 | 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] | 185 | """ |
| 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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 194 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 197 | """Unset extra Specs from the mentioned flavor. |
| 198 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 199 | For a full list of available parameters, please refer to the official |
| 200 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 201 | 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] | 202 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 203 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 209 | """Get flavor access information given the flavor id. |
| 210 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 211 | For a full list of available parameters, please refer to the official |
| 212 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 213 | 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] | 214 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 215 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 222 | """Add flavor access for the specified tenant. |
| 223 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 224 | For a full list of available parameters, please refer to the official |
| 225 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 226 | 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] | 227 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 228 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 241 | """Remove flavor access from the specified tenant. |
| 242 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 243 | For a full list of available parameters, please refer to the official |
| 244 | API reference: |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 245 | 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] | 246 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 247 | 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) |