blob: 6490e344e8caa88e666c9a454759d9ee2d97da11 [file] [log] [blame]
rajalakshmi-ganesanab426722013-02-08 15:49:15 +05301# Copyright 2013 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.
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053015
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053016from lxml import etree
17
Mate Lakat23a58a32013-08-23 02:06:22 +010018from tempest.common import http
vponomaryov960eeb42014-02-22 18:25:25 +020019from tempest.common import rest_client
Matthew Treinish28f164c2014-03-04 18:55:06 +000020from tempest.common import xml_utils as common
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053025XMLNS = "http://docs.openstack.org/identity/api/v3"
26
27
vponomaryov960eeb42014-02-22 18:25:25 +020028class EndPointClientXML(rest_client.RestClient):
29 TYPE = "xml"
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(EndPointClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.identity.catalog_type
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053034 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000035 self.api_version = "v3"
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053036
37 def _parse_array(self, node):
38 array = []
39 for child in node.getchildren():
40 tag_list = child.tag.split('}', 1)
41 if tag_list[1] == "endpoint":
Haiwei Xuaad85db2014-03-05 05:17:39 +090042 array.append(common.xml_to_json(child))
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053043 return array
44
45 def _parse_body(self, body):
Haiwei Xuaad85db2014-03-05 05:17:39 +090046 json = common.xml_to_json(body)
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053047 return json
48
Sergey Murashov4fccd322014-03-22 09:58:52 +040049 def request(self, method, url, extra_headers=False, headers=None,
50 body=None, wait=None):
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053051 """Overriding the existing HTTP request in super class RestClient."""
Sergey Murashov4fccd322014-03-22 09:58:52 +040052 if extra_headers:
53 try:
54 headers.update(self.get_headers())
55 except (ValueError, TypeError):
56 headers = self.get_headers()
Matthew Treinish684d8992014-01-30 16:27:40 +000057 dscv = CONF.identity.disable_ssl_certificate_validation
Mate Lakat23a58a32013-08-23 02:06:22 +010058 self.http_obj = http.ClosingHttp(
59 disable_ssl_certificate_validation=dscv)
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053060 return super(EndPointClientXML, self).request(method, url,
Sergey Murashov4fccd322014-03-22 09:58:52 +040061 extra_headers,
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053062 headers=headers,
63 body=body)
64
65 def list_endpoints(self):
66 """Get the list of endpoints."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020067 resp, body = self.get("endpoints")
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053068 body = self._parse_array(etree.fromstring(body))
69 return resp, body
70
71 def create_endpoint(self, service_id, interface, url, **kwargs):
Brant Knudson3a9bdf92014-02-27 17:09:50 -060072 """Create endpoint.
73
74 Normally this function wouldn't allow setting values that are not
75 allowed for 'enabled'. Use `force_enabled` to set a non-boolean.
76
77 """
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053078 region = kwargs.get('region', None)
Brant Knudson3a9bdf92014-02-27 17:09:50 -060079 if 'force_enabled' in kwargs:
80 enabled = kwargs['force_enabled']
81 else:
82 enabled = kwargs.get('enabled', None)
83 if enabled is not None:
84 enabled = str(enabled).lower()
Haiwei Xuaad85db2014-03-05 05:17:39 +090085 create_endpoint = common.Element("endpoint",
86 xmlns=XMLNS,
87 service_id=service_id,
88 interface=interface,
89 url=url, region=region,
90 enabled=enabled)
91 resp, body = self.post('endpoints',
92 str(common.Document(create_endpoint)))
rajalakshmi-ganesanab426722013-02-08 15:49:15 +053093 body = self._parse_body(etree.fromstring(body))
94 return resp, body
95
96 def update_endpoint(self, endpoint_id, service_id=None, interface=None,
Brant Knudson3a9bdf92014-02-27 17:09:50 -060097 url=None, region=None, enabled=None, **kwargs):
98 """Updates an endpoint with given parameters.
99
100 Normally this function wouldn't allow setting values that are not
101 allowed for 'enabled'. Use `force_enabled` to set a non-boolean.
102
103 """
Haiwei Xuaad85db2014-03-05 05:17:39 +0900104 doc = common.Document()
105 endpoint = common.Element("endpoint")
rajalakshmi-ganesanab426722013-02-08 15:49:15 +0530106 doc.append(endpoint)
107
108 if service_id:
109 endpoint.add_attr("service_id", service_id)
110 if interface:
111 endpoint.add_attr("interface", interface)
112 if url:
113 endpoint.add_attr("url", url)
114 if region:
115 endpoint.add_attr("region", region)
Brant Knudson3a9bdf92014-02-27 17:09:50 -0600116
117 if 'force_enabled' in kwargs:
118 endpoint.add_attr("enabled", kwargs['force_enabled'])
119 elif enabled is not None:
Brant Knudson33402992014-02-27 13:31:37 -0600120 endpoint.add_attr("enabled", str(enabled).lower())
Brant Knudson3a9bdf92014-02-27 17:09:50 -0600121
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200122 resp, body = self.patch('endpoints/%s' % str(endpoint_id), str(doc))
rajalakshmi-ganesanab426722013-02-08 15:49:15 +0530123 body = self._parse_body(etree.fromstring(body))
124 return resp, body
125
126 def delete_endpoint(self, endpoint_id):
127 """Delete endpoint."""
128 resp_header, resp_body = self.delete('endpoints/%s' % endpoint_id)
129 return resp_header, resp_body