Skip to content

Commit

Permalink
fixed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Aastha Gupta authored and Aastha Gupta committed May 6, 2024
1 parent 58396a2 commit f5cda6f
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 150 deletions.
54 changes: 27 additions & 27 deletions wandb-scim/custom_roles.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import requests
import base64
import json
import requests

class CustomRole(object):
def __init__(self, username, api_key):
def __init__(self, base_url, username, api_key):
"""
Initializes the CustomRole object with username and API key.
Args:
base_url (str): Host url.
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')
base_url = base_url
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):
def create(self, 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.
Expand All @@ -44,18 +46,19 @@ def _create_custom_role(self, url, request_payload):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}
url = f"{self.base_url}/scim/Roles"
response = requests.post(url, json=data, headers=headers)

if response.status_code == 201:
return "Custom role has been created!"
return f"Custom role creation failed. Status code: {response.status_code}"

def _get_custom_role(self, url):
def get(self, role_id):
"""
Retrieves custom role details.
Retrieves custom role details for role_id.
Args:
url (str): The URL for the custom role retrieval endpoint.
role_id (str): role_id from the custom role.
Returns:
str: A message containing custom role details or indicating failure.
Expand All @@ -66,20 +69,17 @@ def _get_custom_role(self, url):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}

url = f"{self.base_url}/scim/Roles/{role_id}"
response = requests.get(url, headers=headers)

if response.status_code == 200:
return f"Custom role details: {response.text}"
return json.dumps(response.json, indent=4)
return f"Get custom role failed. Status code: {response.status_code}"

def _get_all_custom_role(self, url):
def get_all(self):
"""
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.
"""
Expand All @@ -89,19 +89,19 @@ def _get_all_custom_role(self, url):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}

url = f"{self.base_url}/scim/Roles"
response = requests.get(url, headers=headers)

if response.status_code == 200:
return f"All the custom roles details: {response.text}"
return json.dumps(response.json, indent=4)
return f"Get all custom roles failed. Status code: {response.status_code}"

def _add_permission(self, url, request_payload):
def add_permissions(self, role_id, request_payload):
"""
Adds permission to a custom role.
Args:
url (str): The URL for the endpoint to add permission to the custom role.
role_id (str): role_id from 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.
Expand All @@ -120,7 +120,7 @@ def _add_permission(self, url, request_payload):
}
]
}

url = f"{self.base_url}/scim/Roles/{role_id}"
headers = {
"Authorization": self.authorization_header,
"Content-Type": "application/json"
Expand All @@ -138,12 +138,12 @@ def _add_permission(self, url, request_payload):
else:
return f"Failed to update custom role. Status code: {response.status_code}"

def _remove_permission(self, url, request_payload):
def remove_permission(self, role_id, request_payload):
"""
Removes permission from a custom role.
Args:
url (str): The URL for the endpoint to remove permission from the custom role.
role_id (str): role_id 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.
Expand All @@ -166,7 +166,7 @@ def _remove_permission(self, url, request_payload):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}

url = f"{self.base_url}/scim/Roles/{role_id}"
response = requests.patch(url, json=data, headers=headers)

if response.status_code == 200:
Expand All @@ -179,12 +179,12 @@ def _remove_permission(self, url, request_payload):
else:
return f"Failed to update custom role. Status code: {response.status_code}"

def _update_custom_role(self, url, request_payload):
def update_custom_role(self, role_id, request_payload):
"""
Updates name and description of a custom role.
Args:
url (str): The URL for the endpoint to update the custom role.
role_id (str): role_id from the custom role.
request_payload (dict): The payload containing role information.
It should contain the following keys:
- 'roleName': The name of the custom role.
Expand All @@ -205,7 +205,7 @@ def _update_custom_role(self, url, request_payload):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}

url = f"{self.base_url}/scim/Roles/{role_id}"
response = requests.put(url, json=data, headers=headers)

if response.status_code == 200:
Expand All @@ -218,12 +218,12 @@ def _update_custom_role(self, url, request_payload):
else:
return f"Failed to update custom role. Status code: {response.status_code}"

def _delete_custom_role(self, url):
def delete_custom_role(self, role_id):
"""
Deletes a custom role.
Args:
url (str): The URL for the endpoint to delete the custom role.
role_id (str): role_id from the custom role.
Returns:
str: A message indicating whether the custom role deletion was successful or failed.
Expand All @@ -234,7 +234,7 @@ def _delete_custom_role(self, url):
"Authorization": self.authorization_header,
"Content-Type": "application/json"
}

