Created
May 29, 2022 21:13
-
-
Save si3mshady/ecbb78220cf34e1ce5e493ee15fc74c1 to your computer and use it in GitHub Desktop.
#Weekend cohort script #2 add users to group by tag
This file contains hidden or 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
import boto3 | |
#create group - use IAC tool for this | |
#get all users | |
#check user tags | |
#switch to new group | |
REGION = 'us-east-1' | |
iam = boto3.client('iam', region_name=REGION) | |
EVALUATE_KEY = 'testing' | |
EVALUATE_VALUE = 'false' | |
GROUP_NAME = 'mfa' | |
def get_all_users(): | |
return [user.get('UserName') for user in iam.list_users().get('Users')] | |
def add_to_groups(user): | |
return iam.add_user_to_group( | |
GroupName=GROUP_NAME, | |
UserName=user | |
) | |
def check_user_tags(user): | |
user_ = iam.get_user(UserName=user) | |
tags = user_.get('User').get('Tags') | |
for tag in tags: | |
if tag.get('Key').lower() == EVALUATE_KEY.lower() and tag.get('Value').lower() != EVALUATE_VALUE.lower(): | |
return user | |
def init (): | |
# will return ['cohort_user_1', 'cohort_user_2', None, None, None] | |
user_list = [user for user in [check_user_tags(u) for u in get_all_users()] if user != None] | |
results = [result.get('ResponseMetadata').get('HTTPStatusCode') for result in [add_to_groups(user) for user in user_list]] | |
print(results) | |
if __name__ == "__main__": | |
init() | |
#Elliott Arnold 5-28-22 | |
#Weekend cohort script #2 add users to group |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment