harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +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. |
| 15 | |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +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 |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 19 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 21 | from tempest.services.compute.xml import common |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 25 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 26 | |
| 27 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 28 | class PolicyClientXML(rest_client.RestClient): |
| 29 | TYPE = "xml" |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 30 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 31 | def __init__(self, auth_provider): |
| 32 | super(PolicyClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | self.service = CONF.identity.catalog_type |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 34 | self.endpoint_url = 'adminURL' |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 35 | self.api_version = "v3" |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 36 | |
| 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] == "policy": |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 42 | array.append(common.xml_to_json(child)) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 43 | return array |
| 44 | |
| 45 | def _parse_body(self, body): |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 46 | json = common.xml_to_json(body) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 47 | return json |
| 48 | |
| 49 | def request(self, method, url, headers=None, body=None, wait=None): |
| 50 | """Overriding the existing HTTP request in super class RestClient.""" |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 51 | dscv = CONF.identity.disable_ssl_certificate_validation |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 52 | self.http_obj = http.ClosingHttp( |
| 53 | disable_ssl_certificate_validation=dscv) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 54 | return super(PolicyClientXML, self).request(method, url, |
| 55 | headers=headers, |
| 56 | body=body) |
| 57 | |
| 58 | def create_policy(self, blob, type): |
| 59 | """Creates a Policy.""" |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 60 | create_policy = common.Element("policy", xmlns=XMLNS, |
| 61 | blob=blob, type=type) |
| 62 | resp, body = self.post('policies', str(common.Document(create_policy))) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 63 | body = self._parse_body(etree.fromstring(body)) |
| 64 | return resp, body |
| 65 | |
| 66 | def list_policies(self): |
| 67 | """Lists the policies.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 68 | resp, body = self.get('policies') |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 69 | body = self._parse_array(etree.fromstring(body)) |
| 70 | return resp, body |
| 71 | |
| 72 | def get_policy(self, policy_id): |
| 73 | """Lists out the given policy.""" |
| 74 | url = 'policies/%s' % policy_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 75 | resp, body = self.get(url) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 76 | body = self._parse_body(etree.fromstring(body)) |
| 77 | return resp, body |
| 78 | |
| 79 | def update_policy(self, policy_id, **kwargs): |
| 80 | """Updates a policy.""" |
| 81 | resp, body = self.get_policy(policy_id) |
| 82 | type = kwargs.get('type') |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 83 | update_policy = common.Element("policy", xmlns=XMLNS, type=type) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 84 | url = 'policies/%s' % policy_id |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 85 | resp, body = self.patch(url, str(common.Document(update_policy))) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 86 | body = self._parse_body(etree.fromstring(body)) |
| 87 | return resp, body |
| 88 | |
| 89 | def delete_policy(self, policy_id): |
| 90 | """Deletes the policy.""" |
| 91 | url = "policies/%s" % policy_id |
| 92 | return self.delete(url) |