blob: 3c441884198b279b46bfa6b81350721a0a72d6e6 [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
vponomaryov960eeb42014-02-22 18:25:25 +020020from tempest.common import rest_client
Matthew Treinish28f164c2014-03-04 18:55:06 +000021from tempest.common import xml_utils as common
Matthew Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
nayna-patel914b4712013-07-16 08:29:05 +000023
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
nayna-patel914b4712013-07-16 08:29:05 +000025
26XMLNS = "http://docs.openstack.org/identity/api/v3"
27
28
vponomaryov960eeb42014-02-22 18:25:25 +020029class CredentialsClientXML(rest_client.RestClient):
30 TYPE = "xml"
nayna-patel914b4712013-07-16 08:29:05 +000031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
33 super(CredentialsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.service = CONF.identity.catalog_type
nayna-patel914b4712013-07-16 08:29:05 +000035 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000036 self.api_version = "v3"
nayna-patel914b4712013-07-16 08:29:05 +000037
38 def _parse_body(self, body):
Haiwei Xuaad85db2014-03-05 05:17:39 +090039 data = common.xml_to_json(body)
nayna-patel914b4712013-07-16 08:29:05 +000040 return data
41
42 def _parse_creds(self, node):
43 array = []
44 for child in node.getchildren():
45 tag_list = child.tag.split('}', 1)
46 if tag_list[1] == "credential":
Haiwei Xuaad85db2014-03-05 05:17:39 +090047 array.append(common.xml_to_json(child))
nayna-patel914b4712013-07-16 08:29:05 +000048 return array
49
50 def create_credential(self, access_key, secret_key, user_id, project_id):
51 """Creates a credential."""
52 cred_type = 'ec2'
53 access = ""access": "%s"" % access_key
54 secret = ""secret": "%s"" % secret_key
Haiwei Xuaad85db2014-03-05 05:17:39 +090055 blob = common.Element('blob',
56 xmlns=XMLNS)
57 blob.append(common.Text("{%s , %s}"
58 % (access, secret)))
59 credential = common.Element('credential', project_id=project_id,
60 type=cred_type, user_id=user_id)
nayna-patel914b4712013-07-16 08:29:05 +000061 credential.append(blob)
Haiwei Xuaad85db2014-03-05 05:17:39 +090062 resp, body = self.post('credentials', str(common.Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000063 body = self._parse_body(etree.fromstring(body))
64 body['blob'] = json.loads(body['blob'])
65 return resp, body
66
67 def update_credential(self, credential_id, **kwargs):
68 """Updates a credential."""
69 resp, body = self.get_credential(credential_id)
70 cred_type = kwargs.get('type', body['type'])
71 access_key = kwargs.get('access_key', body['blob']['access'])
72 secret_key = kwargs.get('secret_key', body['blob']['secret'])
73 project_id = kwargs.get('project_id', body['project_id'])
74 user_id = kwargs.get('user_id', body['user_id'])
75 access = ""access": "%s"" % access_key
76 secret = ""secret": "%s"" % secret_key
Haiwei Xuaad85db2014-03-05 05:17:39 +090077 blob = common.Element('blob',
78 xmlns=XMLNS)
79 blob.append(common.Text("{%s , %s}"
80 % (access, secret)))
81 credential = common.Element('credential', project_id=project_id,
82 type=cred_type, user_id=user_id)
nayna-patel914b4712013-07-16 08:29:05 +000083 credential.append(blob)
84 resp, body = self.patch('credentials/%s' % credential_id,
Haiwei Xuaad85db2014-03-05 05:17:39 +090085 str(common.Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000086 body = self._parse_body(etree.fromstring(body))
87 body['blob'] = json.loads(body['blob'])
88 return resp, body
89
90 def get_credential(self, credential_id):
91 """To GET Details of a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020092 resp, body = self.get('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +000093 body = self._parse_body(etree.fromstring(body))
94 body['blob'] = json.loads(body['blob'])
95 return resp, body
96
97 def list_credentials(self):
98 """Lists out all the available credentials."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020099 resp, body = self.get('credentials')
nayna-patel914b4712013-07-16 08:29:05 +0000100 body = self._parse_creds(etree.fromstring(body))
101 return resp, body
102
103 def delete_credential(self, credential_id):
104 """Deletes a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200105 resp, body = self.delete('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +0000106 return resp, body