nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 1 | # 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 | |
| 16 | import json |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 17 | |
| 18 | from tempest.common.rest_client import RestClient |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
| 20 | |
| 21 | CONF = config.CONF |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class CredentialsClientJSON(RestClient): |
| 25 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 26 | def __init__(self, auth_provider): |
| 27 | super(CredentialsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | self.service = CONF.identity.catalog_type |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 29 | self.endpoint_url = 'adminURL' |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 30 | self.api_version = "v3" |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 31 | |
| 32 | def create_credential(self, access_key, secret_key, user_id, project_id): |
| 33 | """Creates a credential.""" |
| 34 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 35 | access_key, secret_key) |
| 36 | post_body = { |
| 37 | "blob": blob, |
| 38 | "project_id": project_id, |
| 39 | "type": "ec2", |
| 40 | "user_id": user_id |
| 41 | } |
| 42 | post_body = json.dumps({'credential': post_body}) |
| 43 | resp, body = self.post('credentials', post_body, |
| 44 | self.headers) |
| 45 | body = json.loads(body) |
| 46 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 47 | return resp, body['credential'] |
| 48 | |
| 49 | def update_credential(self, credential_id, **kwargs): |
| 50 | """Updates a credential.""" |
| 51 | resp, body = self.get_credential(credential_id) |
| 52 | cred_type = kwargs.get('type', body['type']) |
| 53 | access_key = kwargs.get('access_key', body['blob']['access']) |
| 54 | secret_key = kwargs.get('secret_key', body['blob']['secret']) |
| 55 | project_id = kwargs.get('project_id', body['project_id']) |
| 56 | user_id = kwargs.get('user_id', body['user_id']) |
| 57 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 58 | access_key, secret_key) |
| 59 | post_body = { |
| 60 | "blob": blob, |
| 61 | "project_id": project_id, |
| 62 | "type": cred_type, |
| 63 | "user_id": user_id |
| 64 | } |
| 65 | post_body = json.dumps({'credential': post_body}) |
| 66 | resp, body = self.patch('credentials/%s' % credential_id, post_body, |
| 67 | self.headers) |
| 68 | body = json.loads(body) |
| 69 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 70 | return resp, body['credential'] |
| 71 | |
| 72 | def get_credential(self, credential_id): |
| 73 | """To GET Details of a credential.""" |
| 74 | resp, body = self.get('credentials/%s' % credential_id) |
| 75 | body = json.loads(body) |
| 76 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 77 | return resp, body['credential'] |
| 78 | |
| 79 | def list_credentials(self): |
| 80 | """Lists out all the available credentials.""" |
| 81 | resp, body = self.get('credentials') |
| 82 | body = json.loads(body) |
| 83 | return resp, body['credentials'] |
| 84 | |
| 85 | def delete_credential(self, credential_id): |
| 86 | """Deletes a credential.""" |
| 87 | resp, body = self.delete('credentials/%s' % credential_id) |
| 88 | return resp, body |