From c446f15f2e0292485d7f7ffac49fe7104be2e226 Mon Sep 17 00:00:00 2001 From: Quan Pham Date: Wed, 31 Jan 2024 12:30:12 -0500 Subject: [PATCH] openshift.py: Use super() in init and added logging --- src/coldfront_plugin_cloud/openshift.py | 50 +++++++++++-------------- test-requirements.txt | 1 - 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/coldfront_plugin_cloud/openshift.py b/src/coldfront_plugin_cloud/openshift.py index 2792df58..6f8d05fc 100644 --- a/src/coldfront_plugin_cloud/openshift.py +++ b/src/coldfront_plugin_cloud/openshift.py @@ -91,8 +91,7 @@ def __init__( resource: resource_models.Resource, allocation: allocation_models.Allocation, ): - self.resource = resource - self.allocation = allocation + super().__init__(resource, allocation) # Load Endpoint URL and Auth token for new k8 client var_name = utils.env_safe_name(self.resource.name) @@ -109,9 +108,11 @@ def __init__( k8_config.host = openshift_url if functional_tests == "true" or verify == "false": + self.logger = logging.getLogger() logger = logging.getLogger() k8_config.verify_ssl = False else: + self.logger = logging.getLogger("django") logger = logging.getLogger("django") k8_config.verify_ssl = True @@ -152,22 +153,14 @@ def disable_project(self, project_id): def reactivate_project(self, project_id): project_name = self.allocation.get_attribute(attributes.ALLOCATION_PROJECT_NAME) - try: - self._create_project(project_name, project_id) - except Conflict: - # This is a reactivation of an already active project - # most likely for a quota update - pass + self._create_project(project_name, project_id) def get_federated_user(self, username): - try: - if self.client.user_exists(username) and self.client.identity_exists( - username) and self.client.useridentitymapping_exists(username, username): - return {"username": username} + if self.client.user_exists(username) and self.client.identity_exists( + username) and self.client.useridentitymapping_exists(username, username): + return {"username": username} - raise NotFound("404: " + f"user ({username}) does not exist") - except NotFound: - pass + self.logger.info("404: " + f"user ({username}) does not exist") def create_federated_user(self, unique_id): try: @@ -189,10 +182,10 @@ def create_federated_user(self, unique_id): self.client.create_useridentitymapping(unique_id, id_user) if created: - return {"msg": f"user created ({unique_id})"} + self.logger.info(f"msg: user created ({unique_id})") + return - raise Conflict("400: " + f"user already exists ({unique_id})") - except Conflict: + except Exception: pass def assign_role_on_user(self, username, project_id): @@ -210,18 +203,17 @@ def remove_role_from_user(self, username, project_id): return self.client.remove_user_from_role( project_id, username, self.member_role_name ) - pass def _create_project(self, project_name, project_id): suggested_project_name = self.client.cnvt_project_name(project_name) if project_name != suggested_project_name: - raise ApiException( - "project name must match regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?'." + self.logger.info("400: " + + "project name must match regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?'." + f" Suggested name: {suggested_project_name}." ) if self.client.project_exists(project_name): - raise Conflict("project already exists.") + self.logger.info("409: project already exists.") display_name = project_name annotations = { @@ -238,7 +230,7 @@ def _create_project(self, project_name, project_id): annotations=annotations, labels=labels, ) - return {"msg": f"project created ({project_name})"} + self.logger.info(f"msg: project created ({project_name})") def _get_role(self, username, project_id): # /users//projects//roles/ @@ -246,9 +238,10 @@ def _get_role(self, username, project_id): if self.client.user_rolebinding_exists( username, project_id, self.member_role_name ): - return { - "msg": f"user role exists ({project_id},{username},{self.member_role_name})" - } + self.logger.info( + f"msg: user role exists ({project_id},{username},{self.member_role_name})" + ) + return raise NotFound( "404: " @@ -257,9 +250,10 @@ def _get_role(self, username, project_id): def _get_project(self, project_id): if self.client.project_exists(project_id): - return {"msg": f"project exists ({project_id})"} + self.logger.info(f"msg: project exists ({project_id})") + return - raise NotFound("400: " + f"project does not exist ({project_id})") + raise NotFound("404: " + f"project does not exist ({project_id})") def _delete_user(self, username): if self.client.user_exists(username): diff --git a/test-requirements.txt b/test-requirements.txt index 3cb6db23..456589c6 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,6 +7,5 @@ python-memcached==1.59 python-novaclient python-neutronclient python-swiftclient -gunicorn kubernetes openshift