blob: b691529a2aa01458ee90655923608899a9a1aaf6 [file] [log] [blame]
Rohit Karajgi07599c52012-11-02 05:35:16 -07001# 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
16import json
17
Marc Koderer6fbd74f2014-08-04 09:38:19 +020018from tempest.api_schema.response.compute.v2\
19 import quota_classes as classes_schema
20from tempest.api_schema.response.compute.v2 import quotas as schema
Haiwei Xuab924622014-03-05 04:33:51 +090021from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
23
24CONF = config.CONF
Rohit Karajgi07599c52012-11-02 05:35:16 -070025
26
Haiwei Xuab924622014-03-05 04:33:51 +090027class QuotasClientJSON(rest_client.RestClient):
Rohit Karajgi07599c52012-11-02 05:35:16 -070028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(QuotasClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Rohit Karajgi07599c52012-11-02 05:35:16 -070032
Zhi Kun Liu90e41312014-03-06 14:56:01 +080033 def get_quota_set(self, tenant_id, user_id=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050034 """List the quota set for a tenant."""
Rohit Karajgi07599c52012-11-02 05:35:16 -070035
36 url = 'os-quota-sets/%s' % str(tenant_id)
Zhi Kun Liu90e41312014-03-06 14:56:01 +080037 if user_id:
38 url += '?user_id=%s' % str(user_id)
Rohit Karajgi07599c52012-11-02 05:35:16 -070039 resp, body = self.get(url)
40 body = json.loads(body)
Yuiko Takada26c3b172014-03-19 14:03:28 +000041 self.validate_response(schema.quota_set, resp, body)
Rohit Karajgi07599c52012-11-02 05:35:16 -070042 return resp, body['quota_set']
Attila Fazekas4ba36582013-02-12 08:26:17 +010043
Leo Toyoda87a52b72013-04-09 10:34:40 +090044 def get_default_quota_set(self, tenant_id):
45 """List the default quota set for a tenant."""
46
47 url = 'os-quota-sets/%s/defaults' % str(tenant_id)
48 resp, body = self.get(url)
49 body = json.loads(body)
Yuiko Takada26c3b172014-03-19 14:03:28 +000050 self.validate_response(schema.quota_set, resp, body)
Leo Toyoda87a52b72013-04-09 10:34:40 +090051 return resp, body['quota_set']
52
Zhi Kun Liu27e154f2014-03-24 03:51:12 -050053 def update_quota_set(self, tenant_id, user_id=None,
54 force=None, injected_file_content_bytes=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010055 metadata_items=None, ram=None, floating_ips=None,
Leo Toyodad5407792013-03-28 14:57:15 +090056 fixed_ips=None, key_pairs=None, instances=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010057 security_group_rules=None, injected_files=None,
58 cores=None, injected_file_path_bytes=None,
59 security_groups=None):
60 """
61 Updates the tenant's quota limits for one or more resources
62 """
63 post_body = {}
64
gengjh07fe8302013-04-17 16:15:22 +080065 if force is not None:
66 post_body['force'] = force
67
Attila Fazekas4ba36582013-02-12 08:26:17 +010068 if injected_file_content_bytes is not None:
69 post_body['injected_file_content_bytes'] = \
70 injected_file_content_bytes
71
72 if metadata_items is not None:
73 post_body['metadata_items'] = metadata_items
74
75 if ram is not None:
76 post_body['ram'] = ram
77
78 if floating_ips is not None:
79 post_body['floating_ips'] = floating_ips
80
Leo Toyodad5407792013-03-28 14:57:15 +090081 if fixed_ips is not None:
82 post_body['fixed_ips'] = fixed_ips
83
Attila Fazekas4ba36582013-02-12 08:26:17 +010084 if key_pairs is not None:
85 post_body['key_pairs'] = key_pairs
86
87 if instances is not None:
88 post_body['instances'] = instances
89
90 if security_group_rules is not None:
91 post_body['security_group_rules'] = security_group_rules
92
93 if injected_files is not None:
94 post_body['injected_files'] = injected_files
95
96 if cores is not None:
97 post_body['cores'] = cores
98
99 if injected_file_path_bytes is not None:
100 post_body['injected_file_path_bytes'] = injected_file_path_bytes
101
102 if security_groups is not None:
103 post_body['security_groups'] = security_groups
104
105 post_body = json.dumps({'quota_set': post_body})
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500106
107 if user_id:
108 resp, body = self.put('os-quota-sets/%s?user_id=%s' %
109 (str(tenant_id), str(user_id)), post_body)
110 else:
111 resp, body = self.put('os-quota-sets/%s' % str(tenant_id),
112 post_body)
Attila Fazekas4ba36582013-02-12 08:26:17 +0100113
114 body = json.loads(body)
Yuiko Takada3657ae82014-03-25 09:52:15 +0000115 self.validate_response(schema.quota_set_update, resp, body)
Attila Fazekas4ba36582013-02-12 08:26:17 +0100116 return resp, body['quota_set']
Yuiko Takada2209cf52014-02-27 12:03:49 +0000117
118 def delete_quota_set(self, tenant_id):
119 """Delete the tenant's quota set."""
Yuiko Takadaa3d584d2014-04-03 14:11:53 +0000120 resp, body = self.delete('os-quota-sets/%s' % str(tenant_id))
121 self.validate_response(schema.delete_quota, resp, body)
122 return resp, body
Matt Riedemann848805f2014-06-16 13:23:51 -0700123
124
125class QuotaClassesClientJSON(rest_client.RestClient):
126
127 def __init__(self, auth_provider):
128 super(QuotaClassesClientJSON, self).__init__(auth_provider)
129 self.service = CONF.compute.catalog_type
130
131 def get_quota_class_set(self, quota_class_id):
132 """List the quota class set for a quota class."""
133
134 url = 'os-quota-class-sets/%s' % str(quota_class_id)
135 resp, body = self.get(url)
136 body = json.loads(body)
137 self.validate_response(classes_schema.quota_set, resp, body)
138 return resp, body['quota_class_set']
139
140 def update_quota_class_set(self, quota_class_id, **kwargs):
141 """
142 Updates the quota class's limits for one or more resources.
143 """
144 post_body = json.dumps({'quota_class_set': kwargs})
145
146 resp, body = self.put('os-quota-class-sets/%s' % str(quota_class_id),
147 post_body)
148
149 body = json.loads(body)
150 self.validate_response(classes_schema.quota_set_update, resp, body)
151 return resp, body['quota_class_set']