Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Schedule(PushEventSource):
"Schedule": PropertyType(True, is_str()),
"Input": PropertyType(False, is_str()),
"Enabled": PropertyType(False, is_type(bool)),
"State": PropertyType(False, is_str()),
"Name": PropertyType(False, is_str()),
"Description": PropertyType(False, is_str()),
"DeadLetterConfig": PropertyType(False, is_type(dict)),
Expand All @@ -122,8 +123,16 @@ def to_cloudformation(self, **kwargs):
resources.append(events_rule)

events_rule.ScheduleExpression = self.Schedule

if self.State and self.Enabled is not None:
raise InvalidEventException(self.relative_id, "State and Enabled Properties cannot both be specified.")

if self.State:
events_rule.State = self.State

if self.Enabled is not None:
events_rule.State = "ENABLED" if self.Enabled else "DISABLED"

events_rule.Name = self.Name
events_rule.Description = self.Description

Expand Down
39 changes: 38 additions & 1 deletion tests/model/eventsources/test_schedule_event_source.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from unittest.mock import Mock, patch
from unittest import TestCase

from samtranslator.model.eventsources.push import Schedule
from samtranslator.model.lambda_ import LambdaFunction
from samtranslator.model.exceptions import InvalidEventException
from parameterized import parameterized


class ScheduleEventSource(TestCase):
Expand All @@ -13,6 +13,27 @@ def setUp(self):
self.schedule_event_source.Schedule = "rate(1 minute)"
self.func = LambdaFunction("func")

def test_to_cloudformation_returns_permission_and_schedule_resources(self):
resources = self.schedule_event_source.to_cloudformation(function=self.func)
self.assertEqual(len(resources), 2)
self.assertEqual(resources[0].resource_type, "AWS::Events::Rule")
self.assertEqual(resources[1].resource_type, "AWS::Lambda::Permission")

schedule = resources[0]
self.assertEqual(schedule.ScheduleExpression, "rate(1 minute)")
self.assertIsNone(schedule.State)

def test_to_cloudformation_transforms_enabled_boolean_to_state(self):
self.schedule_event_source.Enabled = True
resources = self.schedule_event_source.to_cloudformation(function=self.func)
schedule = resources[0]
self.assertEqual(schedule.State, "ENABLED")

self.schedule_event_source.Enabled = False
resources = self.schedule_event_source.to_cloudformation(function=self.func)
schedule = resources[0]
self.assertEqual(schedule.State, "DISABLED")

def test_to_cloudformation_with_retry_policy(self):
retry_policy = {"MaximumRetryAttempts": "10", "MaximumEventAgeInSeconds": "300"}
self.schedule_event_source.RetryPolicy = retry_policy
Expand Down Expand Up @@ -70,3 +91,19 @@ def test_to_cloudformation_with_dlq_generated_with_intrinsic_function_custom_log
self.schedule_event_source.DeadLetterConfig = dead_letter_config
with self.assertRaises(InvalidEventException):
self.schedule_event_source.to_cloudformation(function=self.func)

@parameterized.expand(
[
(True, "Enabled"),
(True, "Disabled"),
(True, {"FN:FakeIntrinsic": "something"}),
(False, "Enabled"),
(False, "Disabled"),
(False, {"FN:FakeIntrinsic": "something"}),
]
)
def test_to_cloudformation_invalid_defined_both_enabled_and_state_provided(self, enabled_value, state_value):
self.schedule_event_source.Enabled = enabled_value
self.schedule_event_source.State = state_value
with self.assertRaises(InvalidEventException):
self.schedule_event_source.to_cloudformation(function=self.func)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Transform: "AWS::Serverless-2016-10-31"

Resources:
ScheduledFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip?versionId=3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO
Handler: hello.handler
Runtime: python3.10
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Name: test-schedule
Description: Test Schedule
State: "Enabled"
Enabled: True
35 changes: 35 additions & 0 deletions tests/translator/input/function_with_event_schedule_state.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Transform: "AWS::Serverless-2016-10-31"
Parameters:
ScheduleState:
Type: String
Default: Disabled

Resources:
ScheduledFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip?versionId=3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO
Handler: hello.handler
Runtime: python3.10
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Name: test-schedule
Description: Test Schedule
State: "Enabled"
Schedule2:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Name: test-schedule
Description: Test Schedule
State: !Sub "Enabled"
Schedule3:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Name: test-schedule
Description: Test Schedule
State: !Ref ScheduleState
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"Parameters": {
"ScheduleState": {
"Type": "String",
"Default": "Disabled"
}
},
"Resources": {
"ScheduledFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip",
"S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"ScheduledFunctionRole",
"Arn"
]
},
"Runtime": "python3.10",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ScheduledFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ScheduledFunctionSchedule1": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": "Enabled",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"ScheduledFunction",
"Arn"
]
},
"Id": "ScheduledFunctionSchedule1LambdaTarget"
}
]
}
},
"ScheduledFunctionSchedule1Permission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ScheduledFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"ScheduledFunctionSchedule1",
"Arn"
]
}
}
},
"ScheduledFunctionSchedule2": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": {
"Fn::Sub": "Enabled"
},
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"ScheduledFunction",
"Arn"
]
},
"Id": "ScheduledFunctionSchedule2LambdaTarget"
}
]
}
},
"ScheduledFunctionSchedule2Permission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ScheduledFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"ScheduledFunctionSchedule2",
"Arn"
]
}
}
},
"ScheduledFunctionSchedule3": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": {
"Ref": "ScheduleState"
},
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"ScheduledFunction",
"Arn"
]
},
"Id": "ScheduledFunctionSchedule3LambdaTarget"
}
]
}
},
"ScheduledFunctionSchedule3Permission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ScheduledFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"ScheduledFunctionSchedule3",
"Arn"
]
}
}
}
}
}
Loading