url = f"{self.base_url}/scim/Roles/{role_id}"
response = requests.delete(url, headers=headers)

if response.status_code == 204:
Expand Down
60 changes: 25 additions & 35 deletions wandb-scim/examples/custom_roles_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
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):
def create_custom_role(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",
create_role_response = custom_role.create(
request_payload={
"permissionJson": permission_json,
"inheritedFrom": inherited_from
Expand All @@ -28,51 +26,48 @@ def create_custom_role(base_url, custom_role, permission_json, inherited_from):
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")

def get_custom_role(base_url, custom_role, role_id):
def get_custom_role(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}")
get_role_response = custom_role.get(role_id)
print(get_role_response)
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")

def get_all_roles(base_url, custom_role):
def get_all_roles(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")
get_all_roles_response = custom_role.get_all()
print(get_all_roles_response)
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")

def add_permission(base_url, custom_role, role_id, permission_json):
def add_permission(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(
url=f"{base_url}/Roles/{role_id}",
update_role_response = custom_role.add_permissions(
role_id,
request_payload={
"permissionJson": permission_json
}
Expand All @@ -81,20 +76,19 @@ def add_permission(base_url, custom_role, role_id, permission_json):
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")

def remove_permission(base_url, custom_role, role_id, permission_json):
def remove_permission(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(
url=f"{base_url}/Roles/{role_id}",
remove_role_response = custom_role.remove_permission(
role_id,
request_payload={
"permissionJson": permission_json
}
Expand All @@ -103,12 +97,11 @@ def remove_permission(base_url, custom_role, role_id, permission_json):
except requests.exceptions.RequestException as e:
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):
def update_custom_role(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.
Expand All @@ -117,8 +110,8 @@ def update_custom_role(base_url, custom_role, role_id, role_name, role_descripti
"""
try:
# Update a custom role
update_custom_role_response = custom_role._update_custom_role(
url=f"{base_url}/Roles/{role_id}",
update_custom_role_response = custom_role.update_custom_role(
role_id,
request_payload={
"roleName": role_name,
"roleDescription": role_description,
Expand All @@ -129,20 +122,17 @@ def update_custom_role(base_url, custom_role, role_id, role_name, role_descripti
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")

def delete_custom_role(base_url, custom_role, role_id):
def delete_custom_role(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(
url=f"{base_url}/Roles/{role_id}"
)
delete_role_response = custom_role.delete_custom_role(role_id)
print(delete_role_response)
except requests.exceptions.RequestException as e:
print(f"Error occurred during API request: {str(e)}")
Expand All @@ -153,15 +143,15 @@ def delete_custom_role(base_url, custom_role, role_id):
base_url = "https://example.com/api" # Base URL of the API

# Instantiate the CustomRole class with your credentials
custom_role = CustomRole(username, api_key)
custom_role = CustomRole(base_url, username, api_key)
role_id = "abc" # Replace with actual role ID
permission_json = {"name": "project:update"} # Replace with required permissions

# Test Functions
get_all_roles(base_url, custom_role)
# create_custom_role(base_url, custom_role, permission_json, "member")
# get_custom_role(base_url, custom_role, role_id)
# add_permission(base_url, custom_role, role_id, permission_json)
# remove_permission(base_url, custom_role, role_id, permission_json)
# update_custom_role(base_url, custom_role, role_id, "test-role", "sample test role description", "member")
# delete_custom_role(base_url,custom_role,role_id)
get_all_roles(custom_role)
# create_custom_role(custom_role, permission_json, "member")
# get_custom_role(custom_role, role_id)
# add_permission(custom_role, role_id, permission_json)
# remove_permission(custom_role, role_id, permission_json)
# update_custom_role(custom_role, role_id, "test-role", "sample test role description", "member")
# delete_custom_role(custom_role, role_id)
Loading

0 comments on commit f5cda6f

Please sign in to comment.