Skip to content

Commit 88423bc

Browse files
committed
Make set_user_application_permission idempotent
https://jsw.ibm.com/browse/MASCORE-6072
1 parent af1dbf5 commit 88423bc

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

bin/mas-devops-create-initial-users-for-saas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ if __name__ == "__main__":
6969
try:
7070
# Try to load in-cluster configuration
7171
config.load_incluster_config()
72-
logger.info("Loaded in-cluster configuration")
72+
logger.debug("Loaded in-cluster configuration")
7373
except ConfigException:
7474
# If that fails, fall back to kubeconfig file
7575
config.load_kube_config()
76-
logger.info("Loaded kubeconfig file")
76+
logger.debug("Loaded kubeconfig file")
7777

7878
user_utils = MASUserUtils(mas_instance_id, mas_workspace_id, client.api_client.ApiClient())
7979

src/mas/devops/users.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ def get_or_create_user(self, payload):
287287
"familyName": family_name
288288
}
289289
'''
290-
self.logger.info(f"Creating (or getting) user {payload["id"]}")
291290
existing_user = self.get_user(payload["id"])
292291

293292
if existing_user is not None:
@@ -436,10 +435,38 @@ def add_user_to_workspace(self, user_id, is_workspace_admin=False):
436435

437436
raise Exception(f"{response.status_code} {response.text}")
438437

438+
def get_user_application_permissions(self, user_id, application_id):
439+
self.logger.debug(f"Getting user {user_id} permissions for application {application_id}")
440+
url = f"{self.mas_api_url}/workspaces/{self.mas_workspace_id}/applications/{application_id}/users/{user_id}"
441+
headers = {
442+
"Accept": "application/json",
443+
"x-access-token": self.superuser_auth_token
444+
}
445+
response = requests.get(
446+
url,
447+
headers=headers,
448+
verify=self.mas_api_url_ca_chain_file_path
449+
)
450+
451+
if response.status_code == 200:
452+
return response.json()
453+
454+
if response.status_code == 404:
455+
return None
456+
457+
raise Exception(f"{response.status_code} {response.text}")
458+
439459
def set_user_application_permission(self, user_id, application_id, role):
440460
'''
441-
TODO: idempotency
461+
No-op if user already has a role established for the application. No attempt will be made to update the role if it differs.
442462
'''
463+
464+
existing_permissions = self.get_user_application_permissions(user_id, application_id)
465+
466+
if existing_permissions is not None:
467+
self.logger.info(f"User {user_id} already has permissions set for application {application_id}")
468+
return None
469+
443470
self.logger.info(f"Setting user {user_id} role for {application_id} to {role}")
444471
url = f"{self.mas_api_url}/workspaces/{self.mas_workspace_id}/applications/{application_id}/users/{user_id}"
445472
querystring = {}
@@ -457,7 +484,11 @@ def set_user_application_permission(self, user_id, application_id, role):
457484
params=querystring,
458485
verify=self.mas_api_url_ca_chain_file_path
459486
)
460-
return response.json()
487+
488+
if response.status_code == 200:
489+
return None
490+
491+
raise Exception(f"{response.status_code} {response.text}")
461492

462493
def check_user_sync(self, user_id, application_id, timeout_secs=60 * 10):
463494
t_end = time.time() + timeout_secs

0 commit comments

Comments
 (0)