| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from keystoneclient import base |
| 16 |
from keystoneclient import exceptions |
| 17 |
from keystoneclient.i18n import _ |
| 18 |
from keystoneclient.v3 import endpoint_groups |
| 19 |
from keystoneclient.v3 import endpoints |
| 20 |
from keystoneclient.v3 import projects |
| 21 |
|
| 22 |
|
| 23 |
class EndpointFilterManager(base.Manager): |
| 24 |
"""Manager class for manipulating project-endpoint associations. |
| 25 |
|
| 26 |
Project-endpoint associations can be with endpoints directly or via |
| 27 |
endpoint groups. |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
OS_EP_FILTER_EXT = '/OS-EP-FILTER' |
| 32 |
|
| 33 |
def _build_base_url(self, project=None, endpoint=None): |
| 34 |
project_id = base.getid(project) |
| 35 |
endpoint_id = base.getid(endpoint) |
| 36 |
|
| 37 |
if project_id and endpoint_id: |
| 38 |
api_path = '/projects/%s/endpoints/%s' % (project_id, endpoint_id) |
| 39 |
elif project_id: |
| 40 |
api_path = '/projects/%s/endpoints' % (project_id) |
| 41 |
elif endpoint_id: |
| 42 |
api_path = '/endpoints/%s/projects' % (endpoint_id) |
| 43 |
else: |
| 44 |
msg = _('Must specify a project, an endpoint, or both') |
| 45 |
raise exceptions.ValidationError(msg) |
| 46 |
|
| 47 |
return self.OS_EP_FILTER_EXT + api_path |
| 48 |
|
| 49 |
def _build_group_base_url(self, project=None, endpoint_group=None): |
| 50 |
project_id = base.getid(project) |
| 51 |
endpoint_group_id = base.getid(endpoint_group) |
| 52 |
|
| 53 |
if project_id and endpoint_group_id: |
| 54 |
api_path = '/endpoint_groups/%s/projects/%s' % ( |
| 55 |
endpoint_group_id, project_id) |
| 56 |
elif project_id: |
| 57 |
api_path = '/projects/%s/endpoint_groups' % (project_id) |
| 58 |
elif endpoint_group_id: |
| 59 |
api_path = '/endpoint_groups/%s/projects' % (endpoint_group_id) |
| 60 |
else: |
| 61 |
msg = _('Must specify a project, an endpoint group, or both') |
| 62 |
raise exceptions.ValidationError(msg) |
| 63 |
|
| 64 |
return self.OS_EP_FILTER_EXT + api_path |
| 65 |
|
| 66 |
def add_endpoint_to_project(self, project, endpoint): |
| 67 |
"""Create a project-endpoint association.""" |
| 68 |
if not (project and endpoint): |
| 69 |
raise ValueError(_('project and endpoint are required')) |
| 70 |
|
| 71 |
base_url = self._build_base_url(project=project, |
| 72 |
endpoint=endpoint) |
| 73 |
return super(EndpointFilterManager, self)._put(url=base_url) |
| 74 |
|
| 75 |
def delete_endpoint_from_project(self, project, endpoint): |
| 76 |
"""Remove a project-endpoint association.""" |
| 77 |
if not (project and endpoint): |
| 78 |
raise ValueError(_('project and endpoint are required')) |
| 79 |
|
| 80 |
base_url = self._build_base_url(project=project, |
| 81 |
endpoint=endpoint) |
| 82 |
return super(EndpointFilterManager, self)._delete(url=base_url) |
| 83 |
|
| 84 |
def check_endpoint_in_project(self, project, endpoint): |
| 85 |
"""Check if project-endpoint association exists.""" |
| 86 |
if not (project and endpoint): |
| 87 |
raise ValueError(_('project and endpoint are required')) |
| 88 |
|
| 89 |
base_url = self._build_base_url(project=project, |
| 90 |
endpoint=endpoint) |
| 91 |
return super(EndpointFilterManager, self)._head(url=base_url) |
| 92 |
|
| 93 |
def list_endpoints_for_project(self, project): |
| 94 |
"""List all endpoints for a given project.""" |
| 95 |
if not project: |
| 96 |
raise ValueError(_('project is required')) |
| 97 |
|
| 98 |
base_url = self._build_base_url(project=project) |
| 99 |
return super(EndpointFilterManager, self)._list( |
| 100 |
base_url, |
| 101 |
endpoints.EndpointManager.collection_key, |
| 102 |
obj_class=endpoints.EndpointManager.resource_class) |
| 103 |
|
| 104 |
def list_projects_for_endpoint(self, endpoint): |
| 105 |
"""List all projects for a given endpoint.""" |
| 106 |
if not endpoint: |
| 107 |
raise ValueError(_('endpoint is required')) |
| 108 |
|
| 109 |
base_url = self._build_base_url(endpoint=endpoint) |
| 110 |
return super(EndpointFilterManager, self)._list( |
| 111 |
base_url, |
| 112 |
projects.ProjectManager.collection_key, |
| 113 |
obj_class=projects.ProjectManager.resource_class) |
| 114 |
|
| 115 |
def add_endpoint_group_to_project(self, endpoint_group, project): |
| 116 |
"""Create a project-endpoint group association.""" |
| 117 |
if not (project and endpoint_group): |
| 118 |
raise ValueError(_('project and endpoint_group are required')) |
| 119 |
|
| 120 |
base_url = self._build_group_base_url(project=project, |
| 121 |
endpoint_group=endpoint_group) |
| 122 |
return super(EndpointFilterManager, self)._put(url=base_url) |
| 123 |
|
| 124 |
def delete_endpoint_group_from_project(self, endpoint_group, project): |
| 125 |
"""Remove a project-endpoint group association.""" |
| 126 |
if not (project and endpoint_group): |
| 127 |
raise ValueError(_('project and endpoint_group are required')) |
| 128 |
|
| 129 |
base_url = self._build_group_base_url(project=project, |
| 130 |
endpoint_group=endpoint_group) |
| 131 |
return super(EndpointFilterManager, self)._delete(url=base_url) |
| 132 |
|
| 133 |
def check_endpoint_group_in_project(self, endpoint_group, project): |
| 134 |
"""Check if project-endpoint group association exists.""" |
| 135 |
if not (project and endpoint_group): |
| 136 |
raise ValueError(_('project and endpoint_group are required')) |
| 137 |
|
| 138 |
base_url = self._build_group_base_url(project=project, |
| 139 |
endpoint_group=endpoint_group) |
| 140 |
return super(EndpointFilterManager, self)._head(url=base_url) |
| 141 |
|
| 142 |
def list_endpoint_groups_for_project(self, project): |
| 143 |
"""List all endpoint groups for a given project.""" |
| 144 |
if not project: |
| 145 |
raise ValueError(_('project is required')) |
| 146 |
|
| 147 |
base_url = self._build_group_base_url(project=project) |
| 148 |
|
| 149 |
return super(EndpointFilterManager, self)._list( |
| 150 |
base_url, |
| 151 |
'endpoint_groups', |
| 152 |
obj_class=endpoint_groups.EndpointGroupManager.resource_class) |
| 153 |
|
| 154 |
def list_projects_for_endpoint_group(self, endpoint_group): |
| 155 |
"""List all projects associated with a given endpoint group.""" |
| 156 |
if not endpoint_group: |
| 157 |
raise ValueError(_('endpoint_group is required')) |
| 158 |
|
| 159 |
base_url = self._build_group_base_url(endpoint_group=endpoint_group) |
| 160 |
return super(EndpointFilterManager, self)._list( |
| 161 |
base_url, |
| 162 |
projects.ProjectManager.collection_key, |
| 163 |
obj_class=projects.ProjectManager.resource_class) |