blob: f6fa6785fca81db0e1f7c76a72eeb07ef2f044bc [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
nayna-patel914b4712013-07-16 08:29:05 +000017
18from lxml import etree
19
20from tempest.common.rest_client import RestClientXML
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
nayna-patel914b4712013-07-16 08:29:05 +000022from 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
Matthew Treinish684d8992014-01-30 16:27:40 +000027CONF = config.CONF
nayna-patel914b4712013-07-16 08:29:05 +000028
29XMLNS = "http://docs.openstack.org/identity/api/v3"
30
31
32class CredentialsClientXML(RestClientXML):
33
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000034 def __init__(self, auth_provider):
35 super(CredentialsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000036 self.service = CONF.identity.catalog_type
nayna-patel914b4712013-07-16 08:29:05 +000037 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000038 self.api_version = "v3"
nayna-patel914b4712013-07-16 08:29:05 +000039
40 def _parse_body(self, body):
41 data = xml_to_json(body)
42 return data
43
44 def _parse_creds(self, node):
45 array = []
46 for child in node.getchildren():
47 tag_list = child.tag.split('}', 1)
48 if tag_list[1] == "credential":
49 array.append(xml_to_json(child))
50 return array
51
52 def create_credential(self, access_key, secret_key, user_id, project_id):
53 """Creates a credential."""
54 cred_type = 'ec2'
55 access = ""access": "%s"" % access_key
56 secret = ""secret": "%s"" % secret_key
57 blob = Element('blob',
58 xmlns=XMLNS)
59 blob.append(Text("{%s , %s}"
60 % (access, secret)))
61 credential = Element('credential', project_id=project_id,
62 type=cred_type, user_id=user_id)
63 credential.append(blob)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020064 resp, body = self.post('credentials', str(Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000065 body = self._parse_body(etree.fromstring(body))
66 body['blob'] = json.loads(body['blob'])
67 return resp, body
68
69 def update_credential(self, credential_id, **kwargs):
70 """Updates a credential."""
71 resp, body = self.get_credential(credential_id)
72 cred_type = kwargs.get('type', body['type'])
73 access_key = kwargs.get('access_key', body['blob']['access'])
74 secret_key = kwargs.get('secret_key', body['blob']['secret'])
75 project_id = kwargs.get('project_id', body['project_id'])
76 user_id = kwargs.get('user_id', body['user_id'])
77 access = ""access": "%s"" % access_key
78 secret = ""secret": "%s"" % secret_key
79 blob = Element('blob',
80 xmlns=XMLNS)
81 blob.append(Text("{%s , %s}"
82 % (access, secret)))
83 credential = Element('credential', project_id=project_id,
84 type=cred_type, user_id=user_id)
85 credential.append(blob)
86 resp, body = self.patch('credentials/%s' % credential_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020087 str(Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000088 body = self._parse_body(etree.fromstring(body))
89 body['blob'] = json.loads(body['blob'])
90 return resp, body
91
92 def get_credential(self, credential_id):
93 """To GET Details of a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020094 resp, body = self.get('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +000095 body = self._parse_body(etree.fromstring(body))
96 body['blob'] = json.loads(body['blob'])
97 return resp, body
98
99 def list_credentials(self):
100 """Lists out all the available credentials."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200101 resp, body = self.get('credentials')
nayna-patel914b4712013-07-16 08:29:05 +0000102 body = self._parse_creds(etree.fromstring(body))
103 return resp, body
104
105 def delete_credential(self, credential_id):
106 """Deletes a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200107 resp, body = self.delete('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +0000108 return resp, body