blob: c12018ac19d42592d9c2a130e234d9c078d9614c [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 Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
harika-vakadi40e10112013-02-08 14:38:09 +053021from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
23from tempest.services.compute.xml.common import xml_to_json
24
Matthew Treinish684d8992014-01-30 16:27:40 +000025CONF = config.CONF
26
harika-vakadi40e10112013-02-08 14:38:09 +053027XMLNS = "http://docs.openstack.org/identity/api/v3"
28
29
vponomaryov960eeb42014-02-22 18:25:25 +020030class PolicyClientXML(rest_client.RestClient):
31 TYPE = "xml"
harika-vakadi40e10112013-02-08 14:38:09 +053032
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def __init__(self, auth_provider):
34 super(PolicyClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000035 self.service = CONF.identity.catalog_type
harika-vakadi40e10112013-02-08 14:38:09 +053036 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000037 self.api_version = "v3"
harika-vakadi40e10112013-02-08 14:38:09 +053038
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] == "policy":
44 array.append(xml_to_json(child))
45 return array
46
47 def _parse_body(self, body):
48 json = xml_to_json(body)
49 return json
50
51 def request(self, method, url, headers=None, body=None, wait=None):
52 """Overriding the existing HTTP request in super class RestClient."""
Matthew Treinish684d8992014-01-30 16:27:40 +000053 dscv = CONF.identity.disable_ssl_certificate_validation
Mate Lakat23a58a32013-08-23 02:06:22 +010054 self.http_obj = http.ClosingHttp(
55 disable_ssl_certificate_validation=dscv)
harika-vakadi40e10112013-02-08 14:38:09 +053056 return super(PolicyClientXML, self).request(method, url,
57 headers=headers,
58 body=body)
59
60 def create_policy(self, blob, type):
61 """Creates a Policy."""
62 create_policy = Element("policy", xmlns=XMLNS, blob=blob, type=type)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020063 resp, body = self.post('policies', str(Document(create_policy)))
harika-vakadi40e10112013-02-08 14:38:09 +053064 body = self._parse_body(etree.fromstring(body))
65 return resp, body
66
67 def list_policies(self):
68 """Lists the policies."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020069 resp, body = self.get('policies')
harika-vakadi40e10112013-02-08 14:38:09 +053070 body = self._parse_array(etree.fromstring(body))
71 return resp, body
72
73 def get_policy(self, policy_id):
74 """Lists out the given policy."""
75 url = 'policies/%s' % policy_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020076 resp, body = self.get(url)
harika-vakadi40e10112013-02-08 14:38:09 +053077 body = self._parse_body(etree.fromstring(body))
78 return resp, body
79
80 def update_policy(self, policy_id, **kwargs):
81 """Updates a policy."""
82 resp, body = self.get_policy(policy_id)
83 type = kwargs.get('type')
84 update_policy = Element("policy", xmlns=XMLNS, type=type)
85 url = 'policies/%s' % policy_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020086 resp, body = self.patch(url, str(Document(update_policy)))
harika-vakadi40e10112013-02-08 14:38:09 +053087 body = self._parse_body(etree.fromstring(body))
88 return resp, body
89
90 def delete_policy(self, policy_id):
91 """Deletes the policy."""
92 url = "policies/%s" % policy_id
93 return self.delete(url)