Skip to content

Commit

Permalink
openshift.py: Use super() in init and added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
QuanMPhm committed Mar 12, 2024
1 parent b0f8d16 commit c446f15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
50 changes: 22 additions & 28 deletions src/coldfront_plugin_cloud/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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 = {
Expand All @@ -238,17 +230,18 @@ 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/<user_name>/projects/<project>/roles/<role>

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: "
Expand All @@ -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):
Expand Down
1 change: 0 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ python-memcached==1.59
python-novaclient
python-neutronclient
python-swiftclient
gunicorn
kubernetes
openshift

0 comments on commit c446f15

Please sign in to comment.