harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [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 |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 17 | |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 18 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
| 20 | |
| 21 | CONF = config.CONF |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 22 | |
| 23 | |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 24 | class PolicyClientJSON(rest_client.RestClient): |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 25 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 26 | def __init__(self, auth_provider): |
| 27 | super(PolicyClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | self.service = CONF.identity.catalog_type |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 29 | self.endpoint_url = 'adminURL' |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 30 | self.api_version = "v3" |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 31 | |
| 32 | def create_policy(self, blob, type): |
| 33 | """Creates a Policy.""" |
| 34 | post_body = { |
| 35 | "blob": blob, |
| 36 | "type": type |
| 37 | } |
| 38 | post_body = json.dumps({'policy': post_body}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 39 | resp, body = self.post('policies', post_body) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 40 | self.expected_success(201, resp.status) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 41 | body = json.loads(body) |
David Kranz | 70f137c | 2014-10-23 17:57:18 -0400 | [diff] [blame] | 42 | return rest_client.ResponseBody(resp, body['policy']) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 43 | |
| 44 | def list_policies(self): |
| 45 | """Lists the policies.""" |
| 46 | resp, body = self.get('policies') |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 47 | self.expected_success(200, resp.status) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 48 | body = json.loads(body) |
David Kranz | 291bf79 | 2014-12-02 10:31:40 -0500 | [diff] [blame] | 49 | return rest_client.ResponseBodyList(resp, body['policies']) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 50 | |
| 51 | def get_policy(self, policy_id): |
| 52 | """Lists out the given policy.""" |
| 53 | url = 'policies/%s' % policy_id |
| 54 | resp, body = self.get(url) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 55 | self.expected_success(200, resp.status) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 56 | body = json.loads(body) |
David Kranz | 291bf79 | 2014-12-02 10:31:40 -0500 | [diff] [blame] | 57 | return rest_client.ResponseBody(resp, body['policy']) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 58 | |
| 59 | def update_policy(self, policy_id, **kwargs): |
| 60 | """Updates a policy.""" |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 61 | type = kwargs.get('type') |
| 62 | post_body = { |
| 63 | 'type': type |
| 64 | } |
| 65 | post_body = json.dumps({'policy': post_body}) |
| 66 | url = 'policies/%s' % policy_id |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 67 | resp, body = self.patch(url, post_body) |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 68 | self.expected_success(200, resp.status) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 69 | body = json.loads(body) |
David Kranz | 291bf79 | 2014-12-02 10:31:40 -0500 | [diff] [blame] | 70 | return rest_client.ResponseBody(resp, body['policy']) |
harika-vakadi | 40e1011 | 2013-02-08 14:38:09 +0530 | [diff] [blame] | 71 | |
| 72 | def delete_policy(self, policy_id): |
| 73 | """Deletes the policy.""" |
| 74 | url = "policies/%s" % policy_id |
David Kranz | 2aaf531 | 2014-08-29 09:22:10 -0400 | [diff] [blame] | 75 | resp, body = self.delete(url) |
| 76 | self.expected_success(204, resp.status) |
David Kranz | 291bf79 | 2014-12-02 10:31:40 -0500 | [diff] [blame] | 77 | return rest_client.ResponseBody(resp, body) |