Skip to content

Commit

Permalink
add functional documentation and inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
velotioaastha committed Apr 25, 2024
1 parent 3afef6e commit 7bb328d
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 72 deletions.
90 changes: 89 additions & 1 deletion wandb-scim/custom_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@

class CustomRole(object):
def __init__(self, username, api_key):
"""
Initializes the CustomRole object with username and API key.
Args:
username (str): The username for authentication.
api_key (str): The API key for authentication.
"""
# Encode the username and API key into a base64-encoded string for Basic Authentication
auth_str = f"{username}:{api_key}"
auth_bytes = auth_str.encode('ascii')
self.auth_token = base64.b64encode(auth_bytes).decode('ascii')

# Create the authorization header for API requests
self.authorization_header = f"Basic {self.auth_token}"

def _create_custom_role(self, url, request_payload):
"""
Creates a new custom role.
Args:
url (str): The URL for the custom role creation endpoint.
request_payload (dict): The payload containing custom role data.
It should contain the following keys:
- 'permissionJson': The permissions JSON for the custom role.
- 'inheritedFrom': The inheritance information for the custom role.
Returns:
str: A message indicating whether the custom role creation was successful or failed.
"""
print("Creating the custom role")
data = {
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:Role"],
"name": "Sample custom role",
"description": "A sample custom role for example",
"permissions": [request_payload['permissionjson']],
"permissions": [request_payload['permissionJson']],
"inheritedFrom": request_payload['inheritedFrom']
}
headers = {
Expand All @@ -28,6 +51,15 @@ def _create_custom_role(self, url, request_payload):
return f"Custom role creation failed. Status code: {response.status_code}"

def _get_custom_role(self, url):
"""
Retrieves custom role details.
Args:
url (str): The URL for the custom role retrieval endpoint.
Returns:
str: A message containing custom role details or indicating failure.
"""
print("Getting the custom role")

headers = {
Expand All @@ -42,6 +74,15 @@ def _get_custom_role(self, url):
return f"Get custom role failed. Status code: {response.status_code}"

def _get_all_custom_role(self, url):
"""
Retrieves details of all custom roles in the organization.
Args:
url (str): The URL for the endpoint to get all custom roles.
Returns:
str: A message containing details of all custom roles or indicating failure.
"""
print("Getting all the custom roles in org")

headers = {
Expand All @@ -56,6 +97,18 @@ def _get_all_custom_role(self, url):
return f"Get all custom roles failed. Status code: {response.status_code}"

def _add_permission(self, url, request_payload):
"""
Adds permission to a custom role.
Args:
url (str): The URL for the endpoint to add permission to the custom role.
request_payload (dict): The payload containing permission information.
It should contain the following key:
- 'permissionJson': The permissions JSON to be added to the custom role.
Returns:
str: A message indicating whether the permission addition was successful or failed.
"""
print("Add permission to a custom role")
data = {
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
Expand Down Expand Up @@ -86,6 +139,18 @@ def _add_permission(self, url, request_payload):
return f"Failed to update custom role. Status code: {response.status_code}"

def _remove_permission(self, url, request_payload):
"""
Removes permission from a custom role.
Args:
url (str): The URL for the endpoint to remove permission from the custom role.
request_payload (dict): The payload containing permission information.
It should contain the following key:
- 'permissionJson': The permissions JSON to be removed from the custom role.
Returns:
str: A message indicating whether the permission removal was successful or failed.
"""
print("Remove permission from a custom role")
data = {
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
Expand Down Expand Up @@ -115,6 +180,20 @@ def _remove_permission(self, url, request_payload):
return f"Failed to update custom role. Status code: {response.status_code}"

def _update_custom_role(self, url, request_payload):
"""
Updates name and description of a custom role.
Args:
url (str): The URL for the endpoint to update the custom role.
request_payload (dict): The payload containing role information.
It should contain the following keys:
- 'roleName': The name of the custom role.
- 'roleDescription': The description of the custom role.
- 'inheritedFrom': The inheritance information for the custom role.
Returns:
str: A message indicating whether the custom role update was successful or failed.
"""
print("Update name and description of custom role")
data = {
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:Role"],
Expand All @@ -140,6 +219,15 @@ def _update_custom_role(self, url, request_payload):
return f"Failed to update custom role. Status code: {response.status_code}"

def _delete_custom_role(self, url):
"""
Deletes a custom role.
Args:
url (str): The URL for the endpoint to delete the custom role.
Returns:
str: A message indicating whether the custom role deletion was successful or failed.
"""
print("Deleting custom role")

headers = {
Expand Down
66 changes: 65 additions & 1 deletion wandb-scim/examples/custom_roles_example.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
# calling_module.py

import requests
import sys
sys.path.append('../')
from custom_roles import CustomRole # Assuming CustomRole class is defined in custom_role.py

def create_custom_role(base_url, custom_role, permission_json, inherited_from):
"""
Creates a new custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
permission_json (str): JSON string representing permissions for the custom role.
inherited_from (str): The source from which the custom role inherits permissions.
"""
try:
# Create a new custom role
create_role_response = custom_role._create_custom_role(
url=f"{base_url}/Roles",
request_payload={
"permissionjson": permission_json,
"permissionJson": permission_json,
"inheritedFrom": inherited_from
}
)
Expand All @@ -18,6 +29,14 @@ def create_custom_role(base_url, custom_role, permission_json, inherited_from):
print(f"Error occurred during API request: {str(e)}")

def get_custom_role(base_url, custom_role, role_id):
"""
Retrieves details of a specific custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
role_id (str): The ID of the custom role to retrieve.
"""
try:
# Get details of a custom role
get_role_response = custom_role._get_custom_role(f"{base_url}/Roles/{role_id}")
Expand All @@ -26,6 +45,13 @@ def get_custom_role(base_url, custom_role, role_id):
print(f"Error occurred during API request: {str(e)}")

def get_all_roles(base_url, custom_role):
"""
Retrieves details of all custom roles.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
"""
try:
# Get all custom roles
get_all_roles_response = custom_role._get_all_custom_role(f"{base_url}/Roles")
Expand All @@ -34,6 +60,15 @@ def get_all_roles(base_url, custom_role):
print(f"Error occurred during API request: {str(e)}")

def add_permission(base_url, custom_role, role_id, permission_json):
"""
Adds permission to a custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
role_id (str): The ID of the custom role to update.
permission_json (str): JSON string representing the permission to add.
"""
try:
# Add permission to a custom role
update_role_response = custom_role._add_permission(
Expand All @@ -47,6 +82,15 @@ def add_permission(base_url, custom_role, role_id, permission_json):
print(f"Error occurred during API request: {str(e)}")

def remove_permission(base_url, custom_role, role_id, permission_json):
"""
Removes permission from a custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
role_id (str): The ID of the custom role to update.
permission_json (str): JSON string representing the permission to remove.
"""
try:
# Remove permission from a custom role
remove_role_response = custom_role._remove_permission(
Expand All @@ -60,6 +104,17 @@ def remove_permission(base_url, custom_role, role_id, permission_json):
print(f"Error occurred during API request: {str(e)}")

def update_custom_role(base_url, custom_role, role_id, role_name, role_description, inherited_from):
"""
Updates a custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
role_id (str): The ID of the custom role to update.
role_name (str): The updated name of the custom role.
role_description (str): The updated description of the custom role.
inherited_from (str): The updated source from which the custom role inherits permissions.
"""
try:
# Update a custom role
update_custom_role_response = custom_role._update_custom_role(
Expand All @@ -75,6 +130,14 @@ def update_custom_role(base_url, custom_role, role_id, role_name, role_descripti
print(f"Error occurred during API request: {str(e)}")

def delete_custom_role(base_url, custom_role, role_id):
"""
Deletes a custom role.
Args:
base_url (str): The base URL of the API.
custom_role (CustomRole): An instance of the CustomRole class.
role_id (str): The ID of the custom role to delete.
"""
try:
# Delete the custom role
delete_role_response = custom_role._delete_custom_role(
Expand All @@ -91,4 +154,5 @@ def delete_custom_role(base_url, custom_role, role_id):

# Instantiate the CustomRole class with your credentials
custom_role = CustomRole(username, api_key)
# Retrieve details of all roles in the organization
get_all_roles(base_url, custom_role)
47 changes: 45 additions & 2 deletions wandb-scim/examples/teams_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# calling_module.py

import requests
import sys
sys.path.append('../')
from teams import Teams # Assuming the Teams class is defined in teams.py

def create_team(base_url, teams, display_name, member_id):
"""
Creates a new team.
Args:
base_url (str): The base URL of the API.
teams (Teams): An instance of the Teams class.
display_name (str): The display name of the new team.
member_id (str): The ID of the member to be added to the team.
"""
try:
# Create a new team
create_team_response = teams._create_team(
Expand All @@ -18,6 +29,14 @@ def create_team(base_url, teams, display_name, member_id):
print(f"Error occurred during API request: {str(e)}")

def get_team(base_url, teams, team_id):
"""
Retrieves details of a specific team.
Args:
base_url (str): The base URL of the API.
teams (Teams): An instance of the Teams class.
team_id (str): The ID of the team to retrieve.
"""
try:
# Get team details
get_team_response = teams._get_team(f"{base_url}/Groups/{team_id}")
Expand All @@ -26,6 +45,13 @@ def get_team(base_url, teams, team_id):
print(f"Error occurred during API request: {str(e)}")

def get_all_teams(base_url, teams):
"""
Retrieves details of all teams in the organization.
Args:
base_url (str): The base URL of the API.
teams (Teams): An instance of the Teams class.
"""
try:
# Get all teams in the organization
get_all_teams_response = teams._get_all_teams(f"{base_url}/Groups")
Expand All @@ -34,6 +60,15 @@ def get_all_teams(base_url, teams):
print(f"Error occurred during API request: {str(e)}")

def update_team_add_member(base_url, teams, team_id, member_id):
"""
Updates a team by adding a member.
Args:
base_url (str): The base URL of the API.
teams (Teams): An instance of the Teams class.
team_id (str): The ID of the team to update.
member_id (str): The ID of the member to add to the team.
"""
try:
# Update team by adding a member
update_team_response = teams._add_team(
Expand All @@ -45,6 +80,15 @@ def update_team_add_member(base_url, teams, team_id, member_id):
print(f"Error occurred during API request: {str(e)}")

def update_team_remove_member(base_url, teams, team_id, member_id):
"""
Updates a team by removing a member.
Args:
base_url (str): The base URL of the API.
teams (Teams): An instance of the Teams class.
team_id (str): The ID of the team to update.
member_id (str): The ID of the member to remove from the team.
"""
try:
# Update team by removing a member
update_team_response = teams._remove_team(
Expand All @@ -62,6 +106,5 @@ def update_team_remove_member(base_url, teams, team_id, member_id):

# Instantiate the Teams class with your credentials
teams = Teams(username, api_key)

# Test different methods
# Retrieve details of all teams
get_all_teams(base_url, teams)
Loading

0 comments on commit 7bb328d

Please sign in to comment.