Skip to content

Commit 14e5329

Browse files
committed
adding tests; currently broken
1 parent 3fa61fa commit 14e5329

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

stacker_blueprints/iam_roles.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,22 @@ def create_template(self):
228228

229229
self.create_policy()
230230

231-
class IAMRole(RoleBaseBlueprint):
231+
232+
class IAMRole(Blueprint):
232233
"""
233234
Blueprint to create an IAM role.
234235
235236
- class_path: stacker_blueprints.iam_roles.IAMRole
236237
name: my-role
237238
variables:
238-
AttachedPolicies:
239-
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
240239
Name: myRole
241240
Path: /
241+
AttachedPolicies:
242+
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
242243
AssumeRole:
243-
- arn:aws:user/alphonse
244+
- arn:aws:iam::123456789012:user/JohnDoe
244245
"""
245246
VARIABLES = {
246-
"AttachedPolicies": {
247-
"type": list,
248-
"description": "List of ARNs of policies to attach",
249-
"default": [],
250-
},
251247
"Name": {
252248
"type": str,
253249
"description": "The name of the role",
@@ -258,37 +254,43 @@ class IAMRole(RoleBaseBlueprint):
258254
"description": "Provide the path",
259255
"default": "/",
260256
},
257+
"AttachedPolicies": {
258+
"type": list,
259+
"description": "List of ARNs of policies to attach",
260+
"default": [],
261+
},
261262
"AssumeRole": {
262263
"type": list,
263264
"description": "List of ARNs of entities allowed to assume this role",
264265
"default": [],
265266
},
266267
}
267268

268-
def create_role(self, name, assumerole_policy):
269+
def create_template(self):
269270
variables = self.get_variables()
270271

271-
role = t.add_resource(
272+
ar_policy = iam.Policy(
273+
Statement=[
274+
iam.Statement(
275+
Effect='Allow',
276+
Principal=p,
277+
) for p in variables['AssumeRole']
278+
]
279+
)
280+
281+
role = self.template.add_resource(
272282
iam.Role(
273-
name,
283+
variables['Name'],
274284
Path=variables['Path'],
275-
AssumeRolePolicyDocument=assumerole_policy,
276285
ManagedPolicyArns=variables['AttachedPolicies'],
286+
AssumeRolePolicyDocument=ar_policy,
277287
)
278288
)
279289

280-
t.add_output(
290+
self.template.add_output(
281291
Output(name + "RoleName", Value=Ref(role))
282292
)
283293

284-
t.add_output(
294+
self.template.add_output(
285295
Output(name + "RoleArn", Value=GetAtt(role.title, "Arn"))
286296
)
287-
288-
self.roles.append(role)
289-
return role
290-
291-
def create_template(self):
292-
variables = self.get_variables()
293-
self.create_ec2_role(variables["Name"])
294-
self.create_policy(variables["Name"])

tests/test_iam_roles.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,31 @@ def test_role_name(self):
8888
blueprint.resolve_variables(self.generate_variables())
8989
blueprint.create_template()
9090
self.assertRenderedBlueprint(blueprint)
91+
92+
93+
class TestIamRoleBlueprint(TestIamRolesCommon):
94+
95+
def test_role(self):
96+
self.common_variables = {
97+
'AttachedPolicies': [
98+
'arn:aws:iam::aws:policy/CloudWatchLogsFullAccess'
99+
],
100+
'Path': '/',
101+
}
102+
blueprint = self.create_blueprint('test_iam_roles_ec2_role', class_name=iam_roles.IAMRole)
103+
blueprint.resolve_variables(self.generate_variables())
104+
blueprint.create_template()
105+
self.assertRenderedBlueprint(blueprint)
106+
107+
def test_role_name(self):
108+
self.common_variables = {
109+
'AttachedPolicies': [
110+
'arn:aws:iam::aws:policy/CloudWatchLogsFullAccess'
111+
],
112+
'Name': 'myRole',
113+
'Path': '/',
114+
}
115+
blueprint = self.create_blueprint('test_iam_roles_ec2_role_name', class_name=iam_roles.IAMRole)
116+
blueprint.resolve_variables(self.generate_variables())
117+
blueprint.create_template()
118+
self.assertRenderedBlueprint(blueprint)

0 commit comments

Comments
 (0)