diff --git a/wandb-scim/examples/user_example.py b/wandb-scim/examples/user_example.py index 3ceda7ec..928c6a03 100644 --- a/wandb-scim/examples/user_example.py +++ b/wandb-scim/examples/user_example.py @@ -52,6 +52,21 @@ def get_all_users(user): except requests.exceptions.RequestException as e: print(f"Error occurred during API request: {str(e)}") +def activate_user(user, user_id): + """ + Activates a user. + + Args: + user (User): An instance of the User class. + user_id (str): The ID of the user to activate. + """ + try: + # Activate a user + activate_user_response = user.activate(user_id) + print(activate_user_response) + except requests.exceptions.RequestException as e: + print(f"Error occurred during API request: {str(e)}") + def deactivate_user(user, user_id): """ Deactivates a user. @@ -67,6 +82,21 @@ def deactivate_user(user, user_id): except requests.exceptions.RequestException as e: print(f"Error occurred during API request: {str(e)}") +def delete_user(user, user_id): + """ + Deletes a user. + + Args: + user (User): An instance of the User class. + user_id (str): The ID of the user to delete. + """ + try: + # Delete a user + delete_user_response = user.delete(user_id) + print(delete_user_response) + except requests.exceptions.RequestException as e: + print(f"Error occurred during API request: {str(e)}") + def assign_org_role_to_user(user, user_id, role_name): """ Assigns a org-level role to a user. @@ -121,5 +151,7 @@ def assign_team_role_to_user(user, user_id, team_name, role_name): # create_user(user, "test@example.com", "Test User") # get_user(user, "user_id") # deactivate_user(user, "user_id") + # activate_user(user, "user_id") + # delete_user(user, "user_id") # assign_org_role_to_user(user, "user_id", "role_name") # assign_team_role_to_user(user, "user_id", "team_name", "role_name") \ No newline at end of file diff --git a/wandb-scim/users.py b/wandb-scim/users.py index e9397ba3..8e40e99f 100644 --- a/wandb-scim/users.py +++ b/wandb-scim/users.py @@ -98,6 +98,41 @@ def get_all(self): return json.dumps(response.text, indent=4) return f"Get users failed. Status code: {response.status_code}" + def activate(self, user_id): + """ + Activates a user. + + Args: + user_id (str): user_id of the user. + + Returns: + str: A message indicating whether the user activation was successful or failed. + """ + print("Activating the User") + headers = { + "Authorization": self.authorization_header, + "Content-Type": "application/json" + } + # Send a PATCH request to activate the user + url = f"{self.base_url}/scim/Users/{user_id}" + payload = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [ + { + "op": "replace", + "value": {"active": True} + } + ] + } + response = requests.patch(url, headers=headers, json=payload) + + if response.status_code == 200: + return "User activated successfully!" + elif response.status_code == 404: + return "User not found" + else: + return f"Failed to activate user. Status code: {response.status_code}" + def deactivate(self, user_id): """ Deactivates a user. @@ -108,17 +143,52 @@ def deactivate(self, user_id): Returns: str: A message indicating whether the user deactivation was successful or failed. """ - print("deleting the User") + print("Deactivating the User") headers = { "Authorization": self.authorization_header, "Content-Type": "application/json" } - # Send a DELETE request to deactivate the user + # Send a PATCH request to deactivate the user + url = f"{self.base_url}/scim/Users/{user_id}" + payload = { + "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + "Operations": [ + { + "op": "replace", + "value": {"active": False} + } + ] + } + response = requests.patch(url, headers=headers, json=payload) + + if response.status_code == 200: + return "User deactivated successfully!" + elif response.status_code == 404: + return "User not found" + else: + return f"Failed to deactivate user. Status code: {response.status_code}" + + def delete(self, user_id): + """ + Delete a user. + + Args: + user_id (str): user_id of the user. + + Returns: + str: A message indicating whether the user deletion was successful or failed. + """ + print("Delete the User") + headers = { + "Authorization": self.authorization_header, + "Content-Type": "application/json" + } + # Send a DELETE request to delete the user url = f"{self.base_url}/scim/Users/{user_id}" response = requests.delete(url, headers=headers) if response.status_code == 204: - return "User has deleted successfully!" + return "User deleted successfully!" elif response.status_code == 404: return "User not found" else: @@ -155,6 +225,7 @@ def assign_org_role(self, user_id, request_payload): # Send a PATCH request to assign the role to the user url = f"{self.base_url}/scim/Users/{user_id}" response = requests.patch(url, json=data, headers=headers) + if response.status_code == 200: updated_data = response.json() # Get the updated resource data from the response print("Updated Data:", updated_data)