generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feaet: Refactor with AWS execute aws api call and handle api errors f…
…unctions
- Loading branch information
Showing
2 changed files
with
125 additions
and
64 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
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,64 +1,52 @@ | ||
import os | ||
from integrations.aws.client import paginate, assume_role_client | ||
from integrations.aws.client import execute_aws_api_call, handle_aws_api_errors | ||
|
||
INSTANCE_ID = os.environ.get("AWS_SSO_INSTANCE_ID", "") | ||
INSTANCE_ARN = os.environ.get("AWS_SSO_INSTANCE_ARN", "") | ||
ROLE_ARN = os.environ.get("AWS_SSO_ROLE_ARN", "") | ||
|
||
|
||
def list_users(identity_store_id=None, attribute_path=None, attribute_value=None): | ||
@handle_aws_api_errors | ||
def list_users(**kwargs): | ||
"""Retrieves all users from the AWS Identity Center (identitystore)""" | ||
client = assume_role_client("identitystore", ROLE_ARN) | ||
if not identity_store_id: | ||
identity_store_id = INSTANCE_ID | ||
kwargs = {"IdentityStoreId": identity_store_id} | ||
|
||
if attribute_path and attribute_value: | ||
kwargs["Filters"] = [ | ||
{"AttributePath": attribute_path, "AttributeValue": attribute_value}, | ||
] | ||
|
||
return paginate(client, "list_users", ["Users"], **kwargs) | ||
if "IdentityStoreId" not in kwargs: | ||
kwargs["IdentityStoreId"] = INSTANCE_ID | ||
return execute_aws_api_call( | ||
"identitystore", "list_users", paginated=True, keys=["Users"], **kwargs | ||
) | ||
|
||
|
||
def list_groups(identity_store_id=None, attribute_path=None, attribute_value=None): | ||
@handle_aws_api_errors | ||
def list_groups(**kwargs): | ||
"""Retrieves all groups from the AWS Identity Center (identitystore)""" | ||
client = assume_role_client("identitystore", ROLE_ARN) | ||
if not identity_store_id: | ||
identity_store_id = INSTANCE_ID | ||
kwargs = {"IdentityStoreId": identity_store_id} | ||
|
||
if attribute_path and attribute_value: | ||
kwargs["Filters"] = [ | ||
{"AttributePath": attribute_path, "AttributeValue": attribute_value}, | ||
] | ||
|
||
return paginate(client, "list_groups", ["Groups"], **kwargs) | ||
if "IdentityStoreId" not in kwargs: | ||
kwargs["IdentityStoreId"] = INSTANCE_ID | ||
return execute_aws_api_call( | ||
"identitystore", "list_groups", paginated=True, keys=["Groups"], **kwargs | ||
) | ||
|
||
|
||
def list_group_memberships(identity_store_id, group_id): | ||
@handle_aws_api_errors | ||
def list_group_memberships(group_id, **kwargs): | ||
"""Retrieves all group memberships from the AWS Identity Center (identitystore)""" | ||
client = assume_role_client("identitystore", ROLE_ARN) | ||
|
||
if not identity_store_id: | ||
identity_store_id = INSTANCE_ID | ||
return paginate( | ||
client, | ||
if "IdentityStoreId" not in kwargs: | ||
kwargs["IdentityStoreId"] = INSTANCE_ID | ||
return execute_aws_api_call( | ||
"identitystore", | ||
"list_group_memberships", | ||
["GroupMemberships"], | ||
IdentityStoreId=identity_store_id, | ||
GroupId=group_id, | ||
**kwargs, | ||
) | ||
|
||
|
||
def list_groups_with_membership(identity_store_id): | ||
@handle_aws_api_errors | ||
def list_groups_with_membership(): | ||
"""Retrieves all groups with their members from the AWS Identity Center (identitystore)""" | ||
if not identity_store_id: | ||
identity_store_id = INSTANCE_ID | ||
groups = list_groups(identity_store_id) | ||
groups = list_groups() | ||
for group in groups: | ||
group["GroupMemberships"] = list_group_memberships( | ||
identity_store_id, group["GroupId"] | ||
group["GroupId"] | ||
) | ||
|
||
return groups |