blob: 22ed44d984d0966ebd2e65f54d4f4b8a51245242 [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 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
vponomaryov960eeb42014-02-22 18:25:25 +020032class CredentialsClientXML(rest_client.RestClient):
33 TYPE = "xml"
nayna-patel914b4712013-07-16 08:29:05 +000034
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000035 def __init__(self, auth_provider):
36 super(CredentialsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000037 self.service = CONF.identity.catalog_type
nayna-patel914b4712013-07-16 08:29:05 +000038 self.endpoint_url = 'adminURL'
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000039 self.api_version = "v3"
nayna-patel914b4712013-07-16 08:29:05 +000040
41 def _parse_body(self, body):
42 data = xml_to_json(body)
43 return data
44
45 def _parse_creds(self, node):
46 array = []
47 for child in node.getchildren():
48 tag_list = child.tag.split('}', 1)
49 if tag_list[1] == "credential":
50 array.append(xml_to_json(child))
51 return array
52
53 def create_credential(self, access_key, secret_key, user_id, project_id):
54 """Creates a credential."""
55 cred_type = 'ec2'
56 access = ""access": "%s"" % access_key
57 secret = ""secret": "%s"" % secret_key
58 blob = Element('blob',
59 xmlns=XMLNS)
60 blob.append(Text("{%s , %s}"
61 % (access, secret)))
62 credential = Element('credential', project_id=project_id,
63 type=cred_type, user_id=user_id)
64 credential.append(blob)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020065 resp, body = self.post('credentials', str(Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000066 body = self._parse_body(etree.fromstring(body))
67 body['blob'] = json.loads(body['blob'])
68 return resp, body
69
70 def update_credential(self, credential_id, **kwargs):
71 """Updates a credential."""
72 resp, body = self.get_credential(credential_id)
73 cred_type = kwargs.get('type', body['type'])
74 access_key = kwargs.get('access_key', body['blob']['access'])
75 secret_key = kwargs.get('secret_key', body['blob']['secret'])
76 project_id = kwargs.get('project_id', body['project_id'])
77 user_id = kwargs.get('user_id', body['user_id'])
78 access = ""access": "%s"" % access_key
79 secret = ""secret": "%s"" % secret_key
80 blob = Element('blob',
81 xmlns=XMLNS)
82 blob.append(Text("{%s , %s}"
83 % (access, secret)))
84 credential = Element('credential', project_id=project_id,
85 type=cred_type, user_id=user_id)
86 credential.append(blob)
87 resp, body = self.patch('credentials/%s' % credential_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020088 str(Document(credential)))
nayna-patel914b4712013-07-16 08:29:05 +000089 body = self._parse_body(etree.fromstring(body))
90 body['blob'] = json.loads(body['blob'])
91 return resp, body
92
93 def get_credential(self, credential_id):
94 """To GET Details of a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020095 resp, body = self.get('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +000096 body = self._parse_body(etree.fromstring(body))
97 body['blob'] = json.loads(body['blob'])
98 return resp, body
99
100 def list_credentials(self):
101 """Lists out all the available credentials."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200102 resp, body = self.get('credentials')
nayna-patel914b4712013-07-16 08:29:05 +0000103 body = self._parse_creds(etree.fromstring(body))
104 return resp, body
105
106 def delete_credential(self, credential_id):
107 """Deletes a credential."""
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200108 resp, body = self.delete('credentials/%s' % credential_id)
nayna-patel914b4712013-07-16 08:29:05 +0000109 return resp, body