blob: 76696787b11141e1e8f0bc019948d9ac3b587017 [file] [log] [blame]
ravikumar-venkatesanacf99d72014-07-09 14:58:25 +00001# Copyright 2014 Hewlett-Packard Development Company, L.P
ravikumar-venkatesan3052e942014-05-12 18:25:17 +00002# 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
16import urllib
17
18from lxml import etree
19
20from tempest.common import http
21from tempest.common import rest_client
22from tempest.common import xml_utils as common
23from tempest import config
24
25CONF = config.CONF
26
27XMLNS = "http://docs.openstack.org/identity/api/v3"
28
29
30class 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)))
David Kranz2aaf5312014-08-29 09:22:10 -040082 self.expected_success(201, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000083 body = self._parse_body(etree.fromstring(body))
84 return resp, body
85
86 def update_region(self, region_id, **kwargs):
87 """Updates an region with given parameters.
88 """
89 description = kwargs.get('description', None)
90 update_region = common.Element("region",
91 xmlns=XMLNS,
92 description=description)
93 if 'parent_region_id' in kwargs:
94 update_region.append(common.Element('parent_region_id',
95 kwargs.get('parent_region_id')))
96
97 resp, body = self.patch('regions/%s' % str(region_id),
98 str(common.Document(update_region)))
David Kranz2aaf5312014-08-29 09:22:10 -040099 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +0000100 body = self._parse_body(etree.fromstring(body))
101 return resp, body
102
103 def get_region(self, region_id):
104 """Get Region."""
105 url = 'regions/%s' % region_id
106 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -0400107 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +0000108 body = self._parse_body(etree.fromstring(body))
109 return resp, body
110
111 def list_regions(self, params=None):
112 """Get the list of regions."""
113 url = 'regions'
114 if params:
115 url += '?%s' % urllib.urlencode(params)
116 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -0400117 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +0000118 body = self._parse_array(etree.fromstring(body))
119 return resp, body
120
121 def delete_region(self, region_id):
122 """Delete region."""
123 resp, body = self.delete('regions/%s' % region_id)
David Kranz2aaf5312014-08-29 09:22:10 -0400124 self.expected_success(204, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +0000125 return resp, body