blob: 3d98d99bbdd29aa3fbb842351c0ea57c0a11101d [file] [log] [blame]
harika-vakadi40e10112013-02-08 14:38:09 +05301# 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
16import json
17from urlparse import urlparse
18
19from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
21
22CONF = config.CONF
harika-vakadi40e10112013-02-08 14:38:09 +053023
24
25class PolicyClientJSON(RestClient):
26
Matthew Treinish684d8992014-01-30 16:27:40 +000027 def __init__(self, username, password, auth_url, tenant_name=None):
28 super(PolicyClientJSON, self).__init__(username, password,
harika-vakadi40e10112013-02-08 14:38:09 +053029 auth_url, tenant_name)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.identity.catalog_type
harika-vakadi40e10112013-02-08 14:38:09 +053031 self.endpoint_url = 'adminURL'
32
33 def request(self, method, url, headers=None, body=None, wait=None):
34 """Overriding the existing HTTP request in super class rest_client."""
35 self._set_auth()
36 self.base_url = self.base_url.replace(urlparse(self.base_url).path,
37 "/v3")
38 return super(PolicyClientJSON, self).request(method, url,
39 headers=headers,
40 body=body)
41
42 def create_policy(self, blob, type):
43 """Creates a Policy."""
44 post_body = {
45 "blob": blob,
46 "type": type
47 }
48 post_body = json.dumps({'policy': post_body})
49 resp, body = self.post('policies', post_body, self.headers)
50 body = json.loads(body)
51 return resp, body['policy']
52
53 def list_policies(self):
54 """Lists the policies."""
55 resp, body = self.get('policies')
56 body = json.loads(body)
57 return resp, body['policies']
58
59 def get_policy(self, policy_id):
60 """Lists out the given policy."""
61 url = 'policies/%s' % policy_id
62 resp, body = self.get(url)
63 body = json.loads(body)
64 return resp, body['policy']
65
66 def update_policy(self, policy_id, **kwargs):
67 """Updates a policy."""
68 resp, body = self.get_policy(policy_id)
69 type = kwargs.get('type')
70 post_body = {
71 'type': type
72 }
73 post_body = json.dumps({'policy': post_body})
74 url = 'policies/%s' % policy_id
75 resp, body = self.patch(url, post_body,
76 self.headers)
77 body = json.loads(body)
78 return resp, body['policy']
79
80 def delete_policy(self, policy_id):
81 """Deletes the policy."""
82 url = "policies/%s" % policy_id
83 return self.delete(url)