-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4b274d
commit b912d99
Showing
1 changed file
with
66 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from typing import Annotated | ||
from httpx import Client | ||
# from collections import defaultdict | ||
# from dateutil import parser | ||
# from datetime import datetime, timezone, timedelta | ||
from collections import defaultdict | ||
from dateutil import parser | ||
from datetime import datetime, timezone, timedelta | ||
|
||
from admyral.action import action, ArgumentMetadata | ||
|
||
# from admyral.context import ctx | ||
from admyral.context import ctx | ||
from admyral.typings import JsonValue | ||
|
||
|
||
|
@@ -40,47 +40,30 @@ def list_retool_inactive_users( | |
# Returns a list of users. The API token must have the "Users > Read" scope. | ||
# https://docs.retool.com/reference/api/#tag/Users/paths/~1users~1%7BuserId%7D/patch | ||
|
||
# secret = ctx.get().secrets.get("RETOOL_SECRET") | ||
# api_key = secret["api_key"] | ||
# domain = secret["domain"] | ||
|
||
# with get_retool_client(domain, api_key) as client: | ||
# # TODO: handle pageination | ||
# response = client.get("/users") | ||
# response.raise_for_status() | ||
# response_json = response.json() | ||
|
||
# if not response_json["success"]: | ||
# raise RuntimeError( | ||
# f"Failed to list Retool users: {response_json['message']}" | ||
# ) | ||
|
||
# utc_time_now = datetime.now(timezone.utc) - timedelta( | ||
# days=inactivity_threshold_in_days | ||
# ) | ||
|
||
# return [ | ||
# user | ||
# for user in response_json["data"] | ||
# if parser.isoparse(user["last_active"]) <= utc_time_now | ||
# ] | ||
|
||
# Mock data | ||
return [ | ||
{ | ||
"id": "string", | ||
"legacy_id": "string", | ||
"email": "[email protected]", | ||
"active": False, | ||
"created_at": "2019-02-08T11:45:48.899Z", | ||
"last_active": "2019-02-08T11:45:48.899Z", | ||
"first_name": "Franky", | ||
"last_name": "string", | ||
"metadata": {}, | ||
"is_admin": True, | ||
"user_type": "default", | ||
} | ||
] | ||
secret = ctx.get().secrets.get("RETOOL_SECRET") | ||
api_key = secret["api_key"] | ||
domain = secret["domain"] | ||
|
||
with get_retool_client(domain, api_key) as client: | ||
# TODO: handle pageination | ||
response = client.get("/users") | ||
response.raise_for_status() | ||
response_json = response.json() | ||
|
||
if not response_json["success"]: | ||
raise RuntimeError( | ||
f"Failed to list Retool users: {response_json['message']}" | ||
) | ||
|
||
utc_time_now = datetime.now(timezone.utc) - timedelta( | ||
days=inactivity_threshold_in_days | ||
) | ||
|
||
return [ | ||
user | ||
for user in response_json["data"] | ||
if parser.isoparse(user["last_active"]) <= utc_time_now | ||
] | ||
|
||
|
||
@action( | ||
|
@@ -93,53 +76,41 @@ def list_groups_per_user() -> dict[str, JsonValue]: | |
# Get all permission groups for an organization or space. The API token must have the "Groups > Read" scope. | ||
# https://docs.retool.com/reference/api/v2/#tag/Groups/paths/~1groups~1%7BgroupId%7D/put | ||
|
||
# secret = ctx.get().secrets.get("RETOOL_SECRET") | ||
# api_key = secret["api_key"] | ||
# domain = secret["domain"] | ||
|
||
# with get_retool_client(domain, api_key) as client: | ||
# # Fetch all users to get the last active date | ||
# response = client.get("/users") | ||
# response.raise_for_status() | ||
# response_json = response.json() | ||
|
||
# if not response_json["success"]: | ||
# raise RuntimeError( | ||
# f"Failed to list Retool users: {response_json['message']}" | ||
# ) | ||
|
||
# groups_per_user = { | ||
# user["email"]: { | ||
# "groups": defaultdict(list), | ||
# "last_active": user["last_active"] | ||
# } | ||
# for user in response_json["data"] | ||
# } | ||
|
||
# # Fetch all groups | ||
# response = client.get("/groups") | ||
# response.raise_for_status() | ||
# response_json = response.json() | ||
|
||
# if not response_json["success"]: | ||
# raise RuntimeError( | ||
# f"Failed to list Retool groups: {response_json['message']}" | ||
# ) | ||
|
||
# for group in response_json["data"]: | ||
# for member in group["member"]: | ||
# groups_per_user[member]["groups"].append(group["name"]) | ||
|
||
# return groups_per_user | ||
|
||
# Mock data | ||
return { | ||
"[email protected]": { | ||
"groups": ["group1", "group2"], | ||
"last_active": "2019-02-08T11:45:48.899Z", | ||
}, | ||
"[email protected]": { | ||
"groups": ["group1"], | ||
"last_active": "2019-02-08T11:45:48.899Z", | ||
}, | ||
} | ||
secret = ctx.get().secrets.get("RETOOL_SECRET") | ||
api_key = secret["api_key"] | ||
domain = secret["domain"] | ||
|
||
with get_retool_client(domain, api_key) as client: | ||
# Fetch all users to get the last active date | ||
response = client.get("/users") | ||
response.raise_for_status() | ||
response_json = response.json() | ||
|
||
if not response_json["success"]: | ||
raise RuntimeError( | ||
f"Failed to list Retool users: {response_json['message']}" | ||
) | ||
|
||
groups_per_user = { | ||
user["email"]: { | ||
"groups": defaultdict(list), | ||
"last_active": user["last_active"], | ||
} | ||
for user in response_json["data"] | ||
} | ||
|
||
# Fetch all groups | ||
response = client.get("/groups") | ||
response.raise_for_status() | ||
response_json = response.json() | ||
|
||
if not response_json["success"]: | ||
raise RuntimeError( | ||
f"Failed to list Retool groups: {response_json['message']}" | ||
) | ||
|
||
for group in response_json["data"]: | ||
for member in group["member"]: | ||
groups_per_user[member]["groups"].append(group["name"]) | ||
|
||
return groups_per_user |