From 8a100e5286e2dd7575248f9c7cc1415d1f2890f3 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Mon, 29 Jul 2019 14:46:35 -0700 Subject: [PATCH] feat(codepipeline): make Pipeline importable by ARN (#3469) Add the capability to import an externally-defined CodePipeline using Pipeline.fromPipelineArn(). Fixes #3467 --- .../@aws-cdk/aws-codepipeline/lib/pipeline.ts | 20 +++++++++++++++++-- .../aws-codepipeline/test/test.pipeline.ts | 12 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts b/packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts index c1169e722e91d..653a3868fe86f 100644 --- a/packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts +++ b/packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts @@ -103,8 +103,8 @@ export interface PipelineProps { } abstract class PipelineBase extends Resource implements IPipeline { - public abstract pipelineName: string; - public abstract pipelineArn: string; + public abstract readonly pipelineName: string; + public abstract readonly pipelineArn: string; /** * Defines an event rule triggered by this CodePipeline. @@ -159,6 +159,22 @@ abstract class PipelineBase extends Resource implements IPipeline { * // ... add more stages */ export class Pipeline extends PipelineBase { + /** + * Import a pipeline into this app. + * + * @param scope the scope into which to import this pipeline + * @param id the logical ID of the returned pipeline construct + * @param pipelineArn The ARN of the pipeline (e.g. `arn:aws:codepipeline:us-east-1:123456789012:MyDemoPipeline`) + */ + public static fromPipelineArn(scope: Construct, id: string, pipelineArn: string): IPipeline { + class Import extends PipelineBase { + public readonly pipelineName = Stack.of(scope).parseArn(pipelineArn).resource; + public readonly pipelineArn = pipelineArn; + } + + return new Import(scope, id); + } + /** * The IAM role AWS CodePipeline will use to perform actions or assume roles for actions with * a more specific IAM role. diff --git a/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts b/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts index ceb3b625f3247..3a3df493c79dc 100644 --- a/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts +++ b/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts @@ -28,5 +28,17 @@ export = { test.done(); }, + + 'can be imported by ARN'(test: Test) { + const stack = new cdk.Stack(); + + const pipeline = codepipeline.Pipeline.fromPipelineArn(stack, 'Pipeline', + 'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline'); + + test.equal(pipeline.pipelineArn, 'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline'); + test.equal(pipeline.pipelineName, 'MyPipeline'); + + test.done(); + }, }, };