Skip to content

Commit

Permalink
feat: refactor list groups with members to use list_users (#710)
Browse files Browse the repository at this point in the history
* feat: refactor list groups with members to use list_users

* feat: improve error handling for list_groups_with_members

* chore: fmt

* fix: update execute API call docstring
  • Loading branch information
gcharest authored Jan 13, 2025
1 parent cfa30c4 commit b9769ae
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 63 deletions.
40 changes: 18 additions & 22 deletions app/integrations/google_next/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def list_users(
service: Resource,
customer: str | None = None,
**kwargs,
):
) -> list[dict]:
"""List all users in the Google Workspace domain.
Args:
Expand Down Expand Up @@ -90,7 +90,7 @@ def list_groups(
service: Resource,
customer: str | None = None,
**kwargs,
):
) -> list[dict]:
"""List all groups in the Google Workspace domain. A query can be provided to filter the results (e.g. query="email:prefix-*" will filter for all groups where the email starts with 'prefix-').
Args:
Expand Down Expand Up @@ -156,7 +156,7 @@ def list_groups_with_members(
list: A list of group objects with members and their details.
"""

groups = list_groups(
groups: list[dict] = list_groups(
service,
query=query,
fields="groups(email, name, directMembersCount, description)",
Expand All @@ -165,6 +165,8 @@ def list_groups_with_members(
if len(groups) == 0:
return []

users: list[dict] = list_users(service)

if groups_filters is not None:
for groups_filter in groups_filters:
groups = filters.filter_by_condition(groups, groups_filter)
Expand All @@ -187,14 +189,18 @@ def list_groups_with_members(
group["error"] = f"Error getting members: {e}"
logger.warning(f"Error getting members for group {group['email']}: {e}")
continue
members = get_members_details(service, members)
members = get_members_details(members, users)
if members:
group.update({"members": members})
if any(member.get("error") for member in members) and not group.get(
"error"
):
group["error"] = "Error getting members details."
groups_with_members.append(group)
return groups_with_members


def get_members_details(service: Resource, members: list[dict]):
def get_members_details(members: list[dict], users: list[dict]):
"""Get user details for a list of members.
Args:
Expand All @@ -206,24 +212,14 @@ def get_members_details(service: Resource, members: list[dict]):
"""

for member in members:
user_details = {}
try:
logger.info(f"Getting user details for member: {member['email']}")
user_details = retry_request(
get_user,
service,
member["email"],
max_attempts=3,
delay=1,
fields="name, primaryEmail",
)
except Exception as e:
member["error"] = f"{e}"
logger.warning(
f"Error getting user details for member {member['email']}: {e}"
)
continue
logger.info(f"Getting user details for member: {member['email']}")
user_details = next(
(user for user in users if user["primaryEmail"] == member["email"]), None
)
if user_details:
member.update(user_details)
else:
member["error"] = "User details not found"
logger.warning(f"User details not found for member: {member['email']}")

return members
5 changes: 1 addition & 4 deletions app/integrations/google_next/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,9 @@ def execute_google_api_call(
"""Execute a Google API call on a resource.
Args:
service_name (str): The name of the Google service.
version (str): The version of the Google service.
service (Resource): The Google service resource.
resource_path (str): The path to the resource, which can include nested resources separated by dots.
method (str): The method to call on the resource.
scopes (list, optional): The scopes for the Google service.
delegated_user_email (str, optional): The email address of the user to impersonate.
paginate (bool, optional): Whether to paginate the API call.
**kwargs: Additional keyword arguments for the API call.
Expand Down
Loading

0 comments on commit b9769ae

Please sign in to comment.