-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam_user.py
73 lines (59 loc) · 1.99 KB
/
iam_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import boto3
class IAMUser:
def __init__(self):
"""
Initializes an instance of IAMUser class.
"""
self.iam = boto3.resource("iam")
def create_user(self, username: str):
"""
Creates a new IAM user.
Args:
username (str): The username of the IAM user.
"""
user = self.iam.create_user(UserName=username)
print(f"IAM user created: {username}, with ID {user.id}")
def delete_user(self, username: str):
"""
Deletes an IAM user.
Args:
username (str): The username of the IAM user.
"""
user = self.iam.User(username)
user.delete()
print(f"IAM user deleted: {username}")
def list_users(self):
"""
Lists all IAM users.
"""
for user in self.iam.users.all():
print(user.name)
def add_user_to_group(self, group_name: str, username: str):
"""
Adds an IAM user to a group.
Args:
group_name (str): The name of the group.
username (str): The username of the IAM user.
"""
group = self.iam.Group(group_name)
group.add_user(UserName=username)
print(f"IAM user added to group: {username} -> {group_name}")
def remove_user_from_group(self, group_name: str, username: str):
"""
Removes an IAM user from a group.
Args:
group_name (str): The name of the group.
username (str): The username of the IAM user.
"""
group = self.iam.Group(group_name)
group.remove_user(UserName=username)
print(f"IAM user removed from group: {username} -> {group_name}")
def list_user_groups(self, username: str):
"""
Lists the groups that an IAM user belongs to.
Args:
username (str): The username of the IAM user.
"""
groups = self.iam.groups.filter(UserName=username)
for group in groups:
print(group.name)