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 |
| 24 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 25 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
| 27 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 28 | class FlavorsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 29 | |
| 30 | def list_flavors(self, detail=False, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 31 | """Lists flavors. |
| 32 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 33 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 36 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 37 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 52 | """Shows details for a flavor. |
| 53 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 54 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 57 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 58 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 66 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 69 | """ |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 83 | """Delete the given flavor. |
| 84 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 85 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 88 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 89 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 111 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 114 | """ |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 124 | """Get extra Specs details of the mentioned flavor. |
| 125 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 126 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 129 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 130 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 137 | """Get extra Specs key-value of the mentioned flavor and key. |
| 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: |
| 141 | http://developer.openstack.org/api-ref-compute-v2.1.html#showFlavorExtraSpec |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 142 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 143 | 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 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: |
| 156 | http://developer.openstack.org/api-ref-compute-v2.1.html#updateFlavorExtraSpec |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 157 | """ |
| 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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 166 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 169 | """Unset extra Specs from the mentioned flavor. |
| 170 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 171 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 174 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 175 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 181 | """Get flavor access information given the flavor id. |
| 182 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 183 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 186 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 187 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 194 | """Add flavor access for the specified tenant. |
| 195 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame^] | 196 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 199 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 200 | 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 213 | """Remove flavor access from the specified tenant. |
| 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: |
| 217 | http://developer.openstack.org/api-ref-compute-v2.1.html#removeFlavorAccess |
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 | 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) |