rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 1 | # 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-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 15 | |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 16 | from lxml import etree |
| 17 | |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 18 | from tempest.common import http |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 19 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 21 | from tempest.services.compute.xml.common import Document |
| 22 | from tempest.services.compute.xml.common import Element |
| 23 | from tempest.services.compute.xml.common import xml_to_json |
| 24 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 27 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 28 | |
| 29 | |
| 30 | class EndPointClientXML(RestClientXML): |
| 31 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 32 | def __init__(self, auth_provider): |
| 33 | super(EndPointClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 34 | self.service = CONF.identity.catalog_type |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 35 | self.endpoint_url = 'adminURL' |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 36 | self.api_version = "v3" |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 37 | |
| 38 | def _parse_array(self, node): |
| 39 | array = [] |
| 40 | for child in node.getchildren(): |
| 41 | tag_list = child.tag.split('}', 1) |
| 42 | if tag_list[1] == "endpoint": |
| 43 | array.append(xml_to_json(child)) |
| 44 | return array |
| 45 | |
| 46 | def _parse_body(self, body): |
| 47 | json = xml_to_json(body) |
| 48 | return json |
| 49 | |
| 50 | def request(self, method, url, headers=None, body=None, wait=None): |
| 51 | """Overriding the existing HTTP request in super class RestClient.""" |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 52 | dscv = CONF.identity.disable_ssl_certificate_validation |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 53 | self.http_obj = http.ClosingHttp( |
| 54 | disable_ssl_certificate_validation=dscv) |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 55 | return super(EndPointClientXML, self).request(method, url, |
| 56 | headers=headers, |
| 57 | body=body) |
| 58 | |
| 59 | def list_endpoints(self): |
| 60 | """Get the list of endpoints.""" |
| 61 | resp, body = self.get("endpoints", self.headers) |
| 62 | body = self._parse_array(etree.fromstring(body)) |
| 63 | return resp, body |
| 64 | |
| 65 | def create_endpoint(self, service_id, interface, url, **kwargs): |
| 66 | """Create endpoint.""" |
| 67 | region = kwargs.get('region', None) |
| 68 | enabled = kwargs.get('enabled', None) |
| 69 | create_endpoint = Element("endpoint", |
| 70 | xmlns=XMLNS, |
| 71 | service_id=service_id, |
| 72 | interface=interface, |
| 73 | url=url, region=region, |
| 74 | enabled=enabled) |
| 75 | resp, body = self.post('endpoints', str(Document(create_endpoint)), |
| 76 | self.headers) |
| 77 | body = self._parse_body(etree.fromstring(body)) |
| 78 | return resp, body |
| 79 | |
| 80 | def update_endpoint(self, endpoint_id, service_id=None, interface=None, |
| 81 | url=None, region=None, enabled=None): |
| 82 | """Updates an endpoint with given parameters.""" |
| 83 | doc = Document() |
| 84 | endpoint = Element("endpoint") |
| 85 | doc.append(endpoint) |
| 86 | |
| 87 | if service_id: |
| 88 | endpoint.add_attr("service_id", service_id) |
| 89 | if interface: |
| 90 | endpoint.add_attr("interface", interface) |
| 91 | if url: |
| 92 | endpoint.add_attr("url", url) |
| 93 | if region: |
| 94 | endpoint.add_attr("region", region) |
| 95 | if enabled is not None: |
| 96 | endpoint.add_attr("enabled", enabled) |
| 97 | resp, body = self.patch('endpoints/%s' % str(endpoint_id), |
| 98 | str(doc), self.headers) |
| 99 | body = self._parse_body(etree.fromstring(body)) |
| 100 | return resp, body |
| 101 | |
| 102 | def delete_endpoint(self, endpoint_id): |
| 103 | """Delete endpoint.""" |
| 104 | resp_header, resp_body = self.delete('endpoints/%s' % endpoint_id) |
| 105 | return resp_header, resp_body |