refactor: move the common code to manager layer

There are some methods (`_get_endpoint_groups_for_project` and
`_get_endpoints_filtered_by_endpoint_group`) needed both by catalog
controller and manager, move those methods into manager so that
controller can call these methods directly from manager.

Change-Id: I3a82c606d62bc2ad54f7454cd6ee4dbce0b88219
Related-Bug: #1516469
This commit is contained in:
Dave Chen 2015-12-09 12:59:37 +08:00
parent f86448a311
commit 2a10954b5e
2 changed files with 14 additions and 43 deletions

View File

@ -385,40 +385,7 @@ class EndpointV3(controller.V3Controller):
@dependency.requires('catalog_api', 'resource_api')
class _ControllerBase(controller.V3Controller):
"""Base behaviors for endpoint filter controllers."""
def _get_endpoint_groups_for_project(self, project_id):
# recover the project endpoint group memberships and for each
# membership recover the endpoint group
self.resource_api.get_project(project_id)
try:
refs = self.catalog_api.list_endpoint_groups_for_project(
project_id)
endpoint_groups = [self.catalog_api.get_endpoint_group(
ref['endpoint_group_id']) for ref in refs]
return endpoint_groups
except exception.EndpointGroupNotFound:
return []
def _get_endpoints_filtered_by_endpoint_group(self, endpoint_group_id):
endpoints = self.catalog_api.list_endpoints()
filters = self.catalog_api.get_endpoint_group(
endpoint_group_id)['filters']
filtered_endpoints = []
for endpoint in endpoints:
is_candidate = True
for key, value in filters.items():
if endpoint[key] != value:
is_candidate = False
break
if is_candidate:
filtered_endpoints.append(endpoint)
return filtered_endpoints
class EndpointFilterV3Controller(_ControllerBase):
class EndpointFilterV3Controller(controller.V3Controller):
def __init__(self):
super(EndpointFilterV3Controller, self).__init__()
@ -487,7 +454,8 @@ class EndpointFilterV3Controller(_ControllerBase):
projects)
class EndpointGroupV3Controller(_ControllerBase):
@dependency.requires('catalog_api', 'resource_api')
class EndpointGroupV3Controller(controller.V3Controller):
collection_name = 'endpoint_groups'
member_name = 'endpoint_group'
@ -559,7 +527,8 @@ class EndpointGroupV3Controller(_ControllerBase):
def list_endpoint_groups_for_project(self, context, project_id):
"""List all endpoint groups associated with a given project."""
return EndpointGroupV3Controller.wrap_collection(
context, self._get_endpoint_groups_for_project(project_id))
context,
self.catalog_api.get_endpoint_groups_for_project(project_id))
@controller.protected()
def list_projects_associated_with_endpoint_group(self,
@ -583,12 +552,14 @@ class EndpointGroupV3Controller(_ControllerBase):
context,
endpoint_group_id):
"""List all the endpoints filtered by a specific endpoint group."""
filtered_endpoints = self._get_endpoints_filtered_by_endpoint_group(
endpoint_group_id)
filtered_endpoints = (self.catalog_api.
get_endpoints_filtered_by_endpoint_group(
endpoint_group_id))
return EndpointV3.wrap_collection(context, filtered_endpoints)
class ProjectEndpointGroupV3Controller(_ControllerBase):
@dependency.requires('catalog_api', 'resource_api')
class ProjectEndpointGroupV3Controller(controller.V3Controller):
collection_name = 'project_endpoint_groups'
member_name = 'project_endpoint_group'

View File

@ -322,7 +322,7 @@ class Manager(manager.Manager):
endpoint_group_id, project_id)
COMPUTED_CATALOG_REGION.invalidate()
def _get_endpoint_groups_for_project(self, project_id):
def get_endpoint_groups_for_project(self, project_id):
# recover the project endpoint group memberships and for each
# membership recover the endpoint group
self.resource_api.get_project(project_id)
@ -335,7 +335,7 @@ class Manager(manager.Manager):
except exception.EndpointGroupNotFound:
return []
def _get_endpoints_filtered_by_endpoint_group(self, endpoint_group_id):
def get_endpoints_filtered_by_endpoint_group(self, endpoint_group_id):
endpoints = self.list_endpoints()
filters = self.driver.get_endpoint_group(endpoint_group_id)['filters']
filtered_endpoints = []
@ -371,9 +371,9 @@ class Manager(manager.Manager):
# need to recover endpoint_groups associated with project
# then for each endpoint group return the endpoints.
endpoint_groups = self._get_endpoint_groups_for_project(project_id)
endpoint_groups = self.get_endpoint_groups_for_project(project_id)
for endpoint_group in endpoint_groups:
endpoint_refs = self._get_endpoints_filtered_by_endpoint_group(
endpoint_refs = self.get_endpoints_filtered_by_endpoint_group(
endpoint_group['id'])
# now check if any endpoints for current endpoint group are not
# contained in the list of filtered endpoints