ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [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 | import json |
| 17 | |
Marc Koderer | 6fbd74f | 2014-08-04 09:38:19 +0200 | [diff] [blame] | 18 | from tempest.api_schema.response.compute.v3 import quotas as schema |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 19 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
| 21 | |
| 22 | CONF = config.CONF |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 23 | |
| 24 | |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 25 | class QuotasV3ClientJSON(rest_client.RestClient): |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 26 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 27 | def __init__(self, auth_provider): |
| 28 | super(QuotasV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 | self.service = CONF.compute.catalog_v3_type |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 30 | |
Zhi Kun Liu | 90e4131 | 2014-03-06 14:56:01 +0800 | [diff] [blame] | 31 | def get_quota_set(self, tenant_id, user_id=None): |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 32 | """List the quota set for a tenant.""" |
| 33 | |
| 34 | url = 'os-quota-sets/%s' % str(tenant_id) |
Zhi Kun Liu | 90e4131 | 2014-03-06 14:56:01 +0800 | [diff] [blame] | 35 | if user_id: |
| 36 | url += '?user_id=%s' % str(user_id) |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 37 | resp, body = self.get(url) |
| 38 | body = json.loads(body) |
Yuiko Takada | 26c3b17 | 2014-03-19 14:03:28 +0000 | [diff] [blame] | 39 | self.validate_response(schema.quota_set, resp, body) |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 40 | return resp, body['quota_set'] |
| 41 | |
Ken'ichi Ohmichi | 776cda6 | 2014-02-18 21:21:23 +0900 | [diff] [blame] | 42 | def get_quota_set_detail(self, tenant_id): |
| 43 | """Get the quota set detail for a tenant.""" |
| 44 | |
| 45 | url = 'os-quota-sets/%s/detail' % str(tenant_id) |
| 46 | resp, body = self.get(url) |
| 47 | body = json.loads(body) |
Yuiko Takada | 2f6c17e | 2014-03-20 13:21:05 +0000 | [diff] [blame] | 48 | self.validate_response(schema.quota_set_detail, resp, body) |
Ken'ichi Ohmichi | 776cda6 | 2014-02-18 21:21:23 +0900 | [diff] [blame] | 49 | return resp, body['quota_set'] |
| 50 | |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 51 | def get_default_quota_set(self, tenant_id): |
| 52 | """List the default quota set for a tenant.""" |
| 53 | |
| 54 | url = 'os-quota-sets/%s/defaults' % str(tenant_id) |
| 55 | resp, body = self.get(url) |
| 56 | body = json.loads(body) |
Yuiko Takada | 26c3b17 | 2014-03-19 14:03:28 +0000 | [diff] [blame] | 57 | self.validate_response(schema.quota_set, resp, body) |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 58 | return resp, body['quota_set'] |
| 59 | |
Zhi Kun Liu | 27e154f | 2014-03-24 03:51:12 -0500 | [diff] [blame] | 60 | def update_quota_set(self, tenant_id, user_id=None, force=None, |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 61 | metadata_items=None, ram=None, floating_ips=None, |
| 62 | fixed_ips=None, key_pairs=None, instances=None, |
ivan-zhu | b6d69ee | 2013-12-17 14:16:31 +0800 | [diff] [blame] | 63 | security_group_rules=None, cores=None, |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 64 | security_groups=None): |
| 65 | """ |
| 66 | Updates the tenant's quota limits for one or more resources |
| 67 | """ |
| 68 | post_body = {} |
| 69 | |
| 70 | if force is not None: |
| 71 | post_body['force'] = force |
| 72 | |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 73 | if metadata_items is not None: |
| 74 | post_body['metadata_items'] = metadata_items |
| 75 | |
| 76 | if ram is not None: |
| 77 | post_body['ram'] = ram |
| 78 | |
| 79 | if floating_ips is not None: |
| 80 | post_body['floating_ips'] = floating_ips |
| 81 | |
| 82 | if fixed_ips is not None: |
| 83 | post_body['fixed_ips'] = fixed_ips |
| 84 | |
| 85 | if key_pairs is not None: |
| 86 | post_body['key_pairs'] = key_pairs |
| 87 | |
| 88 | if instances is not None: |
| 89 | post_body['instances'] = instances |
| 90 | |
| 91 | if security_group_rules is not None: |
| 92 | post_body['security_group_rules'] = security_group_rules |
| 93 | |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 94 | if cores is not None: |
| 95 | post_body['cores'] = cores |
| 96 | |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 97 | if security_groups is not None: |
| 98 | post_body['security_groups'] = security_groups |
| 99 | |
| 100 | post_body = json.dumps({'quota_set': post_body}) |
Zhi Kun Liu | 27e154f | 2014-03-24 03:51:12 -0500 | [diff] [blame] | 101 | |
| 102 | if user_id: |
| 103 | resp, body = self.put('os-quota-sets/%s?user_id=%s' % |
| 104 | (str(tenant_id), str(user_id)), post_body) |
| 105 | else: |
| 106 | resp, body = self.put('os-quota-sets/%s' % str(tenant_id), |
| 107 | post_body) |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 108 | |
| 109 | body = json.loads(body) |
Yuiko Takada | 26c3b17 | 2014-03-19 14:03:28 +0000 | [diff] [blame] | 110 | self.validate_response(schema.quota_set, resp, body) |
ivan-zhu | 2435818 | 2013-11-27 15:08:06 +0800 | [diff] [blame] | 111 | return resp, body['quota_set'] |
Yuiko Takada | d4f8652 | 2014-02-27 10:46:50 +0000 | [diff] [blame] | 112 | |
| 113 | def delete_quota_set(self, tenant_id): |
| 114 | """Delete the tenant's quota set.""" |
Yuiko Takada | a3d584d | 2014-04-03 14:11:53 +0000 | [diff] [blame] | 115 | resp, body = self.delete('os-quota-sets/%s' % str(tenant_id)) |
| 116 | self.validate_response(schema.delete_quota, resp, body) |
| 117 | return resp, body |