blob: 85e481cf509940e6a45777e417c518fd5a13e9ba [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
vponomaryov960eeb42014-02-22 18:25:25 +020018from tempest.common import rest_client
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
vponomaryov960eeb42014-02-22 18:25:25 +020028class QuotasClientXML(rest_client.RestClient):
29 TYPE = "xml"
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(QuotasClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053034
35 def _format_quota(self, q):
36 quota = {}
37 for k, v in q.items():
38 try:
39 v = int(v)
40 except ValueError:
41 pass
42
43 quota[k] = v
44
45 return quota
46
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053047 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020051 resp, body = self.get(url)
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +053052 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020060 resp, body = self.get(url)
Leo Toyoda87a52b72013-04-09 10:34:40 +090061 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),
vponomaryovf4c27f92014-02-18 10:56:42 +0200120 str(Document(post_body)))
Attila Fazekas4ba36582013-02-12 08:26:17 +0100121 body = xml_to_json(etree.fromstring(body))
122 body = self._format_quota(body)
123 return resp, body
Yuiko Takada2209cf52014-02-27 12:03:49 +0000124
125 def delete_quota_set(self, tenant_id):
126 """Delete the tenant's quota set."""
127 return self.delete('os-quota-sets/%s' % str(tenant_id))