1
1
import json
2
2
from abc import ABCMeta
3
- from typing import Any , Dict , Optional , cast
3
+ from typing import Any , Dict , List , Optional , Union , cast
4
4
5
5
from samtranslator .metrics .method_decorator import cw_timer
6
6
from samtranslator .model import Property , PropertyType , Resource , ResourceMacro
10
10
from samtranslator .model .exceptions import InvalidEventException
11
11
from samtranslator .model .iam import IAMRole , IAMRolePolicies
12
12
from samtranslator .model .intrinsics import fnSub
13
+ from samtranslator .model .stepfunctions .resources import StepFunctionsStateMachine
13
14
from samtranslator .model .types import IS_BOOL , IS_DICT , IS_STR , PassThrough
14
15
from samtranslator .swagger .swagger import SwaggerEditor
15
16
from samtranslator .translator import logical_id_generator
@@ -50,7 +51,13 @@ def _generate_logical_id(self, prefix, suffix, resource_type): # type: ignore[n
50
51
generator = logical_id_generator .LogicalIdGenerator (prefix + resource_type , suffix )
51
52
return generator .gen ()
52
53
53
- def _construct_role (self , resource , permissions_boundary = None , prefix = None , suffix = "" ): # type: ignore[no-untyped-def]
54
+ def _construct_role (
55
+ self ,
56
+ resource : StepFunctionsStateMachine ,
57
+ permissions_boundary : Optional [str ],
58
+ prefix : Optional [str ],
59
+ suffix : str = "" ,
60
+ ) -> IAMRole :
54
61
"""Constructs the IAM Role resource allowing the event service to invoke
55
62
the StartExecution API of the state machine resource it is associated with.
56
63
@@ -93,6 +100,7 @@ class Schedule(EventSource):
93
100
"DeadLetterConfig" : PropertyType (False , IS_DICT ),
94
101
"RetryPolicy" : PropertyType (False , IS_DICT ),
95
102
"Target" : Property (False , IS_DICT ),
103
+ "RoleArn" : Property (False , IS_STR ),
96
104
}
97
105
98
106
Schedule : PassThrough
@@ -104,6 +112,7 @@ class Schedule(EventSource):
104
112
DeadLetterConfig : Optional [Dict [str , Any ]]
105
113
RetryPolicy : Optional [PassThrough ]
106
114
Target : Optional [PassThrough ]
115
+ RoleArn : Optional [PassThrough ]
107
116
108
117
@cw_timer (prefix = SFN_EVETSOURCE_METRIC_PREFIX )
109
118
def to_cloudformation (self , resource , ** kwargs ): # type: ignore[no-untyped-def]
@@ -113,7 +122,7 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
113
122
:returns: a list of vanilla CloudFormation Resources, to which this Schedule event expands
114
123
:rtype: list
115
124
"""
116
- resources = []
125
+ resources : List [ Any ] = []
117
126
118
127
permissions_boundary = kwargs .get ("permissions_boundary" )
119
128
@@ -135,8 +144,12 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
135
144
events_rule .Name = self .Name
136
145
events_rule .Description = self .Description
137
146
138
- role = self ._construct_role (resource , permissions_boundary ) # type: ignore[no-untyped-call]
139
- resources .append (role )
147
+ role : Union [IAMRole , str , Dict [str , Any ]]
148
+ if self .RoleArn is None :
149
+ role = self ._construct_role (resource , permissions_boundary , prefix = None )
150
+ resources .append (role )
151
+ else :
152
+ role = self .RoleArn
140
153
141
154
source_arn = events_rule .get_runtime_attr ("arn" )
142
155
dlq_queue_arn = None
@@ -146,26 +159,44 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
146
159
self , source_arn , passthrough_resource_attributes
147
160
)
148
161
resources .extend (dlq_resources )
149
- events_rule .Targets = [self ._construct_target (resource , role , dlq_queue_arn )] # type: ignore[no-untyped-call]
162
+ events_rule .Targets = [self ._construct_target (resource , role , dlq_queue_arn )]
150
163
151
164
return resources
152
165
153
- def _construct_target (self , resource , role , dead_letter_queue_arn = None ): # type: ignore[no-untyped-def]
154
- """Constructs the Target property for the EventBridge Rule.
155
-
156
- :returns: the Target property
157
- :rtype: dict
166
+ def _construct_target (
167
+ self ,
168
+ resource : StepFunctionsStateMachine ,
169
+ role : Union [IAMRole , str , Dict [str , Any ]],
170
+ dead_letter_queue_arn : Optional [str ],
171
+ ) -> Dict [str , Any ]:
172
+ """_summary_
173
+
174
+ Parameters
175
+ ----------
176
+ resource
177
+ StepFunctionsState machine resource to be generated
178
+ role
179
+ The role to be used by the Schedule event resource either generated or user provides arn
180
+ dead_letter_queue_arn
181
+ Dead letter queue associated with the resource
182
+
183
+ Returns
184
+ -------
185
+ The Target property
158
186
"""
159
187
target_id = (
160
188
self .Target ["Id" ]
161
189
if self .Target and "Id" in self .Target
162
190
else generate_valid_target_id (self .logical_id , EVENT_RULE_SFN_TARGET_SUFFIX )
163
191
)
192
+
164
193
target = {
165
194
"Arn" : resource .get_runtime_attr ("arn" ),
166
195
"Id" : target_id ,
167
- "RoleArn" : role .get_runtime_attr ("arn" ),
168
196
}
197
+
198
+ target ["RoleArn" ] = role .get_runtime_attr ("arn" ) if isinstance (role , IAMRole ) else role
199
+
169
200
if self .Input is not None :
170
201
target ["Input" ] = self .Input
171
202
@@ -216,7 +247,7 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
216
247
:returns: a list of vanilla CloudFormation Resources, to which this CloudWatch Events/EventBridge event expands
217
248
:rtype: list
218
249
"""
219
- resources = []
250
+ resources : List [ Any ] = []
220
251
221
252
permissions_boundary = kwargs .get ("permissions_boundary" )
222
253
@@ -231,7 +262,11 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
231
262
232
263
resources .append (events_rule )
233
264
234
- role = self ._construct_role (resource , permissions_boundary ) # type: ignore[no-untyped-call]
265
+ role = self ._construct_role (
266
+ resource ,
267
+ permissions_boundary ,
268
+ prefix = None ,
269
+ )
235
270
resources .append (role )
236
271
237
272
source_arn = events_rule .get_runtime_attr ("arn" )
@@ -331,7 +366,7 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
331
366
:returns: a list of vanilla CloudFormation Resources, to which this Api event expands
332
367
:rtype: list
333
368
"""
334
- resources = []
369
+ resources : List [ Any ] = []
335
370
336
371
intrinsics_resolver = kwargs .get ("intrinsics_resolver" )
337
372
permissions_boundary = kwargs .get ("permissions_boundary" )
@@ -340,7 +375,7 @@ def to_cloudformation(self, resource, **kwargs): # type: ignore[no-untyped-def]
340
375
# Convert to lower case so that user can specify either GET or get
341
376
self .Method = self .Method .lower ()
342
377
343
- role = self ._construct_role (resource , permissions_boundary ) # type: ignore[no-untyped-call]
378
+ role = self ._construct_role (resource , permissions_boundary , prefix = None )
344
379
resources .append (role )
345
380
346
381
explicit_api = kwargs ["explicit_api" ]
0 commit comments