rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [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 | |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 16 | from lxml import etree |
| 17 | |
| 18 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 20 | from tempest.services.compute.xml.common import Document |
| 21 | from tempest.services.compute.xml.common import Element |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 22 | from tempest.services.compute.xml.common import xml_to_json |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import XMLNS_11 |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 24 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 27 | |
| 28 | class QuotasClientXML(RestClientXML): |
| 29 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 30 | def __init__(self, auth_provider): |
| 31 | super(QuotasClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 32 | self.service = CONF.compute.catalog_type |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 33 | |
| 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) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 53 | resp, body = self.get(url) |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 54 | body = xml_to_json(etree.fromstring(body)) |
| 55 | body = self._format_quota(body) |
| 56 | return resp, body |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 57 | |
Leo Toyoda | 87a52b7 | 2013-04-09 10:34:40 +0900 | [diff] [blame] | 58 | 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) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 62 | resp, body = self.get(url) |
Leo Toyoda | 87a52b7 | 2013-04-09 10:34:40 +0900 | [diff] [blame] | 63 | body = xml_to_json(etree.fromstring(body)) |
| 64 | body = self._format_quota(body) |
| 65 | return resp, body |
| 66 | |
gengjh | 07fe830 | 2013-04-17 16:15:22 +0800 | [diff] [blame] | 67 | def update_quota_set(self, tenant_id, force=None, |
| 68 | injected_file_content_bytes=None, |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 69 | metadata_items=None, ram=None, floating_ips=None, |
Leo Toyoda | d540779 | 2013-03-28 14:57:15 +0900 | [diff] [blame] | 70 | fixed_ips=None, key_pairs=None, instances=None, |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 71 | 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 | |
gengjh | 07fe830 | 2013-04-17 16:15:22 +0800 | [diff] [blame] | 80 | if force is not None: |
| 81 | post_body.add_attr('force', force) |
| 82 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 83 | 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 Toyoda | d540779 | 2013-03-28 14:57:15 +0900 | [diff] [blame] | 96 | if fixed_ips is not None: |
| 97 | post_body.add_attr('fixed_ips', fixed_ips) |
| 98 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 99 | 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), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 122 | str(Document(post_body))) |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 123 | body = xml_to_json(etree.fromstring(body)) |
| 124 | body = self._format_quota(body) |
| 125 | return resp, body |