| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # Copyright 2012 NTT Data | 
|  | 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 | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 18 |  | 
|  | 19 | from tempest.lib.api_schema.response.compute.v2_1 import quotas as schema | 
|  | 20 | from tempest.lib.common import rest_client | 
| Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 21 | from tempest.lib.services.compute import base_compute_client | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 22 |  | 
|  | 23 |  | 
| Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 24 | class QuotasClient(base_compute_client.BaseComputeClient): | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 25 |  | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 26 | def show_quota_set(self, tenant_id, user_id=None, detail=False): | 
|  | 27 | """List the quota set for a tenant. | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 28 |  | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 29 | For a full list of available parameters, please refer to the official | 
|  | 30 | API reference: | 
| deepak_mourya | fb0b708 | 2018-01-23 17:00:27 +0530 | [diff] [blame] | 31 | https://developer.openstack.org/api-ref/compute/#show-a-quota | 
|  | 32 | https://developer.openstack.org/api-ref/compute/#show-the-detail-of-quota | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 33 | """ | 
|  | 34 |  | 
|  | 35 | params = {} | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 36 | url = 'os-quota-sets/%s' % tenant_id | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 37 | if detail: | 
|  | 38 | url += '/detail' | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 39 | if user_id: | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 40 | params.update({'user_id': user_id}) | 
|  | 41 | if params: | 
|  | 42 | url += '?%s' % urllib.urlencode(params) | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 43 | resp, body = self.get(url) | 
|  | 44 | body = json.loads(body) | 
| Felipe Monteiro | 3f52a4a | 2017-02-07 12:05:54 -0500 | [diff] [blame] | 45 | if detail: | 
|  | 46 | self.validate_response(schema.get_quota_set_details, resp, body) | 
|  | 47 | else: | 
|  | 48 | self.validate_response(schema.get_quota_set, resp, body) | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 49 | return rest_client.ResponseBody(resp, body) | 
|  | 50 |  | 
|  | 51 | def show_default_quota_set(self, tenant_id): | 
| deepak_mourya | fb0b708 | 2018-01-23 17:00:27 +0530 | [diff] [blame] | 52 | """List the default quota set for a tenant. | 
|  | 53 |  | 
|  | 54 | https://developer.openstack.org/api-ref/compute/#list-default-quotas-for-tenant | 
|  | 55 | """ | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 56 |  | 
|  | 57 | url = 'os-quota-sets/%s/defaults' % tenant_id | 
|  | 58 | resp, body = self.get(url) | 
|  | 59 | body = json.loads(body) | 
|  | 60 | self.validate_response(schema.get_quota_set, resp, body) | 
|  | 61 | return rest_client.ResponseBody(resp, body) | 
|  | 62 |  | 
|  | 63 | def update_quota_set(self, tenant_id, user_id=None, **kwargs): | 
|  | 64 | """Updates the tenant's quota limits for one or more resources. | 
|  | 65 |  | 
| OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 66 | For a full list of available parameters, please refer to the official | 
|  | 67 | API reference: | 
| zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 68 | https://developer.openstack.org/api-ref/compute/#update-quotas | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 69 | """ | 
|  | 70 |  | 
|  | 71 | post_body = json.dumps({'quota_set': kwargs}) | 
|  | 72 |  | 
|  | 73 | if user_id: | 
|  | 74 | resp, body = self.put('os-quota-sets/%s?user_id=%s' % | 
|  | 75 | (tenant_id, user_id), post_body) | 
|  | 76 | else: | 
|  | 77 | resp, body = self.put('os-quota-sets/%s' % tenant_id, | 
|  | 78 | post_body) | 
|  | 79 |  | 
|  | 80 | body = json.loads(body) | 
|  | 81 | self.validate_response(schema.update_quota_set, resp, body) | 
|  | 82 | return rest_client.ResponseBody(resp, body) | 
|  | 83 |  | 
|  | 84 | def delete_quota_set(self, tenant_id): | 
| deepak_mourya | fb0b708 | 2018-01-23 17:00:27 +0530 | [diff] [blame] | 85 | """Delete the tenant's quota set. | 
|  | 86 |  | 
|  | 87 | https://developer.openstack.org/api-ref/compute/#revert-quotas-to-defaults | 
|  | 88 | """ | 
| Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 89 | resp, body = self.delete('os-quota-sets/%s' % tenant_id) | 
|  | 90 | self.validate_response(schema.delete_quota, resp, body) | 
|  | 91 | return rest_client.ResponseBody(resp, body) |