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 tempest.common.rest_client import RestClient |
| 22 | |
| 23 | |
| 24 | class CredentialsClientJSON(RestClient): |
| 25 | |
| 26 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 27 | super(CredentialsClientJSON, self).__init__(config, username, password, |
| 28 | auth_url, tenant_name) |
| 29 | self.service = self.config.identity.catalog_type |
| 30 | self.endpoint_url = 'adminURL' |
| 31 | |
| 32 | def request(self, method, url, headers=None, body=None, wait=None): |
| 33 | """Overriding the existing HTTP request in super class rest_client.""" |
| 34 | self._set_auth() |
| 35 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 36 | "/v3") |
| 37 | return super(CredentialsClientJSON, self).request(method, url, |
| 38 | headers=headers, |
| 39 | body=body) |
| 40 | |
| 41 | def create_credential(self, access_key, secret_key, user_id, project_id): |
| 42 | """Creates a credential.""" |
| 43 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 44 | access_key, secret_key) |
| 45 | post_body = { |
| 46 | "blob": blob, |
| 47 | "project_id": project_id, |
| 48 | "type": "ec2", |
| 49 | "user_id": user_id |
| 50 | } |
| 51 | post_body = json.dumps({'credential': post_body}) |
| 52 | resp, body = self.post('credentials', post_body, |
| 53 | self.headers) |
| 54 | body = json.loads(body) |
| 55 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 56 | return resp, body['credential'] |
| 57 | |
| 58 | def update_credential(self, credential_id, **kwargs): |
| 59 | """Updates a credential.""" |
| 60 | resp, body = self.get_credential(credential_id) |
| 61 | cred_type = kwargs.get('type', body['type']) |
| 62 | access_key = kwargs.get('access_key', body['blob']['access']) |
| 63 | secret_key = kwargs.get('secret_key', body['blob']['secret']) |
| 64 | project_id = kwargs.get('project_id', body['project_id']) |
| 65 | user_id = kwargs.get('user_id', body['user_id']) |
| 66 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 67 | access_key, secret_key) |
| 68 | post_body = { |
| 69 | "blob": blob, |
| 70 | "project_id": project_id, |
| 71 | "type": cred_type, |
| 72 | "user_id": user_id |
| 73 | } |
| 74 | post_body = json.dumps({'credential': post_body}) |
| 75 | resp, body = self.patch('credentials/%s' % credential_id, post_body, |
| 76 | self.headers) |
| 77 | body = json.loads(body) |
| 78 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 79 | return resp, body['credential'] |
| 80 | |
| 81 | def get_credential(self, credential_id): |
| 82 | """To GET Details of a credential.""" |
| 83 | resp, body = self.get('credentials/%s' % credential_id) |
| 84 | body = json.loads(body) |
| 85 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 86 | return resp, body['credential'] |
| 87 | |
| 88 | def list_credentials(self): |
| 89 | """Lists out all the available credentials.""" |
| 90 | resp, body = self.get('credentials') |
| 91 | body = json.loads(body) |
| 92 | return resp, body['credentials'] |
| 93 | |
| 94 | def delete_credential(self, credential_id): |
| 95 | """Deletes a credential.""" |
| 96 | resp, body = self.delete('credentials/%s' % credential_id) |
| 97 | return resp, body |