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 |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 19 | from tempest.services.compute.xml.common import Document |
| 20 | from tempest.services.compute.xml.common import Element |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 21 | from tempest.services.compute.xml.common import xml_to_json |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 22 | from tempest.services.compute.xml.common import XMLNS_11 |
rajalakshmi-ganesan | 1982c3c | 2013-01-10 14:56:45 +0530 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class 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 Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 55 | |
Leo Toyoda | 87a52b7 | 2013-04-09 10:34:40 +0900 | [diff] [blame] | 56 | 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 | |
gengjh | 07fe830 | 2013-04-17 16:15:22 +0800 | [diff] [blame] | 65 | def update_quota_set(self, tenant_id, force=None, |
| 66 | injected_file_content_bytes=None, |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 67 | metadata_items=None, ram=None, floating_ips=None, |
Leo Toyoda | d540779 | 2013-03-28 14:57:15 +0900 | [diff] [blame] | 68 | fixed_ips=None, key_pairs=None, instances=None, |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 69 | 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 | |
gengjh | 07fe830 | 2013-04-17 16:15:22 +0800 | [diff] [blame] | 78 | if force is not None: |
| 79 | post_body.add_attr('force', force) |
| 80 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 81 | 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 Toyoda | d540779 | 2013-03-28 14:57:15 +0900 | [diff] [blame] | 94 | if fixed_ips is not None: |
| 95 | post_body.add_attr('fixed_ips', fixed_ips) |
| 96 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 97 | 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 |