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 | |
Ken'ichi Ohmichi | 5e39799 | 2014-12-17 09:03:19 +0000 | [diff] [blame] | 18 | from tempest.services.identity.v3.json import base |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 19 | |
| 20 | |
Ken'ichi Ohmichi | 5e39799 | 2014-12-17 09:03:19 +0000 | [diff] [blame] | 21 | class CredentialsClientJSON(base.IdentityV3Client): |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 22 | |
| 23 | def create_credential(self, access_key, secret_key, user_id, project_id): |
| 24 | """Creates a credential.""" |
| 25 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 26 | access_key, secret_key) |
| 27 | post_body = { |
| 28 | "blob": blob, |
| 29 | "project_id": project_id, |
| 30 | "type": "ec2", |
| 31 | "user_id": user_id |
| 32 | } |
| 33 | post_body = json.dumps({'credential': post_body}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 34 | resp, body = self.post('credentials', post_body) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 35 | self.expected_success(201, resp.status) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 36 | body = json.loads(body) |
| 37 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 38 | return resp, body['credential'] |
| 39 | |
| 40 | def update_credential(self, credential_id, **kwargs): |
| 41 | """Updates a credential.""" |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 42 | _, body = self.get_credential(credential_id) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 43 | cred_type = kwargs.get('type', body['type']) |
| 44 | access_key = kwargs.get('access_key', body['blob']['access']) |
| 45 | secret_key = kwargs.get('secret_key', body['blob']['secret']) |
| 46 | project_id = kwargs.get('project_id', body['project_id']) |
| 47 | user_id = kwargs.get('user_id', body['user_id']) |
| 48 | blob = "{\"access\": \"%s\", \"secret\": \"%s\"}" % ( |
| 49 | access_key, secret_key) |
| 50 | post_body = { |
| 51 | "blob": blob, |
| 52 | "project_id": project_id, |
| 53 | "type": cred_type, |
| 54 | "user_id": user_id |
| 55 | } |
| 56 | post_body = json.dumps({'credential': post_body}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 57 | resp, body = self.patch('credentials/%s' % credential_id, post_body) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 58 | self.expected_success(200, resp.status) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 59 | body = json.loads(body) |
| 60 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 61 | return resp, body['credential'] |
| 62 | |
| 63 | def get_credential(self, credential_id): |
| 64 | """To GET Details of a credential.""" |
| 65 | resp, body = self.get('credentials/%s' % credential_id) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 66 | self.expected_success(200, resp.status) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 67 | body = json.loads(body) |
| 68 | body['credential']['blob'] = json.loads(body['credential']['blob']) |
| 69 | return resp, body['credential'] |
| 70 | |
| 71 | def list_credentials(self): |
| 72 | """Lists out all the available credentials.""" |
| 73 | resp, body = self.get('credentials') |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 74 | self.expected_success(200, resp.status) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 75 | body = json.loads(body) |
| 76 | return resp, body['credentials'] |
| 77 | |
| 78 | def delete_credential(self, credential_id): |
| 79 | """Deletes a credential.""" |
| 80 | resp, body = self.delete('credentials/%s' % credential_id) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 81 | self.expected_success(204, resp.status) |
nayna-patel | 914b471 | 2013-07-16 08:29:05 +0000 | [diff] [blame] | 82 | return resp, body |