diff --git a/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/CodePipelineEvent.java b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/CodePipelineEvent.java new file mode 100644 index 00000000..17e7f1b1 --- /dev/null +++ b/aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/CodePipelineEvent.java @@ -0,0 +1,156 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with + * the License. A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + */ + +package com.amazonaws.services.lambda.runtime.events; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +/** + * Represents an CodePipeline event sent to Lambda. + * See: Invoke an AWS Lambda function in a pipeline in CodePipeline + */ +@Data +@Builder(setterPrefix = "with") +@NoArgsConstructor +@AllArgsConstructor +public class CodePipelineEvent implements Serializable { + private static final long serialVersionUID = -4828716548429210697L; + + @JsonProperty("CodePipeline.job") + private Job codePipelineJob; + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Job implements Serializable { + private static final long serialVersionUID = 2211711169692638977L; + + private String id; + private String accountId; + private Data data; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Data implements Serializable { + private static final long serialVersionUID = 8786599041834868262L; + + private ActionConfiguration actionConfiguration; + private List inputArtifacts; + private List outputArtifacts; + private ArtifactCredentials artifactCredentials; + private String continuationToken; + private EncryptionKey encryptionKey; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class ActionConfiguration implements Serializable { + private static final long serialVersionUID = -7285651174501621217L; + + private Configuration configuration; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Configuration implements Serializable { + private static final long serialVersionUID = 580024317691702894L; + + @JsonProperty("FunctionName") + private String functionName; + @JsonProperty("UserParameters") + private String userParameters; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Artifact implements Serializable { + private static final long serialVersionUID = 6406621244704594358L; + + private String name; + private String revision; + private Location location; + + @JsonInclude + public String getRevision() { + return revision; + } + + @JsonInclude + public void setRevision(String revision) { + this.revision = revision; + } + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class Location implements Serializable { + private static final long serialVersionUID = 149382199413534713L; + + private String type; + private S3Location s3Location; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class S3Location implements Serializable { + private static final long serialVersionUID = -8922449809993769709L; + + private String bucketName; + private String objectKey; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class ArtifactCredentials implements Serializable { + private static final long serialVersionUID = 7710347495607396747L; + + private String accessKeyId; + private String secretAccessKey; + private String sessionToken; + } + + @lombok.Data + @Builder(setterPrefix = "with") + @NoArgsConstructor + @AllArgsConstructor + public static class EncryptionKey implements Serializable { + private static final long serialVersionUID = -9105569908901180610L; + + String id; + String type; + } +} diff --git a/aws-lambda-java-events/src/test/java/com/amazonaws/services/lambda/runtime/events/LambdaEventSerializersTest.java b/aws-lambda-java-events/src/test/java/com/amazonaws/services/lambda/runtime/events/LambdaEventSerializersTest.java index 67012557..926f16ac 100644 --- a/aws-lambda-java-events/src/test/java/com/amazonaws/services/lambda/runtime/events/LambdaEventSerializersTest.java +++ b/aws-lambda-java-events/src/test/java/com/amazonaws/services/lambda/runtime/events/LambdaEventSerializersTest.java @@ -180,6 +180,14 @@ public void testCodeCommitEvent() throws IOException, JSONException { assertEquals(expected, actual, STRICT); } + @Test + public void testCodePipelineEvent() throws IOException, JSONException { + String expected = EventUtils.readEvent("code_pipeline_event.json"); + String actual = deserializeSerializeJsonToString(expected, CodePipelineEvent.class); + + assertEquals(expected, actual, STRICT); + } + @Test public void testCognitoEvent() throws IOException, JSONException { String expected = EventUtils.readEvent("cognito_event.json"); diff --git a/aws-lambda-java-events/src/test/resources/event_models/code_pipeline_event.json b/aws-lambda-java-events/src/test/resources/event_models/code_pipeline_event.json new file mode 100644 index 00000000..d0af5934 --- /dev/null +++ b/aws-lambda-java-events/src/test/resources/event_models/code_pipeline_event.json @@ -0,0 +1,38 @@ +{ + "CodePipeline.job": { + "id": "11111111-abcd-1111-abcd-111111abcdef", + "accountId": "111111111111", + "data": { + "actionConfiguration": { + "configuration": { + "FunctionName": "MyLambdaFunctionForAWSCodePipeline", + "UserParameters": "some-input-such-as-a-URL" + } + }, + "inputArtifacts": [ + { + "location": { + "s3Location": { + "bucketName": "the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890", + "objectKey": "the name of the application, for example CodePipelineDemoApplication.zip" + }, + "type": "S3" + }, + "revision": null, + "name": "ArtifactName" + } + ], + "outputArtifacts": [], + "artifactCredentials": { + "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + "sessionToken": "MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE=", + "accessKeyId": "AKIAIOSFODNN7EXAMPLE" + }, + "continuationToken": "A continuation token if continuing job", + "encryptionKey": { + "id": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", + "type": "KMS" + } + } + } +} \ No newline at end of file diff --git a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java index 5947a566..6fb3f7ff 100644 --- a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java +++ b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java @@ -61,6 +61,10 @@ public static CodeCommitEvent loadCodeCommitEvent(String filename) { return loadEvent(filename, CodeCommitEvent.class); } + public static CodePipelineEvent loadCodePipelineEvent(String filename) { + return loadEvent(filename, CodePipelineEvent.class); + } + public static ConfigEvent loadConfigEvent(String filename) { return loadEvent(filename, ConfigEvent.class); } diff --git a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java index 47a9c52c..a0498f2d 100644 --- a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java +++ b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java @@ -226,6 +226,31 @@ public void testLoadCodeCommitEvent() { assertThat(reference.getRef()).isEqualTo("refs/heads/master"); } + @Test + public void testLoadCodePipelineEvent() { + CodePipelineEvent event = EventLoader.loadCodePipelineEvent("codepipeline_event.json"); + + assertThat(event).isNotNull(); + assertThat(event.getCodePipelineJob().getId()).isEqualTo("11111111-abcd-1111-abcd-111111abcdef"); + assertThat(event.getCodePipelineJob().getAccountId()).isEqualTo("111111111111"); + assertThat(event.getCodePipelineJob().getData().getActionConfiguration().getConfiguration().getFunctionName()).isEqualTo("MyLambdaFunctionForAWSCodePipeline"); + assertThat(event.getCodePipelineJob().getData().getActionConfiguration().getConfiguration().getUserParameters()).isEqualTo("some-input-such-as-a-URL"); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts()).hasSize(1); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getType()).isEqualTo("S3"); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getS3Location().getBucketName()).isEqualTo("the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890"); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getS3Location().getObjectKey()).isEqualTo("the name of the application, for example CodePipelineDemoApplication.zip"); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getRevision()).isNull(); + assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getName()).isEqualTo("ArtifactName"); + assertThat(event.getCodePipelineJob().getData().getOutputArtifacts()).hasSize(0); + assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getSecretAccessKey()).isEqualTo("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); + assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getSessionToken()).isEqualTo("MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE="); + assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getAccessKeyId()).isEqualTo("AKIAIOSFODNN7EXAMPLE"); + assertThat(event.getCodePipelineJob().getData().getContinuationToken()).isEqualTo("A continuation token if continuing job"); + assertThat(event.getCodePipelineJob().getData().getEncryptionKey().getId()).isEqualTo("arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"); + assertThat(event.getCodePipelineJob().getData().getEncryptionKey().getType()).isEqualTo("KMS"); + + } + @Test public void testLoadCloudWatchLogsEvent() { CloudWatchLogsEvent cloudWatchLogsEvent = EventLoader.loadCloudWatchLogsEvent("cloudwatchlogs_event.json"); diff --git a/aws-lambda-java-tests/src/test/resources/codepipeline_event.json b/aws-lambda-java-tests/src/test/resources/codepipeline_event.json new file mode 100644 index 00000000..d0af5934 --- /dev/null +++ b/aws-lambda-java-tests/src/test/resources/codepipeline_event.json @@ -0,0 +1,38 @@ +{ + "CodePipeline.job": { + "id": "11111111-abcd-1111-abcd-111111abcdef", + "accountId": "111111111111", + "data": { + "actionConfiguration": { + "configuration": { + "FunctionName": "MyLambdaFunctionForAWSCodePipeline", + "UserParameters": "some-input-such-as-a-URL" + } + }, + "inputArtifacts": [ + { + "location": { + "s3Location": { + "bucketName": "the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890", + "objectKey": "the name of the application, for example CodePipelineDemoApplication.zip" + }, + "type": "S3" + }, + "revision": null, + "name": "ArtifactName" + } + ], + "outputArtifacts": [], + "artifactCredentials": { + "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + "sessionToken": "MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE=", + "accessKeyId": "AKIAIOSFODNN7EXAMPLE" + }, + "continuationToken": "A continuation token if continuing job", + "encryptionKey": { + "id": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", + "type": "KMS" + } + } + } +} \ No newline at end of file