From 3e42a4f31bd0473f865e51a8358c7dbaac6c7c48 Mon Sep 17 00:00:00 2001 From: Eduardo Vieira Date: Tue, 28 Jan 2020 00:01:42 -0300 Subject: [PATCH] feat: CodeCommit events support (#964) --- docs/cloudformation_compatibility.rst | 8 ++ samtranslator/model/eventsources/push.py | 57 ++++++++++ .../validator/sam_schema/schema.json | 15 +++ tests/translator/input/codecommit.yaml | 12 +++ .../translator/output/aws-cn/codecommit.json | 100 ++++++++++++++++++ .../output/aws-us-gov/codecommit.json | 100 ++++++++++++++++++ tests/translator/output/codecommit.json | 100 ++++++++++++++++++ tests/translator/test_translator.py | 1 + tests/translator/validator/test_validator.py | 1 + versions/2016-10-31.md | 19 ++++ 10 files changed, 413 insertions(+) create mode 100644 tests/translator/input/codecommit.yaml create mode 100644 tests/translator/output/aws-cn/codecommit.json create mode 100644 tests/translator/output/aws-us-gov/codecommit.json create mode 100644 tests/translator/output/codecommit.json diff --git a/docs/cloudformation_compatibility.rst b/docs/cloudformation_compatibility.rst index b431d9d27..4809f2dc2 100644 --- a/docs/cloudformation_compatibility.rst +++ b/docs/cloudformation_compatibility.rst @@ -169,6 +169,14 @@ Sql All AwsIotSqlVersion All ======================== ================================== ======================== +CodeCommit +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +======================== ================================== ======================== + Property Name Intrinsic(s) Supported Reasons +======================== ================================== ======================== +RepositoryName None +======================== ================================== ======================== + AlexaSkill ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This event has no Properties diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index 82b7ddeb1..0f3407c94 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -469,6 +469,63 @@ def _inject_sqs_queue_policy(self, topic_arn, queue_arn, queue_url, logical_id=N return policy +class CodeCommit(PushEventSource): + """CodeCommit event source for SAM Functions.""" + + resource_type = "CodeCommit" + principal = "events.amazonaws.com" + property_types = {"RepositoryName": PropertyType(True, is_str())} + + def to_cloudformation(self, **kwargs): + """ + Returns the CloudWatch Events/EventBridge Rule and Lambda Permission allowing + CodeCommit to invoke the function. + + :param dict kwargs: no existing resources need to be modified + :returns: a list of vanilla CloudFormation Resources, to which this CodeCommit event expands + :rtype: list + """ + function = kwargs.get("function") + + if not function: + raise TypeError("Missing required keyword argument: function") + + resources = [] + + events_rule = EventsRule(self.logical_id) + events_rule.EventPattern = self._construct_pattern() + events_rule.Targets = [self._construct_target(function)] + if CONDITION in function.resource_attributes: + events_rule.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION]) + + resources.append(events_rule) + + source_arn = events_rule.get_runtime_attr("arn") + resources.append(self._construct_permission(function, source_arn=source_arn)) + + return resources + + def _construct_pattern(self): + partition = ArnGenerator.get_partition_name() + resource = fnSub( + ArnGenerator.generate_arn(partition=partition, service="codecommit", resource=self.RepositoryName) + ) + + pattern = {"source": ["aws.codecommit"], "resources": [resource]} + + return pattern + + def _construct_target(self, function): + """Constructs the Target property for the CodeCommit Rule. + + :returns: the Target property + :rtype: dict + """ + target = {"Arn": function.get_runtime_attr("arn"), "Id": self.logical_id + "LambdaTarget"} + + return target + + class Api(PushEventSource): """Api method event source for SAM Functions.""" diff --git a/samtranslator/validator/sam_schema/schema.json b/samtranslator/validator/sam_schema/schema.json index c18f10620..0357b5703 100644 --- a/samtranslator/validator/sam_schema/schema.json +++ b/samtranslator/validator/sam_schema/schema.json @@ -495,6 +495,9 @@ { "$ref": "#/definitions/AWS::Serverless::Function.LogEvent" }, + { + "$ref": "#/definitions/AWS::Serverless::Function.CodeCommitEvent" + }, { "$ref": "#/definitions/AWS::Serverless::Function.IoTRuleEvent" }, @@ -549,6 +552,18 @@ ], "type": "object" }, + "AWS::Serverless::Function.CodeCommitEvent": { + "additionalProperties": false, + "properties": { + "RepositoryName": { + "type": "string" + } + }, + "required": [ + "RepositoryName" + ], + "type": "object" + }, "AWS::Serverless::Function.IoTRuleEvent": { "additionalProperties": false, "properties": { diff --git a/tests/translator/input/codecommit.yaml b/tests/translator/input/codecommit.yaml new file mode 100644 index 000000000..0e6736ab2 --- /dev/null +++ b/tests/translator/input/codecommit.yaml @@ -0,0 +1,12 @@ +Resources: + TriggeredFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip?versionId=3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO + Handler: lambda_function.lambda_handler + Runtime: python3.7 + Events: + CodeCommitTrigger: + Type: CodeCommit + Properties: + RepositoryName: my-repository diff --git a/tests/translator/output/aws-cn/codecommit.json b/tests/translator/output/aws-cn/codecommit.json new file mode 100644 index 000000000..912e8861c --- /dev/null +++ b/tests/translator/output/aws-cn/codecommit.json @@ -0,0 +1,100 @@ +{ + "Resources": { + "TriggeredFunctionCodeCommitTriggerPermission": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "Principal": "events.amazonaws.com", + "FunctionName": { + "Ref": "TriggeredFunction" + }, + "SourceArn": { + "Fn::GetAtt": [ + "TriggeredFunctionCodeCommitTrigger", + "Arn" + ] + } + } + }, + "TriggeredFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "lambda_function.lambda_handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip", + "S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO" + }, + "Role": { + "Fn::GetAtt": [ + "TriggeredFunctionRole", + "Arn" + ] + }, + "Runtime": "python3.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "TriggeredFunctionCodeCommitTrigger": { + "Type": "AWS::Events::Rule", + "Properties": { + "EventPattern": { + "source": [ + "aws.codecommit" + ], + "resources": [ + { + "Fn::Sub": "arn:aws-cn:codecommit:${AWS::Region}:${AWS::AccountId}:my-repository" + } + ] + }, + "Targets": [ + { + "Id": "TriggeredFunctionCodeCommitTriggerLambdaTarget", + "Arn": { + "Fn::GetAtt": [ + "TriggeredFunction", + "Arn" + ] + } + } + ] + } + }, + "TriggeredFunctionRole": { + "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": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/codecommit.json b/tests/translator/output/aws-us-gov/codecommit.json new file mode 100644 index 000000000..6498a870a --- /dev/null +++ b/tests/translator/output/aws-us-gov/codecommit.json @@ -0,0 +1,100 @@ +{ + "Resources": { + "TriggeredFunctionCodeCommitTriggerPermission": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "Principal": "events.amazonaws.com", + "FunctionName": { + "Ref": "TriggeredFunction" + }, + "SourceArn": { + "Fn::GetAtt": [ + "TriggeredFunctionCodeCommitTrigger", + "Arn" + ] + } + } + }, + "TriggeredFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "lambda_function.lambda_handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip", + "S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO" + }, + "Role": { + "Fn::GetAtt": [ + "TriggeredFunctionRole", + "Arn" + ] + }, + "Runtime": "python3.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "TriggeredFunctionCodeCommitTrigger": { + "Type": "AWS::Events::Rule", + "Properties": { + "EventPattern": { + "source": [ + "aws.codecommit" + ], + "resources": [ + { + "Fn::Sub": "arn:aws-us-gov:codecommit:${AWS::Region}:${AWS::AccountId}:my-repository" + } + ] + }, + "Targets": [ + { + "Id": "TriggeredFunctionCodeCommitTriggerLambdaTarget", + "Arn": { + "Fn::GetAtt": [ + "TriggeredFunction", + "Arn" + ] + } + } + ] + } + }, + "TriggeredFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/codecommit.json b/tests/translator/output/codecommit.json new file mode 100644 index 000000000..c2cd6a8fb --- /dev/null +++ b/tests/translator/output/codecommit.json @@ -0,0 +1,100 @@ +{ + "Resources": { + "TriggeredFunctionCodeCommitTriggerPermission": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "Principal": "events.amazonaws.com", + "FunctionName": { + "Ref": "TriggeredFunction" + }, + "SourceArn": { + "Fn::GetAtt": [ + "TriggeredFunctionCodeCommitTrigger", + "Arn" + ] + } + } + }, + "TriggeredFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "lambda_function.lambda_handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip", + "S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO" + }, + "Role": { + "Fn::GetAtt": [ + "TriggeredFunctionRole", + "Arn" + ] + }, + "Runtime": "python3.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "TriggeredFunctionCodeCommitTrigger": { + "Type": "AWS::Events::Rule", + "Properties": { + "EventPattern": { + "source": [ + "aws.codecommit" + ], + "resources": [ + { + "Fn::Sub": "arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:my-repository" + } + ] + }, + "Targets": [ + { + "Id": "TriggeredFunctionCodeCommitTriggerLambdaTarget", + "Arn": { + "Fn::GetAtt": [ + "TriggeredFunction", + "Arn" + ] + } + } + ] + } + }, + "TriggeredFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index e1480b183..390971317 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -155,6 +155,7 @@ class TestTranslatorEndToEnd(TestCase): "eventbridgerule_schedule_properties", "cloudwatch_logs_with_ref", "cloudwatchlog", + "codecommit", "streams", "sqs", "simpletable", diff --git a/tests/translator/validator/test_validator.py b/tests/translator/validator/test_validator.py index ceff650a8..6806586dc 100644 --- a/tests/translator/validator/test_validator.py +++ b/tests/translator/validator/test_validator.py @@ -15,6 +15,7 @@ "eventbridgerule", "cloudwatch_logs_with_ref", "cloudwatchlog", + "codecommit", "streams", "sqs", "simpletable", diff --git a/versions/2016-10-31.md b/versions/2016-10-31.md index accba9e21..5b433760a 100644 --- a/versions/2016-10-31.md +++ b/versions/2016-10-31.md @@ -425,6 +425,7 @@ Properties: - [IoTRule](#iotrule) - [AlexaSkill](#alexaskill) - [Cognito](#cognito) + - [CodeCommit](#codecommit) #### S3 @@ -848,6 +849,24 @@ Properties: Type: AlexaSkill ``` +#### CodeCommit + +The object describing an event source with type `CodeCommit`. + +##### Properties + +Property Name | Type | Description +---|:---:|--- +RepositoryName | `string` | **Required.** Name of CodeCommit Repository. + +##### Example: CodeCommit source object + +```yaml +Type: CodeCommit +Properties: + RepositoryName: my-repository +``` + #### Provisioned Concurrency Config object The object describing provisioned concurrency settings on a Lambda Alias