Skip to content

Commit c446f15

Browse files
committed
openshift.py: Use super() in init and added logging
1 parent b0f8d16 commit c446f15

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

src/coldfront_plugin_cloud/openshift.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ def __init__(
9191
resource: resource_models.Resource,
9292
allocation: allocation_models.Allocation,
9393
):
94-
self.resource = resource
95-
self.allocation = allocation
94+
super().__init__(resource, allocation)
9695

9796
# Load Endpoint URL and Auth token for new k8 client
9897
var_name = utils.env_safe_name(self.resource.name)
@@ -109,9 +108,11 @@ def __init__(
109108
k8_config.host = openshift_url
110109

111110
if functional_tests == "true" or verify == "false":
111+
self.logger = logging.getLogger()
112112
logger = logging.getLogger()
113113
k8_config.verify_ssl = False
114114
else:
115+
self.logger = logging.getLogger("django")
115116
logger = logging.getLogger("django")
116117
k8_config.verify_ssl = True
117118

@@ -152,22 +153,14 @@ def disable_project(self, project_id):
152153

153154
def reactivate_project(self, project_id):
154155
project_name = self.allocation.get_attribute(attributes.ALLOCATION_PROJECT_NAME)
155-
try:
156-
self._create_project(project_name, project_id)
157-
except Conflict:
158-
# This is a reactivation of an already active project
159-
# most likely for a quota update
160-
pass
156+
self._create_project(project_name, project_id)
161157

162158
def get_federated_user(self, username):
163-
try:
164-
if self.client.user_exists(username) and self.client.identity_exists(
165-
username) and self.client.useridentitymapping_exists(username, username):
166-
return {"username": username}
159+
if self.client.user_exists(username) and self.client.identity_exists(
160+
username) and self.client.useridentitymapping_exists(username, username):
161+
return {"username": username}
167162

168-
raise NotFound("404: " + f"user ({username}) does not exist")
169-
except NotFound:
170-
pass
163+
self.logger.info("404: " + f"user ({username}) does not exist")
171164

172165
def create_federated_user(self, unique_id):
173166
try:
@@ -189,10 +182,10 @@ def create_federated_user(self, unique_id):
189182
self.client.create_useridentitymapping(unique_id, id_user)
190183

191184
if created:
192-
return {"msg": f"user created ({unique_id})"}
185+
self.logger.info(f"msg: user created ({unique_id})")
186+
return
193187

194-
raise Conflict("400: " + f"user already exists ({unique_id})")
195-
except Conflict:
188+
except Exception:
196189
pass
197190

198191
def assign_role_on_user(self, username, project_id):
@@ -210,18 +203,17 @@ def remove_role_from_user(self, username, project_id):
210203
return self.client.remove_user_from_role(
211204
project_id, username, self.member_role_name
212205
)
213-
pass
214206

215207
def _create_project(self, project_name, project_id):
216208
suggested_project_name = self.client.cnvt_project_name(project_name)
217209
if project_name != suggested_project_name:
218-
raise ApiException(
219-
"project name must match regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?'."
210+
self.logger.info("400: " +
211+
"project name must match regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?'." +
220212
f" Suggested name: {suggested_project_name}."
221213
)
222214

223215
if self.client.project_exists(project_name):
224-
raise Conflict("project already exists.")
216+
self.logger.info("409: project already exists.")
225217

226218
display_name = project_name
227219
annotations = {
@@ -238,17 +230,18 @@ def _create_project(self, project_name, project_id):
238230
annotations=annotations,
239231
labels=labels,
240232
)
241-
return {"msg": f"project created ({project_name})"}
233+
self.logger.info(f"msg: project created ({project_name})")
242234

243235
def _get_role(self, username, project_id):
244236
# /users/<user_name>/projects/<project>/roles/<role>
245237

246238
if self.client.user_rolebinding_exists(
247239
username, project_id, self.member_role_name
248240
):
249-
return {
250-
"msg": f"user role exists ({project_id},{username},{self.member_role_name})"
251-
}
241+
self.logger.info(
242+
f"msg: user role exists ({project_id},{username},{self.member_role_name})"
243+
)
244+
return
252245

253246
raise NotFound(
254247
"404: "
@@ -257,9 +250,10 @@ def _get_role(self, username, project_id):
257250

258251
def _get_project(self, project_id):
259252
if self.client.project_exists(project_id):
260-
return {"msg": f"project exists ({project_id})"}
253+
self.logger.info(f"msg: project exists ({project_id})")
254+
return
261255

262-
raise NotFound("400: " + f"project does not exist ({project_id})")
256+
raise NotFound("404: " + f"project does not exist ({project_id})")
263257

264258
def _delete_user(self, username):
265259
if self.client.user_exists(username):

test-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ python-memcached==1.59
77
python-novaclient
88
python-neutronclient
99
python-swiftclient
10-
gunicorn
1110
kubernetes
1211
openshift

0 commit comments

Comments
 (0)