Skip to content

Commit dcd65f4

Browse files
Saahil Dhullajfuss
authored andcommitted
feat(generate-event): Added support for 50+ events (aws#612)
1 parent 546d271 commit dcd65f4

File tree

87 files changed

+2799
-977
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2799
-977
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ include requirements/base.txt
33
include requirements/dev.txt
44
recursive-include samcli/local/init/templates *
55
recursive-include samcli/lib *.json
6+
recursive-include samcli/commands/local/lib/generated_sample_events *.json
67
prune tests
8+

requirements/base.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ docker>=3.3.0
1010
dateparser~=0.7
1111
python-dateutil~=2.6
1212
pathlib2~=2.3.2; python_version<"3.4"
13+
pystache~=0.5
14+

samcli/commands/local/generate_event/api/cli.py

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
"""
2-
Command group for "generate-event" suite for commands.
2+
Sets up the cli for generate-event
33
"""
44

55
import click
66

7-
from .s3.cli import cli as s3_cli
8-
from .api.cli import cli as api_cli
9-
from .dynamodb.cli import cli as dynamodb_cli
10-
from .kinesis.cli import cli as kinesis_cli
11-
from .schedule.cli import cli as schedule_cli
12-
from .sns.cli import cli as sns_cli
13-
7+
from samcli.cli.main import pass_context
8+
from samcli.commands.local.generate_event.event_generation import GenerateEventCommand
149

1510
HELP_TEXT = """
1611
You can use this command to generate sample payloads from different event sources
1712
such as S3, API Gateway, and SNS. These payloads contain the information that the
1813
event sources send to your Lambda functions.\n
1914
\b
2015
Generate the event that S3 sends to your Lambda function when a new object is uploaded
21-
$ sam local generate-event s3 --bucket <bucket> --key <key>\n
16+
$ sam local generate-event s3 [put/delete]\n
17+
\b
18+
You can even customize the event by adding parameter flags. To find which flags apply to your command,
19+
run:\n
20+
$ sam local generate-event s3 [put/delete] --help\n
21+
Then you can add in those flags that you wish to customize using\n
22+
$ sam local generate-event s3 [put/delete] --bucket <bucket> --key <key>\n
2223
\b
2324
After you generate a sample event, you can use it to test your Lambda function locally
24-
$ sam local generate-event s3 --bucket <bucket> --key <key> | sam local invoke <function logical id>
25+
$ sam local generate-event s3 [put/delete] --bucket <bucket> --key <key> | sam local invoke <function logical id>
2526
"""
2627

2728

28-
@click.group("generate-event", help=HELP_TEXT)
29-
def cli():
29+
@click.command(name="generate-event", cls=GenerateEventCommand, help=HELP_TEXT)
30+
@pass_context
31+
def cli(self):
32+
"""
33+
Generate an event for one of the services listed below:
34+
"""
3035
pass # pragma: no cover
31-
32-
33-
# Add individual commands under this group
34-
cli.add_command(s3_cli)
35-
cli.add_command(api_cli)
36-
cli.add_command(dynamodb_cli)
37-
cli.add_command(kinesis_cli)
38-
cli.add_command(schedule_cli)
39-
cli.add_command(sns_cli)

samcli/commands/local/generate_event/dynamodb/cli.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
"""
2+
Generates the services and commands for selection in SAM CLI generate-event
3+
"""
4+
5+
import functools
6+
import click
7+
8+
from samcli.cli.options import debug_option
9+
import samcli.commands.local.lib.generated_sample_events.events as events
10+
11+
12+
class ServiceCommand(click.MultiCommand):
13+
"""
14+
Top level command that defines the service provided
15+
16+
Methods
17+
----------------
18+
get_command(self, ctx, cmd_name):
19+
Get the subcommand(s) under a given service name.
20+
list_commands(self, ctx):
21+
List all of the subcommands
22+
"""
23+
24+
def __init__(self, events_lib, *args, **kwargs):
25+
"""
26+
Constructor for the ServiceCommand class
27+
28+
Parameters
29+
----------
30+
events_lib: samcli.commands.local.lib.generated_sample_events.events
31+
The events library that allows for CLI population and substitution
32+
args: list
33+
any arguments passed in before kwargs
34+
kwargs: dict
35+
dictionary containing the keys/values used to construct the ServiceCommand
36+
"""
37+
38+
super(ServiceCommand, self).__init__(*args, **kwargs)
39+
if not events_lib:
40+
raise ValueError("Events library is necessary to run this command")
41+
42+
self.events_lib = events_lib
43+
self.all_cmds = self.events_lib.event_mapping
44+
45+
def get_command(self, ctx, cmd_name):
46+
"""
47+
gets the subcommands under the service name
48+
49+
Parameters
50+
----------
51+
ctx : Context
52+
the context object passed into the method
53+
cmd_name : str
54+
the service name
55+
Returns
56+
-------
57+
EventTypeSubCommand:
58+
returns subcommand if successful, None if not.
59+
"""
60+
61+
if cmd_name not in self.all_cmds:
62+
return None
63+
return EventTypeSubCommand(self.events_lib, cmd_name, self.all_cmds[cmd_name])
64+
65+
def list_commands(self, ctx):
66+
"""
67+
lists the service commands available
68+
69+
Parameters
70+
----------
71+
ctx: Context
72+
the context object passed into the method
73+
Returns
74+
-------
75+
list
76+
returns sorted list of the service commands available
77+
"""
78+
79+
return sorted(self.all_cmds.keys())
80+
81+
82+
class EventTypeSubCommand(click.MultiCommand):
83+
"""
84+
Class that describes the commands underneath a given service type
85+
86+
Methods
87+
----------------
88+
get_command(self, ctx, cmd_name):
89+
Get the subcommand(s) under a given service name.
90+
list_commands(self, ctx):
91+
List all of the subcommands
92+
"""
93+
94+
TAGS = 'tags'
95+
96+
def __init__(self, events_lib, top_level_cmd_name, subcmd_definition, *args, **kwargs):
97+
"""
98+
constructor for the EventTypeSubCommand class
99+
100+
Parameters
101+
----------
102+
events_lib: samcli.commands.local.lib.generated_sample_events.events
103+
The events library that allows for CLI population and substitution
104+
top_level_cmd_name: string
105+
the service name
106+
subcmd_definition: dict
107+
the subcommands and their values underneath the service command
108+
args: tuple
109+
any arguments passed in before kwargs
110+
kwargs: dict
111+
key/value pairs passed into the constructor
112+
"""
113+
114+
super(EventTypeSubCommand, self).__init__(*args, **kwargs)
115+
self.top_level_cmd_name = top_level_cmd_name
116+
self.subcmd_definition = subcmd_definition
117+
self.events_lib = events_lib
118+
119+
def get_command(self, ctx, cmd_name):
120+
121+
"""
122+
gets the Click Commands underneath a service name
123+
124+
Parameters
125+
----------
126+
ctx: Context
127+
context object passed in
128+
cmd_name: string
129+
the service name
130+
Returns
131+
-------
132+
cmd: Click.Command
133+
the Click Commands that can be called from the CLI
134+
"""
135+
136+
if cmd_name not in self.subcmd_definition:
137+
return None
138+
parameters = []
139+
for param_name in self.subcmd_definition[cmd_name][self.TAGS].keys():
140+
default = self.subcmd_definition[cmd_name][self.TAGS][param_name]["default"]
141+
parameters.append(click.Option(
142+
["--{}".format(param_name)],
143+
default=default,
144+
help="Specify the {} name you'd like, otherwise the default = {}".format(param_name, default)
145+
))
146+
147+
command_callback = functools.partial(self.cmd_implementation,
148+
self.events_lib,
149+
self.top_level_cmd_name,
150+
cmd_name)
151+
cmd = click.Command(name=cmd_name,
152+
help=self.subcmd_definition[cmd_name]["help"],
153+
params=parameters,
154+
callback=command_callback)
155+
156+
cmd = debug_option(cmd)
157+
return cmd
158+
159+
def list_commands(self, ctx):
160+
"""
161+
lists the commands underneath a particular event
162+
163+
Parameters
164+
----------
165+
ctx: Context
166+
the context object passed in
167+
Returns
168+
-------
169+
the sorted list of commands under a service
170+
"""
171+
return sorted(self.subcmd_definition.keys())
172+
173+
def cmd_implementation(self, events_lib, top_level_cmd_name, subcmd_name, *args, **kwargs):
174+
"""
175+
calls for value substitution in the event json and returns the
176+
customized json as a string
177+
178+
Parameters
179+
----------
180+
events_lib
181+
top_level_cmd_name: string
182+
the name of the service
183+
subcmd_name: string
184+
the name of the event under the service
185+
args: tuple
186+
any arguments passed in before kwargs
187+
kwargs: dict
188+
the keys and values for substitution in the json
189+
Returns
190+
-------
191+
event: string
192+
returns the customized event json as a string
193+
"""
194+
event = events_lib.generate_event(top_level_cmd_name, subcmd_name, kwargs)
195+
click.echo(event)
196+
return event
197+
198+
199+
class GenerateEventCommand(ServiceCommand):
200+
"""
201+
Class that brings ServiceCommand and EventTypeSubCommand into one for easy execution
202+
"""
203+
204+
def __init__(self, *args, **kwargs):
205+
"""
206+
Constructor for GenerateEventCommand class that brings together
207+
ServiceCommand and EventTypeSubCommand into one class
208+
209+
Parameters
210+
----------
211+
args: tuple
212+
any arguments passed in before kwargs
213+
kwargs: dict
214+
commands, subcommands, and parameters for generate-event
215+
"""
216+
super(GenerateEventCommand, self).__init__(events.Events(), *args, **kwargs)

samcli/commands/local/generate_event/kinesis/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)