From c41235457e52a9357349f27da31ca4f11fe42671 Mon Sep 17 00:00:00 2001 From: Seb Kasprzak Date: Wed, 12 Oct 2022 15:15:20 +1100 Subject: [PATCH 1/3] Add ability to set Name of EventBridge Rule as Lambda Event Source and SFN --- samtranslator/model/eventsources/push.py | 3 +++ samtranslator/model/stepfunctions/events.py | 2 ++ samtranslator/validator/sam_schema/schema.json | 3 +++ tests/model/eventsources/test_eventbridge_rule_source.py | 6 ++++++ tests/model/stepfunctions/test_eventbridge_rule_source.py | 6 ++++++ 5 files changed, 20 insertions(+) diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index c38f072a1..616497d7e 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -97,6 +97,7 @@ class Schedule(PushEventSource): principal = "events.amazonaws.com" property_types = { "Schedule": PropertyType(True, is_str()), + "RuleName": PropertyType(False, is_str()), "Input": PropertyType(False, is_str()), "Enabled": PropertyType(False, is_type(bool)), "State": PropertyType(False, is_str()), @@ -180,6 +181,7 @@ class CloudWatchEvent(PushEventSource): principal = "events.amazonaws.com" property_types = { "EventBusName": PropertyType(False, is_str()), + "RuleName": PropertyType(False, is_str()), "Pattern": PropertyType(False, is_type(dict)), "DeadLetterConfig": PropertyType(False, is_type(dict)), "RetryPolicy": PropertyType(False, is_type(dict)), @@ -210,6 +212,7 @@ def to_cloudformation(self, **kwargs): events_rule = EventsRule(self.logical_id, attributes=passthrough_resource_attributes) events_rule.EventBusName = self.EventBusName events_rule.EventPattern = self.Pattern + events_rule.Name = self.RuleName source_arn = events_rule.get_runtime_attr("arn") dlq_queue_arn = None diff --git a/samtranslator/model/stepfunctions/events.py b/samtranslator/model/stepfunctions/events.py index 33c0d000c..420a51eb9 100644 --- a/samtranslator/model/stepfunctions/events.py +++ b/samtranslator/model/stepfunctions/events.py @@ -165,6 +165,7 @@ class CloudWatchEvent(EventSource): principal = "events.amazonaws.com" property_types = { "EventBusName": PropertyType(False, is_str()), + "RuleName": PropertyType(False, is_str()), "Pattern": PropertyType(False, is_type(dict)), "Input": PropertyType(False, is_str()), "InputPath": PropertyType(False, is_str()), @@ -190,6 +191,7 @@ def to_cloudformation(self, resource, **kwargs): events_rule = EventsRule(self.logical_id, attributes=passthrough_resource_attributes) events_rule.EventBusName = self.EventBusName events_rule.EventPattern = self.Pattern + events_rule.Name = self.RuleName if self.State: events_rule.State = self.State diff --git a/samtranslator/validator/sam_schema/schema.json b/samtranslator/validator/sam_schema/schema.json index 83a31d87f..393004f6b 100644 --- a/samtranslator/validator/sam_schema/schema.json +++ b/samtranslator/validator/sam_schema/schema.json @@ -374,6 +374,9 @@ "State": { "type": "string" }, + "RuleName": { + "type": "string" + }, "DeadLetterConfig": { "additionalProperties": false, "properties": { diff --git a/tests/model/eventsources/test_eventbridge_rule_source.py b/tests/model/eventsources/test_eventbridge_rule_source.py index 345650bb9..31fb93a85 100644 --- a/tests/model/eventsources/test_eventbridge_rule_source.py +++ b/tests/model/eventsources/test_eventbridge_rule_source.py @@ -32,6 +32,12 @@ def test_state_when_provided(self): state = cfn[0].State self.assertEqual(state, "DISABLED") + def test_name_when_provided(self): + self.eb_event_source.RuleName = "MyRule" + cfn = self.eb_event_source.to_cloudformation(function=self.func) + name = cfn[0].Name + self.assertEqual(name, "MyRule") + def test_to_cloudformation_with_retry_policy(self): retry_policy = {"MaximumRetryAttempts": "10", "MaximumEventAgeInSeconds": "300"} self.eb_event_source.RetryPolicy = retry_policy diff --git a/tests/model/stepfunctions/test_eventbridge_rule_source.py b/tests/model/stepfunctions/test_eventbridge_rule_source.py index 2b1032928..4f6654210 100644 --- a/tests/model/stepfunctions/test_eventbridge_rule_source.py +++ b/tests/model/stepfunctions/test_eventbridge_rule_source.py @@ -39,3 +39,9 @@ def test_to_cloudformation_with_state(self): resources = self.eb_event_source.to_cloudformation(resource=self.state_machine) state = resources[0].State self.assertEqual(state, "DISABLED") + + def test_name_when_provided(self): + self.eb_event_source.RuleName = "MyRule" + resources = self.eb_event_source.to_cloudformation(resource=self.state_machine) + event_rule = resources[0] + self.assertEqual(event_rule.Name, "MyRule") From 546ddd621718c3ba15cddad85275696164856ceb Mon Sep 17 00:00:00 2001 From: Seb Kasprzak Date: Mon, 17 Oct 2022 10:55:02 +0800 Subject: [PATCH 2/3] added transform tests for Eventbridge rule name --- tests/translator/input/eventbridgerule_with_dlq.yaml | 1 + tests/translator/input/state_machine_with_cwe.yaml | 1 + tests/translator/output/aws-cn/eventbridgerule_with_dlq.json | 1 + tests/translator/output/aws-cn/state_machine_with_cwe.json | 1 + tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json | 1 + tests/translator/output/aws-us-gov/state_machine_with_cwe.json | 1 + tests/translator/output/eventbridgerule_with_dlq.json | 1 + tests/translator/output/state_machine_with_cwe.json | 1 + 8 files changed, 8 insertions(+) diff --git a/tests/translator/input/eventbridgerule_with_dlq.yaml b/tests/translator/input/eventbridgerule_with_dlq.yaml index e651e6520..00422fde1 100644 --- a/tests/translator/input/eventbridgerule_with_dlq.yaml +++ b/tests/translator/input/eventbridgerule_with_dlq.yaml @@ -24,6 +24,7 @@ Resources: Type: EventBridgeRule Properties: EventBusName: ExternalEventBridge + RuleName: MyRule State: ENABLED Pattern: detail: diff --git a/tests/translator/input/state_machine_with_cwe.yaml b/tests/translator/input/state_machine_with_cwe.yaml index ee43797ff..cb08710de 100644 --- a/tests/translator/input/state_machine_with_cwe.yaml +++ b/tests/translator/input/state_machine_with_cwe.yaml @@ -8,6 +8,7 @@ Resources: CWEvent: Type: CloudWatchEvent Properties: + RuleName: MyRule State: ENABLED Pattern: detail: diff --git a/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json b/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json index 42a20c35a..c9941e769 100644 --- a/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json +++ b/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json @@ -79,6 +79,7 @@ } }, "EventBusName": "ExternalEventBridge", + "Name": "MyRule", "State" : "ENABLED", "Targets": [ { diff --git a/tests/translator/output/aws-cn/state_machine_with_cwe.json b/tests/translator/output/aws-cn/state_machine_with_cwe.json index 95940a353..07a305a24 100644 --- a/tests/translator/output/aws-cn/state_machine_with_cwe.json +++ b/tests/translator/output/aws-cn/state_machine_with_cwe.json @@ -10,6 +10,7 @@ ] } }, + "Name": "MyRule", "State": "ENABLED", "Targets": [ { diff --git a/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json b/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json index 17da23c66..b3a6d9d16 100644 --- a/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json +++ b/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json @@ -79,6 +79,7 @@ } }, "EventBusName": "ExternalEventBridge", + "Name": "MyRule", "State" : "ENABLED", "Targets": [ { diff --git a/tests/translator/output/aws-us-gov/state_machine_with_cwe.json b/tests/translator/output/aws-us-gov/state_machine_with_cwe.json index 95940a353..07a305a24 100644 --- a/tests/translator/output/aws-us-gov/state_machine_with_cwe.json +++ b/tests/translator/output/aws-us-gov/state_machine_with_cwe.json @@ -10,6 +10,7 @@ ] } }, + "Name": "MyRule", "State": "ENABLED", "Targets": [ { diff --git a/tests/translator/output/eventbridgerule_with_dlq.json b/tests/translator/output/eventbridgerule_with_dlq.json index 5e9bd4ab0..b730860d0 100644 --- a/tests/translator/output/eventbridgerule_with_dlq.json +++ b/tests/translator/output/eventbridgerule_with_dlq.json @@ -79,6 +79,7 @@ } }, "EventBusName": "ExternalEventBridge", + "Name": "MyRule", "State" : "ENABLED", "Targets": [ { diff --git a/tests/translator/output/state_machine_with_cwe.json b/tests/translator/output/state_machine_with_cwe.json index 95940a353..07a305a24 100644 --- a/tests/translator/output/state_machine_with_cwe.json +++ b/tests/translator/output/state_machine_with_cwe.json @@ -10,6 +10,7 @@ ] } }, + "Name": "MyRule", "State": "ENABLED", "Targets": [ { From 1c15db13ced56cc2006ca179e5cfb71d38bd11d2 Mon Sep 17 00:00:00 2001 From: Sam Liu Date: Mon, 24 Oct 2022 16:37:16 -0700 Subject: [PATCH 3/3] Format files --- tests/translator/output/aws-cn/eventbridgerule_with_dlq.json | 2 +- tests/translator/output/aws-cn/state_machine_with_cwe.json | 2 +- .../translator/output/aws-us-gov/eventbridgerule_with_dlq.json | 2 +- tests/translator/output/aws-us-gov/state_machine_with_cwe.json | 2 +- tests/translator/output/eventbridgerule_with_dlq.json | 2 +- tests/translator/output/state_machine_with_cwe.json | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json b/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json index 613a18c58..65c77a3a5 100644 --- a/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json +++ b/tests/translator/output/aws-cn/eventbridgerule_with_dlq.json @@ -171,7 +171,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [ diff --git a/tests/translator/output/aws-cn/state_machine_with_cwe.json b/tests/translator/output/aws-cn/state_machine_with_cwe.json index a34976ab9..b674effc3 100644 --- a/tests/translator/output/aws-cn/state_machine_with_cwe.json +++ b/tests/translator/output/aws-cn/state_machine_with_cwe.json @@ -24,7 +24,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [ diff --git a/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json b/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json index 8ac521ad6..0c7f07483 100644 --- a/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json +++ b/tests/translator/output/aws-us-gov/eventbridgerule_with_dlq.json @@ -171,7 +171,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [ diff --git a/tests/translator/output/aws-us-gov/state_machine_with_cwe.json b/tests/translator/output/aws-us-gov/state_machine_with_cwe.json index a34976ab9..b674effc3 100644 --- a/tests/translator/output/aws-us-gov/state_machine_with_cwe.json +++ b/tests/translator/output/aws-us-gov/state_machine_with_cwe.json @@ -24,7 +24,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [ diff --git a/tests/translator/output/eventbridgerule_with_dlq.json b/tests/translator/output/eventbridgerule_with_dlq.json index 2a49d4086..2d5c4826c 100644 --- a/tests/translator/output/eventbridgerule_with_dlq.json +++ b/tests/translator/output/eventbridgerule_with_dlq.json @@ -171,7 +171,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [ diff --git a/tests/translator/output/state_machine_with_cwe.json b/tests/translator/output/state_machine_with_cwe.json index a34976ab9..b674effc3 100644 --- a/tests/translator/output/state_machine_with_cwe.json +++ b/tests/translator/output/state_machine_with_cwe.json @@ -24,7 +24,7 @@ "terminated" ] } - }, + }, "Name": "MyRule", "State": "ENABLED", "Targets": [