blob: 73d831b48b2bdc98e59ec6a9fb7f8a43b6f6a50b [file] [log] [blame]
harika-vakadi40e10112013-02-08 14:38:09 +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.
15
harika-vakadi40e10112013-02-08 14:38:09 +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
harika-vakadi40e10112013-02-08 14:38:09 +053022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
harika-vakadi40e10112013-02-08 14:38:09 +053025XMLNS = "http://docs.openstack.org/identity/api/v3"
26
27
vponomaryov960eeb42014-02-22 18:25:25 +020028class PolicyClientXML(rest_client.RestClient):
29 TYPE = "xml"
harika-vakadi40e10112013-02-08 14:38:09 +053030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(PolicyClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.identity.catalog_type
harika-vakadi40e10112013-02-08 14:38:09 +053034 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000035 self.api_version = "v3"
harika-vakadi40e10112013-02-08 14:38:09 +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] == "policy":
Haiwei Xuaad85db2014-03-05 05:17:39 +090042 array.append(common.xml_to_json(child))
harika-vakadi40e10112013-02-08 14:38:09 +053043 return array
44
45 def _parse_body(self, body):
Haiwei Xuaad85db2014-03-05 05:17:39 +090046 json = common.xml_to_json(body)
harika-vakadi40e10112013-02-08 14:38:09 +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):
harika-vakadi40e10112013-02-08 14:38:09 +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)
harika-vakadi40e10112013-02-08 14:38:09 +053060 return super(PolicyClientXML, self).request(method, url,
Sergey Murashov4fccd322014-03-22 09:58:52 +040061 extra_headers,
harika-vakadi40e10112013-02-08 14:38:09 +053062 headers=headers,
63 body=body)
64
65 def create_policy(self, blob, type):
66 """Creates a Policy."""
Haiwei Xuaad85db2014-03-05 05:17:39 +090067 create_policy = common.Element("policy", xmlns=XMLNS,
68 blob=blob, type=type)
69 resp, body = self.post('policies', str(common.Document(create_policy)))
harika-vakadi40e10112013-02-08 14:38:09 +053070 body = self._parse_body(etree.fromstring(body))
71 return resp, body
72
73 def list_policies(self):
74 """Lists the policies."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020075 resp, body = self.get('policies')
harika-vakadi40e10112013-02-08 14:38:09 +053076 body = self._parse_array(etree.fromstring(body))
77 return resp, body
78
79 def get_policy(self, policy_id):
80 """Lists out the given policy."""
81 url = 'policies/%s' % policy_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020082 resp, body = self.get(url)
harika-vakadi40e10112013-02-08 14:38:09 +053083 body = self._parse_body(etree.fromstring(body))
84 return resp, body
85
86 def update_policy(self, policy_id, **kwargs):
87 """Updates a policy."""
88 resp, body = self.get_policy(policy_id)
89 type = kwargs.get('type')
Haiwei Xuaad85db2014-03-05 05:17:39 +090090 update_policy = common.Element("policy", xmlns=XMLNS, type=type)
harika-vakadi40e10112013-02-08 14:38:09 +053091 url = 'policies/%s' % policy_id
Haiwei Xuaad85db2014-03-05 05:17:39 +090092 resp, body = self.patch(url, str(common.Document(update_policy)))
harika-vakadi40e10112013-02-08 14:38:09 +053093 body = self._parse_body(etree.fromstring(body))
94 return resp, body
95
96 def delete_policy(self, policy_id):
97 """Deletes the policy."""
98 url = "policies/%s" % policy_id
99 return self.delete(url)