-
Notifications
You must be signed in to change notification settings - Fork 2
/
iamHandler.py
63 lines (49 loc) · 2.05 KB
/
iamHandler.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
import boto3
from botocore.exceptions import ClientError
import json
class IAM():
session = None
client = None
def __init__(self, profile):
self.session = boto3.Session(profile_name = profile)
self.client = self.session.client('iam')
super().__init__()
def describeRole(self, roleName):
try:
describe_role_res = self.client.get_role(
RoleName=roleName
)
return describe_role_res
except ClientError as error:
if error.response['Error']['Code'] == 'NoSuchEntity':
return 'NoSuchEntity'
else:
return 'Unexpected error occurred... Role could not be created', error
def createRole(self, payloadJsonFileName,roleName, description):
try:
#open the
with open('assets/' + payloadJsonFileName) as f:
role = json.load(f)
create_role_res = self.client.create_role(
RoleName=roleName,
AssumeRolePolicyDocument=json.dumps(role),
Description=description
)
return create_role_res
except ClientError as error:
if error.response['Error']['Code'] == 'EntityAlreadyExists':
return 'Role already exists... hence exiting from here'
else:
return 'Unexpected error occurred... Role could not be created', error
def attachPolicy(self, roleName, policyArn):
try:
attachpolicy_res = self.client.attach_role_policy(
RoleName=roleName,
PolicyArn=policyArn
)
return attachpolicy_res
except ClientError as error:
if error.response['Error']['Code'] == 'EntityAlreadyExists':
return 'Role already exists... hence exiting from here'
else:
return 'Unexpected error occurred... Role could not be created', error