blob: 4344db1901aa3a2e4c5d217e660328c7d0689981 [file] [log] [blame]
nayna-patel914b4712013-07-16 08:29:05 +00001# 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
16import json
17from urlparse import urlparse
18
19from lxml import etree
20
21from tempest.common.rest_client import RestClientXML
22from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
24from tempest.services.compute.xml.common import Text
25from tempest.services.compute.xml.common import xml_to_json
26
27
28XMLNS = "http://docs.openstack.org/identity/api/v3"
29
30
31class CredentialsClientXML(RestClientXML):
32
33 def __init__(self, config, username, password, auth_url, tenant_name=None):
34 super(CredentialsClientXML, self).__init__(config, username, password,
35 auth_url, tenant_name)
36 self.service = self.config.identity.catalog_type
37 self.endpoint_url = 'adminURL'
38
39 def request(self, method, url, headers=None, body=None, wait=None):
40 """Overriding the existing HTTP request in super class rest_client."""
41 self._set_auth()
42 self.base_url = self.base_url.replace(urlparse(self.base_url).path,
43 "/v3")
44 return super(CredentialsClientXML, self).request(method, url,
45 headers=headers,
46 body=body)
47
48 def _parse_body(self, body):
49 data = xml_to_json(body)
50 return data
51
52 def _parse_creds(self, node):
53 array = []
54 for child in node.getchildren():
55 tag_list = child.tag.split('}', 1)
56 if tag_list[1] == "credential":
57 array.append(xml_to_json(child))
58 return array
59
60 def create_credential(self, access_key, secret_key, user_id, project_id):
61 """Creates a credential."""
62 cred_type = 'ec2'
63 access = ""access": "%s"" % access_key
64 secret = ""secret": "%s"" % secret_key
65 blob = Element('blob',
66 xmlns=XMLNS)
67 blob.append(Text("{%s , %s}"
68 % (access, secret)))
69 credential = Element('credential', project_id=project_id,
70 type=cred_type, user_id=user_id)
71 credential.append(blob)
72 resp, body = self.post('credentials', str(Document(credential)),
73 self.headers)
74 body = self._parse_body(etree.fromstring(body))
75 body['blob'] = json.loads(body['blob'])
76 return resp, body
77
78 def update_credential(self, credential_id, **kwargs):
79 """Updates a credential."""
80 resp, body = self.get_credential(credential_id)
81 cred_type = kwargs.get('type', body['type'])
82 access_key = kwargs.get('access_key', body['blob']['access'])
83 secret_key = kwargs.get('secret_key', body['blob']['secret'])
84 project_id = kwargs.get('project_id', body['project_id'])
85 user_id = kwargs.get('user_id', body['user_id'])
86 access = ""access": "%s"" % access_key
87 secret = ""secret": "%s"" % secret_key
88 blob = Element('blob',
89 xmlns=XMLNS)
90 blob.append(Text("{%s , %s}"
91 % (access, secret)))
92 credential = Element('credential', project_id=project_id,
93 type=cred_type, user_id=user_id)
94 credential.append(blob)
95 resp, body = self.patch('credentials/%s' % credential_id,
96 str(Document(credential)),
97 self.headers)
98 body = self._parse_body(etree.fromstring(body))
99 body['blob'] = json.loads(body['blob'])
100 return resp, body
101
102 def get_credential(self, credential_id):
103 """To GET Details of a credential."""
104 resp, body = self.get('credentials/%s' % credential_id, self.headers)
105 body = self._parse_body(etree.fromstring(body))
106 body['blob'] = json.loads(body['blob'])
107 return resp, body
108
109 def list_credentials(self):
110 """Lists out all the available credentials."""
111 resp, body = self.get('credentials', self.headers)
112 body = self._parse_creds(etree.fromstring(body))
113 return resp, body
114
115 def delete_credential(self, credential_id):
116 """Deletes a credential."""
117 resp, body = self.delete('credentials/%s' % credential_id,
118 self.headers)
119 return resp, body