| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from keystoneclient import base |
| 16 |
from keystoneclient.i18n import _ |
| 17 |
from keystoneclient.v3 import endpoints |
| 18 |
from keystoneclient.v3 import policies |
| 19 |
|
| 20 |
|
| 21 |
class EndpointPolicyManager(base.Manager): |
| 22 |
"""Manager class for manipulating endpoint-policy associations.""" |
| 23 |
|
| 24 |
OS_EP_POLICY_EXT = 'OS-ENDPOINT-POLICY' |
| 25 |
|
| 26 |
def _act_on_policy_association_for_endpoint( |
| 27 |
self, policy, endpoint, action): |
| 28 |
if not (policy and endpoint): |
| 29 |
raise ValueError(_('policy and endpoint are required')) |
| 30 |
|
| 31 |
policy_id = base.getid(policy) |
| 32 |
endpoint_id = base.getid(endpoint) |
| 33 |
url = ('/policies/%(policy_id)s/%(ext_name)s' |
| 34 |
'/endpoints/%(endpoint_id)s') % { |
| 35 |
'policy_id': policy_id, |
| 36 |
'ext_name': self.OS_EP_POLICY_EXT, |
| 37 |
'endpoint_id': endpoint_id} |
| 38 |
return action(url=url) |
| 39 |
|
| 40 |
def create_policy_association_for_endpoint(self, policy, endpoint): |
| 41 |
"""Create an association between a policy and an endpoint.""" |
| 42 |
return self._act_on_policy_association_for_endpoint( |
| 43 |
policy, endpoint, self._put) |
| 44 |
|
| 45 |
def check_policy_association_for_endpoint(self, policy, endpoint): |
| 46 |
"""Check an association between a policy and an endpoint.""" |
| 47 |
return self._act_on_policy_association_for_endpoint( |
| 48 |
policy, endpoint, self._head) |
| 49 |
|
| 50 |
def delete_policy_association_for_endpoint(self, policy, endpoint): |
| 51 |
"""Delete an association between a policy and an endpoint.""" |
| 52 |
return self._act_on_policy_association_for_endpoint( |
| 53 |
policy, endpoint, self._delete) |
| 54 |
|
| 55 |
def _act_on_policy_association_for_service(self, policy, service, action): |
| 56 |
if not (policy and service): |
| 57 |
raise ValueError(_('policy and service are required')) |
| 58 |
|
| 59 |
policy_id = base.getid(policy) |
| 60 |
service_id = base.getid(service) |
| 61 |
url = ('/policies/%(policy_id)s/%(ext_name)s' |
| 62 |
'/services/%(service_id)s') % { |
| 63 |
'policy_id': policy_id, |
| 64 |
'ext_name': self.OS_EP_POLICY_EXT, |
| 65 |
'service_id': service_id} |
| 66 |
return action(url=url) |
| 67 |
|
| 68 |
def create_policy_association_for_service(self, policy, service): |
| 69 |
"""Create an association between a policy and a service.""" |
| 70 |
return self._act_on_policy_association_for_service( |
| 71 |
policy, service, self._put) |
| 72 |
|
| 73 |
def check_policy_association_for_service(self, policy, service): |
| 74 |
"""Check an association between a policy and a service.""" |
| 75 |
return self._act_on_policy_association_for_service( |
| 76 |
policy, service, self._head) |
| 77 |
|
| 78 |
def delete_policy_association_for_service(self, policy, service): |
| 79 |
"""Delete an association between a policy and a service.""" |
| 80 |
return self._act_on_policy_association_for_service( |
| 81 |
policy, service, self._delete) |
| 82 |
|
| 83 |
def _act_on_policy_association_for_region_and_service( |
| 84 |
self, policy, region, service, action): |
| 85 |
if not (policy and region and service): |
| 86 |
raise ValueError(_('policy, region and service are required')) |
| 87 |
|
| 88 |
policy_id = base.getid(policy) |
| 89 |
region_id = base.getid(region) |
| 90 |
service_id = base.getid(service) |
| 91 |
url = ('/policies/%(policy_id)s/%(ext_name)s' |
| 92 |
'/services/%(service_id)s/regions/%(region_id)s') % { |
| 93 |
'policy_id': policy_id, |
| 94 |
'ext_name': self.OS_EP_POLICY_EXT, |
| 95 |
'service_id': service_id, |
| 96 |
'region_id': region_id} |
| 97 |
return action(url=url) |
| 98 |
|
| 99 |
def create_policy_association_for_region_and_service( |
| 100 |
self, policy, region, service): |
| 101 |
"""Create an association between a policy and a service in a region.""" |
| 102 |
return self._act_on_policy_association_for_region_and_service( |
| 103 |
policy, region, service, self._put) |
| 104 |
|
| 105 |
def check_policy_association_for_region_and_service( |
| 106 |
self, policy, region, service): |
| 107 |
"""Check an association between a policy and a service in a region.""" |
| 108 |
return self._act_on_policy_association_for_region_and_service( |
| 109 |
policy, region, service, self._head) |
| 110 |
|
| 111 |
def delete_policy_association_for_region_and_service( |
| 112 |
self, policy, region, service): |
| 113 |
"""Delete an association between a policy and a service in a region.""" |
| 114 |
return self._act_on_policy_association_for_region_and_service( |
| 115 |
policy, region, service, self._delete) |
| 116 |
|
| 117 |
def get_policy_for_endpoint(self, endpoint): |
| 118 |
"""Get the effective policy for an endpoint. |
| 119 |
|
| 120 |
:param endpoint: endpoint object or ID |
| 121 |
|
| 122 |
:returns: policies.Policy object |
| 123 |
|
| 124 |
""" |
| 125 |
if not endpoint: |
| 126 |
raise ValueError(_('endpoint is required')) |
| 127 |
|
| 128 |
endpoint_id = base.getid(endpoint) |
| 129 |
url = ('/endpoints/%(endpoint_id)s/%(ext_name)s/policy') % { |
| 130 |
'endpoint_id': endpoint_id, |
| 131 |
'ext_name': self.OS_EP_POLICY_EXT} |
| 132 |
|
| 133 |
resp, body = self.client.get(url) |
| 134 |
return self._prepare_return_value( |
| 135 |
resp, policies.Policy(self, body[policies.PolicyManager.key], |
| 136 |
loaded=True)) |
| 137 |
|
| 138 |
def list_endpoints_for_policy(self, policy): |
| 139 |
"""List endpoints with the effective association to a policy. |
| 140 |
|
| 141 |
:param policy: policy object or ID |
| 142 |
|
| 143 |
:returns: list of endpoints that are associated with the policy |
| 144 |
|
| 145 |
""" |
| 146 |
if not policy: |
| 147 |
raise ValueError(_('policy is required')) |
| 148 |
|
| 149 |
policy_id = base.getid(policy) |
| 150 |
url = ('/policies/%(policy_id)s/%(ext_name)s/endpoints') % { |
| 151 |
'policy_id': policy_id, |
| 152 |
'ext_name': self.OS_EP_POLICY_EXT} |
| 153 |
return self._list( |
| 154 |
url, |
| 155 |
endpoints.EndpointManager.collection_key, |
| 156 |
obj_class=endpoints.EndpointManager.resource_class) |