nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 OpenStack Foundation |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import json |
| 19 | from urlparse import urlparse |
| 20 | |
| 21 | from lxml import etree |
| 22 | |
| 23 | from tempest.common.rest_client import RestClientXML |
| 24 | from tempest.services.compute.xml.common import Document |
| 25 | from tempest.services.compute.xml.common import Element |
| 26 | from tempest.services.compute.xml.common import Text |
| 27 | from tempest.services.compute.xml.common import xml_to_json |
| 28 | |
| 29 | |
| 30 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 31 | |
| 32 | |
| 33 | class CredentialsClientXML(RestClientXML): |
| 34 | |
| 35 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 36 | super(CredentialsClientXML, self).__init__(config, username, password, |
| 37 | auth_url, tenant_name) |
| 38 | self.service = self.config.identity.catalog_type |
| 39 | self.endpoint_url = 'adminURL' |
| 40 | |
| 41 | def request(self, method, url, headers=None, body=None, wait=None): |
| 42 | """Overriding the existing HTTP request in super class rest_client.""" |
| 43 | self._set_auth() |
| 44 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 45 | "/v3") |
| 46 | return super(CredentialsClientXML, self).request(method, url, |
| 47 | headers=headers, |
| 48 | body=body) |
| 49 | |
| 50 | def _parse_body(self, body): |
| 51 | data = xml_to_json(body) |
| 52 | return data |
| 53 | |
| 54 | def _parse_creds(self, node): |
| 55 | array = [] |
| 56 | for child in node.getchildren(): |
| 57 | tag_list = child.tag.split('}', 1) |
| 58 | if tag_list[1] == "credential": |
| 59 | array.append(xml_to_json(child)) |
| 60 | return array |
| 61 | |
| 62 | def create_credential(self, access_key, secret_key, user_id, project_id): |
| 63 | """Creates a credential.""" |
| 64 | cred_type = 'ec2' |
| 65 | access = ""access": "%s"" % access_key |
| 66 | secret = ""secret": "%s"" % secret_key |
| 67 | blob = Element('blob', |
| 68 | xmlns=XMLNS) |
| 69 | blob.append(Text("{%s , %s}" |
| 70 | % (access, secret))) |
| 71 | credential = Element('credential', project_id=project_id, |
| 72 | type=cred_type, user_id=user_id) |
| 73 | credential.append(blob) |
| 74 | resp, body = self.post('credentials', str(Document(credential)), |
| 75 | self.headers) |
| 76 | body = self._parse_body(etree.fromstring(body)) |
| 77 | body['blob'] = json.loads(body['blob']) |
| 78 | return resp, body |
| 79 | |
| 80 | def update_credential(self, credential_id, **kwargs): |
| 81 | """Updates a credential.""" |
| 82 | resp, body = self.get_credential(credential_id) |
| 83 | cred_type = kwargs.get('type', body['type']) |
| 84 | access_key = kwargs.get('access_key', body['blob']['access']) |
| 85 | secret_key = kwargs.get('secret_key', body['blob']['secret']) |
| 86 | project_id = kwargs.get('project_id', body['project_id']) |
| 87 | user_id = kwargs.get('user_id', body['user_id']) |
| 88 | access = ""access": "%s"" % access_key |
| 89 | secret = ""secret": "%s"" % secret_key |
| 90 | blob = Element('blob', |
| 91 | xmlns=XMLNS) |
| 92 | blob.append(Text("{%s , %s}" |
| 93 | % (access, secret))) |
| 94 | credential = Element('credential', project_id=project_id, |
| 95 | type=cred_type, user_id=user_id) |
| 96 | credential.append(blob) |
| 97 | resp, body = self.patch('credentials/%s' % credential_id, |
| 98 | str(Document(credential)), |
| 99 | self.headers) |
| 100 | body = self._parse_body(etree.fromstring(body)) |
| 101 | body['blob'] = json.loads(body['blob']) |
| 102 | return resp, body |
| 103 | |
| 104 | def get_credential(self, credential_id): |
| 105 | """To GET Details of a credential.""" |
| 106 | resp, body = self.get('credentials/%s' % credential_id, self.headers) |
| 107 | body = self._parse_body(etree.fromstring(body)) |
| 108 | body['blob'] = json.loads(body['blob']) |
| 109 | return resp, body |
| 110 | |
| 111 | def list_credentials(self): |
| 112 | """Lists out all the available credentials.""" |
| 113 | resp, body = self.get('credentials', self.headers) |
| 114 | body = self._parse_creds(etree.fromstring(body)) |
| 115 | return resp, body |
| 116 | |
| 117 | def delete_credential(self, credential_id): |
| 118 | """Deletes a credential.""" |
| 119 | resp, body = self.delete('credentials/%s' % credential_id, |
| 120 | self.headers) |
| 121 | return resp, body |