blob: 00c327578d1f374e22b5a0150745045af452b49e [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
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Attila Fazekas4ba36582013-02-12 08:26:17 +010020from tempest.services.compute.xml.common import Document
21from tempest.services.compute.xml.common import Element
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053022from tempest.services.compute.xml.common import xml_to_json
Attila Fazekas4ba36582013-02-12 08:26:17 +010023from tempest.services.compute.xml.common import XMLNS_11
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053024
Matthew Treinish684d8992014-01-30 16:27:40 +000025CONF = config.CONF
26
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053027
28class QuotasClientXML(RestClientXML):
29
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000030 def __init__(self, auth_provider):
31 super(QuotasClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000032 self.service = CONF.compute.catalog_type
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053033
34 def _format_quota(self, q):
35 quota = {}
36 for k, v in q.items():
37 try:
38 v = int(v)
39 except ValueError:
40 pass
41
42 quota[k] = v
43
44 return quota
45
46 def _parse_array(self, node):
47 return [self._format_quota(xml_to_json(x)) for x in node]
48
49 def get_quota_set(self, tenant_id):
50 """List the quota set for a tenant."""
51
52 url = 'os-quota-sets/%s' % str(tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +020053 resp, body = self.get(url)
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053054 body = xml_to_json(etree.fromstring(body))
55 body = self._format_quota(body)
56 return resp, body
Attila Fazekas4ba36582013-02-12 08:26:17 +010057
Leo Toyoda87a52b72013-04-09 10:34:40 +090058 def get_default_quota_set(self, tenant_id):
59 """List the default quota set for a tenant."""
60
61 url = 'os-quota-sets/%s/defaults' % str(tenant_id)
vponomaryovf4c27f92014-02-18 10:56:42 +020062 resp, body = self.get(url)
Leo Toyoda87a52b72013-04-09 10:34:40 +090063 body = xml_to_json(etree.fromstring(body))
64 body = self._format_quota(body)
65 return resp, body
66
gengjh07fe8302013-04-17 16:15:22 +080067 def update_quota_set(self, tenant_id, force=None,
68 injected_file_content_bytes=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010069 metadata_items=None, ram=None, floating_ips=None,
Leo Toyodad5407792013-03-28 14:57:15 +090070 fixed_ips=None, key_pairs=None, instances=None,
Attila Fazekas4ba36582013-02-12 08:26:17 +010071 security_group_rules=None, injected_files=None,
72 cores=None, injected_file_path_bytes=None,
73 security_groups=None):
74 """
75 Updates the tenant's quota limits for one or more resources
76 """
77 post_body = Element("quota_set",
78 xmlns=XMLNS_11)
79
gengjh07fe8302013-04-17 16:15:22 +080080 if force is not None:
81 post_body.add_attr('force', force)
82
Attila Fazekas4ba36582013-02-12 08:26:17 +010083 if injected_file_content_bytes is not None:
84 post_body.add_attr('injected_file_content_bytes',
85 injected_file_content_bytes)
86
87 if metadata_items is not None:
88 post_body.add_attr('metadata_items', metadata_items)
89
90 if ram is not None:
91 post_body.add_attr('ram', ram)
92
93 if floating_ips is not None:
94 post_body.add_attr('floating_ips', floating_ips)
95
Leo Toyodad5407792013-03-28 14:57:15 +090096 if fixed_ips is not None:
97 post_body.add_attr('fixed_ips', fixed_ips)
98
Attila Fazekas4ba36582013-02-12 08:26:17 +010099 if key_pairs is not None:
100 post_body.add_attr('key_pairs', key_pairs)
101
102 if instances is not None:
103 post_body.add_attr('instances', instances)
104
105 if security_group_rules is not None:
106 post_body.add_attr('security_group_rules', security_group_rules)
107
108 if injected_files is not None:
109 post_body.add_attr('injected_files', injected_files)
110
111 if cores is not None:
112 post_body.add_attr('cores', cores)
113
114 if injected_file_path_bytes is not None:
115 post_body.add_attr('injected_file_path_bytes',
116 injected_file_path_bytes)
117
118 if security_groups is not None:
119 post_body.add_attr('security_groups', security_groups)
120
121 resp, body = self.put('os-quota-sets/%s' % str(tenant_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200122 str(Document(post_body)))
Attila Fazekas4ba36582013-02-12 08:26:17 +0100123 body = xml_to_json(etree.fromstring(body))
124 body = self._format_quota(body)
125 return resp, body