-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
command.py
342 lines (330 loc) · 16.1 KB
/
command.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Invoke Command Class.
"""
import json
from click import Context, style
from samcli.cli.core.command import CoreCommand
from samcli.cli.row_modifiers import RowDefinition, ShowcaseRowModifier
from samcli.commands.remote.invoke.core.formatters import RemoteInvokeCommandHelpTextFormatter
from samcli.commands.remote.invoke.core.options import OPTIONS_INFO
class RemoteInvokeCommand(CoreCommand):
class CustomFormatterContext(Context):
formatter_class = RemoteInvokeCommandHelpTextFormatter
context_class = CustomFormatterContext
@staticmethod
def format_examples(ctx: Context, formatter: RemoteInvokeCommandHelpTextFormatter):
with formatter.indented_section(name="Examples", extra_indents=1):
with formatter.indented_section(name="Lambda Functions", extra_indents=1):
with formatter.indented_section(
name="Invoke default lambda function with empty event", extra_indents=1
):
formatter.write_rd(
[
RowDefinition(
name=style(f"${ctx.command_path} --stack-name hello-world"),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Invoke default lambda function with event passed as text input", extra_indents=1
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name hello-world -e"
f" '{json.dumps({'message':'hello!'})}'"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Invoke named lambda function with an event file", extra_indents=1
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name "
f"hello-world HelloWorldFunction --event-file event.json"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(name="Invoke function with event as stdin input", extra_indents=1):
formatter.write_rd(
[
RowDefinition(
name=style(
f"$ echo '{json.dumps({'message':'hello!'})}' | "
f"{ctx.command_path} HelloWorldFunction --event-file -"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Invoke function using lambda ARN and get the full AWS API response", extra_indents=1
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} arn:aws:lambda:us-west-2:123456789012:function:my-function"
f" -e <> --output json"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Asynchronously invoke function with additional boto parameters", extra_indents=1
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} HelloWorldFunction -e <> "
f"--parameter InvocationType=Event --parameter Qualifier=MyQualifier"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Dry invoke a function to validate parameter values and user/role permissions",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} HelloWorldFunction -e <> --output json "
f"--parameter InvocationType=DryRun"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(name="Step Functions", extra_indents=1):
with formatter.indented_section(
name="Start execution with event passed as text input",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name mock-stack StockTradingStateMachine"
f" -e '{json.dumps({'message':'hello!'})}'"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Start execution using its physical-id or ARN with an execution name parameter",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} arn:aws:states:us-east-1:123456789012:stateMachine:MySFN"
f" -e <> --parameter name=mock-execution-name"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Start execution with an event file and get the full AWS API response",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name mock-stack StockTradingStateMachine"
f" --event-file event.json --output json"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Start execution with event as stdin input and pass the X-ray trace header to the execution",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"$ echo '{json.dumps({'message':'hello!'})}' | "
f"${ctx.command_path} --stack-name mock-stack StockTradingStateMachine"
f" --parameter traceHeader=<>"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(name="SQS Queue", extra_indents=1):
with formatter.indented_section(
name="Send a message with the MessageBody passed as event",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(f"${ctx.command_path} --stack-name mock-stack MySQSQueue -e hello-world"),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Send a message using its physical-id and pass event using --event-file",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} https://sqs.us-east-1.amazonaws.com/12345678910/QueueName"
f" --event-file event.json"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Send a message using its ARN and delay the specified message",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} arn:aws:sqs:region:account_id:queue_name -e hello-world"
f" --parameter DelaySeconds=10"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Send a message along with message attributes and get the full AWS API response",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name mock-stack MySQSQueue -e hello-world"
f" --output json --parameter MessageAttributes="
f"'{json.dumps({'City': {'DataType': 'String', 'StringValue': 'City'}})}'"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Send a message to a FIFO SQS Queue",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name mock-stack MySQSQueue -e hello-world"
f" --parameter MessageGroupId=mock-message-group"
f" --parameter MessageDeduplicationId=mock-dedup-id"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(name="Kinesis Data Stream", extra_indents=1):
with formatter.indented_section(
name="Put a record using the data provided as event",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} --stack-name mock-stack MyKinesisStream -e"
f" '{json.dumps({'message':'hello!'})}'"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Put a record using its physical-id and pass event using --event-file",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(f"${ctx.command_path} MyKinesisStreamName" f" --event-file event.json"),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Put a record using its ARN and override the key hash",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path}"
f" arn:aws:kinesis:us-east-2:123456789012:stream/mystream"
f" --event-file event.json --parameter ExplicitHashKey=<>"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
with formatter.indented_section(
name="Put a record with a sequence number for ordering with a PartitionKey",
extra_indents=1,
):
formatter.write_rd(
[
RowDefinition(
name=style(
f"${ctx.command_path} MyKinesisStreamName"
f" --event hello-world --parameter SequenceNumberForOrdering=<>"
f" --parameter PartitionKey=<>"
),
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
@staticmethod
def format_acronyms(formatter: RemoteInvokeCommandHelpTextFormatter):
with formatter.indented_section(name="Acronyms", extra_indents=1):
formatter.write_rd(
[
RowDefinition(
name="ARN",
text="Amazon Resource Name",
extra_row_modifiers=[ShowcaseRowModifier()],
),
]
)
def format_options(self, ctx: Context, formatter: RemoteInvokeCommandHelpTextFormatter) -> None: # type:ignore
# NOTE: `ignore` is put in place here for mypy even though it is the correct behavior,
# as the `formatter_class` can be set in subclass of Command. If ignore is not set,
# mypy raises argument needs to be HelpFormatter as super class defines it.
self.format_description(formatter)
RemoteInvokeCommand.format_examples(ctx, formatter)
RemoteInvokeCommand.format_acronyms(formatter)
CoreCommand._format_options(
ctx=ctx, params=self.get_params(ctx), formatter=formatter, formatting_options=OPTIONS_INFO
)