ravikumar-venkatesan | 3052e94 | 2014-05-12 18:25:17 +0000 | [diff] [blame^] | 1 | # Copyright 2014 OpenStack Foundation |
| 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 | |
| 16 | import urllib |
| 17 | |
| 18 | from lxml import etree |
| 19 | |
| 20 | from tempest.common import http |
| 21 | from tempest.common import rest_client |
| 22 | from tempest.common import xml_utils as common |
| 23 | from tempest import config |
| 24 | |
| 25 | CONF = config.CONF |
| 26 | |
| 27 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 28 | |
| 29 | |
| 30 | class RegionClientXML(rest_client.RestClient): |
| 31 | TYPE = "xml" |
| 32 | |
| 33 | def __init__(self, auth_provider): |
| 34 | super(RegionClientXML, self).__init__(auth_provider) |
| 35 | self.service = CONF.identity.catalog_type |
| 36 | self.region_url = 'adminURL' |
| 37 | self.api_version = "v3" |
| 38 | |
| 39 | def _parse_array(self, node): |
| 40 | array = [] |
| 41 | for child in node.getchildren(): |
| 42 | tag_list = child.tag.split('}', 1) |
| 43 | if tag_list[1] == "region": |
| 44 | array.append(common.xml_to_json(child)) |
| 45 | return array |
| 46 | |
| 47 | def _parse_body(self, body): |
| 48 | json = common.xml_to_json(body) |
| 49 | return json |
| 50 | |
| 51 | def request(self, method, url, extra_headers=False, headers=None, |
| 52 | body=None, wait=None): |
| 53 | """Overriding the existing HTTP request in super class RestClient.""" |
| 54 | if extra_headers: |
| 55 | try: |
| 56 | headers.update(self.get_headers()) |
| 57 | except (ValueError, TypeError): |
| 58 | headers = self.get_headers() |
| 59 | dscv = CONF.identity.disable_ssl_certificate_validation |
| 60 | self.http_obj = http.ClosingHttp( |
| 61 | disable_ssl_certificate_validation=dscv) |
| 62 | return super(RegionClientXML, self).request(method, url, |
| 63 | extra_headers, |
| 64 | headers=headers, |
| 65 | body=body) |
| 66 | |
| 67 | def create_region(self, description, **kwargs): |
| 68 | """Create region.""" |
| 69 | create_region = common.Element("region", |
| 70 | xmlns=XMLNS, |
| 71 | description=description) |
| 72 | if 'parent_region_id' in kwargs: |
| 73 | create_region.append(common.Element( |
| 74 | 'parent_region_id', kwargs.get('parent_region_id'))) |
| 75 | if 'unique_region_id' in kwargs: |
| 76 | resp, body = self.put( |
| 77 | 'regions/%s' % kwargs.get('unique_region_id'), |
| 78 | str(common.Document(create_region))) |
| 79 | else: |
| 80 | resp, body = self.post('regions', |
| 81 | str(common.Document(create_region))) |
| 82 | body = self._parse_body(etree.fromstring(body)) |
| 83 | return resp, body |
| 84 | |
| 85 | def update_region(self, region_id, **kwargs): |
| 86 | """Updates an region with given parameters. |
| 87 | """ |
| 88 | description = kwargs.get('description', None) |
| 89 | update_region = common.Element("region", |
| 90 | xmlns=XMLNS, |
| 91 | description=description) |
| 92 | if 'parent_region_id' in kwargs: |
| 93 | update_region.append(common.Element('parent_region_id', |
| 94 | kwargs.get('parent_region_id'))) |
| 95 | |
| 96 | resp, body = self.patch('regions/%s' % str(region_id), |
| 97 | str(common.Document(update_region))) |
| 98 | body = self._parse_body(etree.fromstring(body)) |
| 99 | return resp, body |
| 100 | |
| 101 | def get_region(self, region_id): |
| 102 | """Get Region.""" |
| 103 | url = 'regions/%s' % region_id |
| 104 | resp, body = self.get(url) |
| 105 | body = self._parse_body(etree.fromstring(body)) |
| 106 | return resp, body |
| 107 | |
| 108 | def list_regions(self, params=None): |
| 109 | """Get the list of regions.""" |
| 110 | url = 'regions' |
| 111 | if params: |
| 112 | url += '?%s' % urllib.urlencode(params) |
| 113 | resp, body = self.get(url) |
| 114 | body = self._parse_array(etree.fromstring(body)) |
| 115 | return resp, body |
| 116 | |
| 117 | def delete_region(self, region_id): |
| 118 | """Delete region.""" |
| 119 | resp, body = self.delete('regions/%s' % region_id) |
| 120 | return resp, body |