Skip to content

Commit

Permalink
feat(generate-event): Added support for 50+ events (aws#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saahil Dhulla authored and jfuss committed Aug 10, 2018
1 parent 546d271 commit dcd65f4
Show file tree
Hide file tree
Showing 87 changed files with 2,799 additions and 977 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ include requirements/base.txt
include requirements/dev.txt
recursive-include samcli/local/init/templates *
recursive-include samcli/lib *.json
recursive-include samcli/commands/local/lib/generated_sample_events *.json
prune tests

2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ docker>=3.3.0
dateparser~=0.7
python-dateutil~=2.6
pathlib2~=2.3.2; python_version<"3.4"
pystache~=0.5

43 changes: 0 additions & 43 deletions samcli/commands/local/generate_event/api/cli.py

This file was deleted.

38 changes: 17 additions & 21 deletions samcli/commands/local/generate_event/cli.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
"""
Command group for "generate-event" suite for commands.
Sets up the cli for generate-event
"""

import click

from .s3.cli import cli as s3_cli
from .api.cli import cli as api_cli
from .dynamodb.cli import cli as dynamodb_cli
from .kinesis.cli import cli as kinesis_cli
from .schedule.cli import cli as schedule_cli
from .sns.cli import cli as sns_cli

from samcli.cli.main import pass_context
from samcli.commands.local.generate_event.event_generation import GenerateEventCommand

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


@click.group("generate-event", help=HELP_TEXT)
def cli():
@click.command(name="generate-event", cls=GenerateEventCommand, help=HELP_TEXT)
@pass_context
def cli(self):
"""
Generate an event for one of the services listed below:
"""
pass # pragma: no cover


# Add individual commands under this group
cli.add_command(s3_cli)
cli.add_command(api_cli)
cli.add_command(dynamodb_cli)
cli.add_command(kinesis_cli)
cli.add_command(schedule_cli)
cli.add_command(sns_cli)
31 changes: 0 additions & 31 deletions samcli/commands/local/generate_event/dynamodb/cli.py

This file was deleted.

216 changes: 216 additions & 0 deletions samcli/commands/local/generate_event/event_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
"""
Generates the services and commands for selection in SAM CLI generate-event
"""

import functools
import click

from samcli.cli.options import debug_option
import samcli.commands.local.lib.generated_sample_events.events as events


class ServiceCommand(click.MultiCommand):
"""
Top level command that defines the service provided
Methods
----------------
get_command(self, ctx, cmd_name):
Get the subcommand(s) under a given service name.
list_commands(self, ctx):
List all of the subcommands
"""

def __init__(self, events_lib, *args, **kwargs):
"""
Constructor for the ServiceCommand class
Parameters
----------
events_lib: samcli.commands.local.lib.generated_sample_events.events
The events library that allows for CLI population and substitution
args: list
any arguments passed in before kwargs
kwargs: dict
dictionary containing the keys/values used to construct the ServiceCommand
"""

super(ServiceCommand, self).__init__(*args, **kwargs)
if not events_lib:
raise ValueError("Events library is necessary to run this command")

self.events_lib = events_lib
self.all_cmds = self.events_lib.event_mapping

def get_command(self, ctx, cmd_name):
"""
gets the subcommands under the service name
Parameters
----------
ctx : Context
the context object passed into the method
cmd_name : str
the service name
Returns
-------
EventTypeSubCommand:
returns subcommand if successful, None if not.
"""

if cmd_name not in self.all_cmds:
return None
return EventTypeSubCommand(self.events_lib, cmd_name, self.all_cmds[cmd_name])

def list_commands(self, ctx):
"""
lists the service commands available
Parameters
----------
ctx: Context
the context object passed into the method
Returns
-------
list
returns sorted list of the service commands available
"""

return sorted(self.all_cmds.keys())


class EventTypeSubCommand(click.MultiCommand):
"""
Class that describes the commands underneath a given service type
Methods
----------------
get_command(self, ctx, cmd_name):
Get the subcommand(s) under a given service name.
list_commands(self, ctx):
List all of the subcommands
"""

TAGS = 'tags'

def __init__(self, events_lib, top_level_cmd_name, subcmd_definition, *args, **kwargs):
"""
constructor for the EventTypeSubCommand class
Parameters
----------
events_lib: samcli.commands.local.lib.generated_sample_events.events
The events library that allows for CLI population and substitution
top_level_cmd_name: string
the service name
subcmd_definition: dict
the subcommands and their values underneath the service command
args: tuple
any arguments passed in before kwargs
kwargs: dict
key/value pairs passed into the constructor
"""

super(EventTypeSubCommand, self).__init__(*args, **kwargs)
self.top_level_cmd_name = top_level_cmd_name
self.subcmd_definition = subcmd_definition
self.events_lib = events_lib

def get_command(self, ctx, cmd_name):

"""
gets the Click Commands underneath a service name
Parameters
----------
ctx: Context
context object passed in
cmd_name: string
the service name
Returns
-------
cmd: Click.Command
the Click Commands that can be called from the CLI
"""

if cmd_name not in self.subcmd_definition:
return None
parameters = []
for param_name in self.subcmd_definition[cmd_name][self.TAGS].keys():
default = self.subcmd_definition[cmd_name][self.TAGS][param_name]["default"]
parameters.append(click.Option(
["--{}".format(param_name)],
default=default,
help="Specify the {} name you'd like, otherwise the default = {}".format(param_name, default)
))

command_callback = functools.partial(self.cmd_implementation,
self.events_lib,
self.top_level_cmd_name,
cmd_name)
cmd = click.Command(name=cmd_name,
help=self.subcmd_definition[cmd_name]["help"],
params=parameters,
callback=command_callback)

cmd = debug_option(cmd)
return cmd

def list_commands(self, ctx):
"""
lists the commands underneath a particular event
Parameters
----------
ctx: Context
the context object passed in
Returns
-------
the sorted list of commands under a service
"""
return sorted(self.subcmd_definition.keys())

def cmd_implementation(self, events_lib, top_level_cmd_name, subcmd_name, *args, **kwargs):
"""
calls for value substitution in the event json and returns the
customized json as a string
Parameters
----------
events_lib
top_level_cmd_name: string
the name of the service
subcmd_name: string
the name of the event under the service
args: tuple
any arguments passed in before kwargs
kwargs: dict
the keys and values for substitution in the json
Returns
-------
event: string
returns the customized event json as a string
"""
event = events_lib.generate_event(top_level_cmd_name, subcmd_name, kwargs)
click.echo(event)
return event


class GenerateEventCommand(ServiceCommand):
"""
Class that brings ServiceCommand and EventTypeSubCommand into one for easy execution
"""

def __init__(self, *args, **kwargs):
"""
Constructor for GenerateEventCommand class that brings together
ServiceCommand and EventTypeSubCommand into one class
Parameters
----------
args: tuple
any arguments passed in before kwargs
kwargs: dict
commands, subcommands, and parameters for generate-event
"""
super(GenerateEventCommand, self).__init__(events.Events(), *args, **kwargs)
Empty file.
Loading

0 comments on commit dcd65f4

Please sign in to comment.