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
15 changes: 11 additions & 4 deletions samtranslator/model/stepfunctions/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
from samtranslator.model import PropertyType, ResourceMacro
from samtranslator.model.events import EventsRule
from samtranslator.model.iam import IAMRole, IAMRolePolicies
from samtranslator.model.types import dict_of, is_str, is_type, list_of, one_of
from samtranslator.model.types import is_str, is_type
from samtranslator.model.intrinsics import fnSub
from samtranslator.translator import logical_id_generator
from samtranslator.model.exceptions import InvalidEventException, InvalidResourceException
from samtranslator.model.exceptions import InvalidEventException
from samtranslator.model.eventbridge_utils import EventBridgeRuleUtils
from samtranslator.model.eventsources.push import Api as PushApi
from samtranslator.translator.arn_generator import ArnGenerator
from samtranslator.swagger.swagger import SwaggerEditor
from samtranslator.open_api.open_api import OpenApiEditor

CONDITION = "Condition"
SFN_EVETSOURCE_METRIC_PREFIX = "SFNEventSource"
Expand Down Expand Up @@ -82,6 +80,7 @@ class Schedule(EventSource):
"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 @@ -105,8 +104,16 @@ def to_cloudformation(self, resource, **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
29 changes: 26 additions & 3 deletions tests/model/stepfunctions/test_schedule_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import TestCase
from samtranslator.model.stepfunctions.events import Schedule
from samtranslator.model.exceptions import InvalidEventException
from parameterized import parameterized


class ScheduleEventSource(TestCase):
Expand Down Expand Up @@ -72,12 +73,18 @@ def test_to_cloudformation_returns_eventrule_and_role_resources(self):
def test_to_cloudformation_throws_when_no_resource(self):
self.assertRaises(TypeError, self.schedule_event_source.to_cloudformation)

def test_to_cloudformation_when_event_is_disabled(self):
def test_to_cloudformation_transforms_enabled_boolean_to_state(self):
self.schedule_event_source.Enabled = True
resources = self.schedule_event_source.to_cloudformation(resource=self.state_machine)
self.assertEqual(len(resources), 2)
schedule = resources[0]
self.assertEqual(schedule.State, "ENABLED")

self.schedule_event_source.Enabled = False
resources = self.schedule_event_source.to_cloudformation(resource=self.state_machine)
self.assertEqual(len(resources), 2)
event_rule = resources[0]
self.assertEqual(event_rule.State, "DISABLED")
schedule = resources[0]
self.assertEqual(schedule.State, "DISABLED")

def test_to_cloudformation_with_input(self):
input_to_service = '{"test_key": "test_value"}'
Expand Down Expand Up @@ -144,3 +151,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(resource=self.state_machine)

@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(resource=self.state_machine)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Transform: "AWS::Serverless-2016-10-31"

Resources:
ScheduledStateMachine:
Type: 'AWS::Serverless::StateMachine'
Properties:
DefinitionUri: s3://sam-demo-bucket/my_state_machine.asl.json
Role: arn:aws:iam::123456123456:role/service-role/SampleRole
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Name: TestSchedule
Description: test schedule
State: "Enabled"
Enabled: True
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Transform: "AWS::Serverless-2016-10-31"

Resources:
ScheduledStateMachine:
Type: 'AWS::Serverless::StateMachine'
Properties:
DefinitionUri: s3://sam-demo-bucket/my_state_machine.asl.json
Role: arn:aws:iam::123456123456:role/service-role/SampleRole
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,204 @@
{
"Resources": {
"ScheduledStateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "my_state_machine.asl.json"
},
"RoleArn": "arn:aws:iam::123456123456:role/service-role/SampleRole",
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
}
},
"ScheduledStateMachineSchedule1": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": "Enabled",
"Targets": [
{
"Arn": {
"Ref": "ScheduledStateMachine"
},
"Id": "ScheduledStateMachineSchedule1StepFunctionsTarget",
"RoleArn": {
"Fn::GetAtt": [
"ScheduledStateMachineSchedule1Role",
"Arn"
]
}
}
]
}
},
"ScheduledStateMachineSchedule1Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
}
}
]
},
"Policies": [
{
"PolicyName": "ScheduledStateMachineSchedule1RoleStartExecutionPolicy",
"PolicyDocument": {
"Statement": [
{
"Action": "states:StartExecution",
"Effect": "Allow",
"Resource": {
"Ref": "ScheduledStateMachine"
}
}
]
}
}
]
}
},
"ScheduledStateMachineSchedule2": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": {
"Fn::Sub": "Enabled"
},
"Targets": [
{
"Arn": {
"Ref": "ScheduledStateMachine"
},
"Id": "ScheduledStateMachineSchedule2StepFunctionsTarget",
"RoleArn": {
"Fn::GetAtt": [
"ScheduledStateMachineSchedule2Role",
"Arn"
]
}
}
]
}
},
"ScheduledStateMachineSchedule2Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
}
}
]
},
"Policies": [
{
"PolicyName": "ScheduledStateMachineSchedule2RoleStartExecutionPolicy",
"PolicyDocument": {
"Statement": [
{
"Action": "states:StartExecution",
"Effect": "Allow",
"Resource": {
"Ref": "ScheduledStateMachine"
}
}
]
}
}
]
}
},
"ScheduledStateMachineSchedule3": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "Test Schedule",
"Name": "test-schedule",
"ScheduleExpression": "rate(1 minute)",
"State": {
"Ref": "ScheduleState"
},
"Targets": [
{
"Arn": {
"Ref": "ScheduledStateMachine"
},
"Id": "ScheduledStateMachineSchedule3StepFunctionsTarget",
"RoleArn": {
"Fn::GetAtt": [
"ScheduledStateMachineSchedule3Role",
"Arn"
]
}
}
]
}
},
"ScheduledStateMachineSchedule3Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
}
}
]
},
"Policies": [
{
"PolicyName": "ScheduledStateMachineSchedule3RoleStartExecutionPolicy",
"PolicyDocument": {
"Statement": [
{
"Action": "states:StartExecution",
"Effect": "Allow",
"Resource": {
"Ref": "ScheduledStateMachine"
}
}
]
}
}
]
}
}
}
}
Loading