Skip to content

Commit

Permalink
Updated examples to match 0.56 SCIM API changes (#543)
Browse files Browse the repository at this point in the history
* added activate and delete user methods, updated deactivate to match 0.56 SCIM API updates

* added activate and delete user methods, updated deactivate to match 0.56 SCIM API updates
  • Loading branch information
mkaesz-wandb authored Jul 2, 2024
1 parent 4df2e4b commit c37f87f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
32 changes: 32 additions & 0 deletions wandb-scim/examples/user_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -121,5 +151,7 @@ def assign_team_role_to_user(user, user_id, team_name, role_name):
# create_user(user, "[email protected]", "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")
77 changes: 74 additions & 3 deletions wandb-scim/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c37f87f

Please sign in to comment.