blob: f157dfbfff092edd2a686f334d2d69d05aad2520 [file] [log] [blame]
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +05301# 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
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053016from lxml import etree
17
18from tempest.common.rest_client import RestClientXML
Attila Fazekas4ba36582013-02-12 08:26:17 +010019from tempest.services.compute.xml.common import Document
20from tempest.services.compute.xml.common import Element
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053021from tempest.services.compute.xml.common import xml_to_json
Attila Fazekas4ba36582013-02-12 08:26:17 +010022from tempest.services.compute.xml.common import XMLNS_11
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053023
24
25class QuotasClientXML(RestClientXML):
26
27 def __init__(self, config, username, password, auth_url, tenant_name=None):
28 super(QuotasClientXML, self).__init__(config, username, password,
29 auth_url, tenant_name)
30 self.service = self.config.compute.catalog_type
31
32 def _format_quota(self, q):
33 quota = {}
34 for k, v in q.items():
35 try:
36 v = int(v)
37 except ValueError:
38 pass
39
40 quota[k] = v
41
42 return quota
43
44 def _parse_array(self, node):
45 return [self._format_quota(xml_to_json(x)) for x in node]
46
47 def get_quota_set(self, tenant_id):
48 """List the quota set for a tenant."""
49
50 url = 'os-quota-sets/%s' % str(tenant_id)
51 resp, body = self.get(url, self.headers)
52 body = xml_to_json(etree.fromstring(body))
53 body = self._format_quota(body)
54 return resp, body
Attila Fazekas4ba36582013-02-12 08:26:17 +010055
Leo Toyoda87a52b72013-04-09 10:34:40 +090056 def get_default_quota_set(self, tenant_id):
57 """List the default quota set for a tenant."""
58
59 url = 'os-quota-sets/%s/defaults' % str(tenant_id)
60 resp, body = self.get(url, self.headers)
61 body = xml_to_json(etree.fromstring(body))
62 body = self._format_quota(body)
63 return resp, body
64
gengjh07fe8302013-04-17 16:15:22 +080065 def update_quota_set(self, tenant_id, force=None,
66 injected_file_content_bytes=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010067 metadata_items=None, ram=None, floating_ips=None,
Leo Toyodad5407792013-03-28 14:57:15 +090068 fixed_ips=None, key_pairs=None, instances=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010069 security_group_rules=None, injected_files=None,
70 cores=None, injected_file_path_bytes=None,
71 security_groups=None):
72 """
73 Updates the tenant's quota limits for one or more resources
74 """
75 post_body = Element("quota_set",
76 xmlns=XMLNS_11)
77
gengjh07fe8302013-04-17 16:15:22 +080078 if force is not None:
79 post_body.add_attr('force', force)
80
Attila Fazekas4ba36582013-02-12 08:26:17 +010081 if injected_file_content_bytes is not None:
82 post_body.add_attr('injected_file_content_bytes',
83 injected_file_content_bytes)
84
85 if metadata_items is not None:
86 post_body.add_attr('metadata_items', metadata_items)
87
88 if ram is not None:
89 post_body.add_attr('ram', ram)
90
91 if floating_ips is not None:
92 post_body.add_attr('floating_ips', floating_ips)
93
Leo Toyodad5407792013-03-28 14:57:15 +090094 if fixed_ips is not None:
95 post_body.add_attr('fixed_ips', fixed_ips)
96
Attila Fazekas4ba36582013-02-12 08:26:17 +010097 if key_pairs is not None:
98 post_body.add_attr('key_pairs', key_pairs)
99
100 if instances is not None:
101 post_body.add_attr('instances', instances)
102
103 if security_group_rules is not None:
104 post_body.add_attr('security_group_rules', security_group_rules)
105
106 if injected_files is not None:
107 post_body.add_attr('injected_files', injected_files)
108
109 if cores is not None:
110 post_body.add_attr('cores', cores)
111
112 if injected_file_path_bytes is not None:
113 post_body.add_attr('injected_file_path_bytes',
114 injected_file_path_bytes)
115
116 if security_groups is not None:
117 post_body.add_attr('security_groups', security_groups)
118
119 resp, body = self.put('os-quota-sets/%s' % str(tenant_id),
120 str(Document(post_body)),
121 self.headers)
122 body = xml_to_json(etree.fromstring(body))
123 body = self._format_quota(body)
124 return resp, body