Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CodeCommit events support (#964) #1443

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ Sql All
AwsIotSqlVersion All
======================== ================================== ========================

CodeCommit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
======================== ================================== ========================
Property Name Intrinsic(s) Supported Reasons
======================== ================================== ========================
RepositoryName None
======================== ================================== ========================

AlexaSkill
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This event has no Properties
Expand Down
57 changes: 57 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
15 changes: 15 additions & 0 deletions samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@
{
"$ref": "#/definitions/AWS::Serverless::Function.LogEvent"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.CodeCommitEvent"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.IoTRuleEvent"
},
Expand Down Expand Up @@ -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": {
Expand Down
12 changes: 12 additions & 0 deletions tests/translator/input/codecommit.yaml
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions tests/translator/output/aws-cn/codecommit.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
}
100 changes: 100 additions & 0 deletions tests/translator/output/aws-us-gov/codecommit.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
}
Loading