From 4589ec4840a263b819ba5fe0b335614ea8b56743 Mon Sep 17 00:00:00 2001 From: ayush987goyal Date: Sun, 20 Sep 2020 11:47:46 +0000 Subject: [PATCH] feat(batch): add import helper --- packages/@aws-cdk/aws-batch/README.md | 19 +++++++++++++- .../@aws-cdk/aws-batch/lib/job-definition.ts | 25 +++++++++++++++++++ .../aws-batch/test/job-definition.test.ts | 10 ++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-batch/README.md b/packages/@aws-cdk/aws-batch/README.md index 314d647dec76e..c4a818e7abc6a 100644 --- a/packages/@aws-cdk/aws-batch/README.md +++ b/packages/@aws-cdk/aws-batch/README.md @@ -242,10 +242,27 @@ new batch.JobDefinition(stack, 'batch-job-def-from-local', { ### Importing an existing Job Definition -To import an existing batch job definition, call `JobDefinition.fromJobDefinitionArn()`. +#### From ARN + +To import an existing batch job definition from its ARN, call `JobDefinition.fromJobDefinitionArn()`. Below is an example: ```ts const job = batch.JobDefinition.fromJobDefinitionArn(this, 'imported-job-definition', 'arn:aws:batch:us-east-1:555555555555:job-definition/my-job-definition'); ``` + +#### From Name + +To import an existing batch job definition from its name, call `JobDefinition.fromJobDefinitionName()`. +If name is specified without a revision then the latest active revision is used. + +Below is an example: + +```ts +// Without revision +const job = batch.JobDefinition.fromJobDefinitionName(this, 'imported-job-definition', 'my-job-definition'); + +// With revision +const job = batch.JobDefinition.fromJobDefinitionName(this, 'imported-job-definition', 'my-job-definition:3'); +``` diff --git a/packages/@aws-cdk/aws-batch/lib/job-definition.ts b/packages/@aws-cdk/aws-batch/lib/job-definition.ts index 4de1549bc86e0..a8d2259c08a55 100644 --- a/packages/@aws-cdk/aws-batch/lib/job-definition.ts +++ b/packages/@aws-cdk/aws-batch/lib/job-definition.ts @@ -269,6 +269,31 @@ export class JobDefinition extends Resource implements IJobDefinition { return new Import(scope, id); } + /** + * Imports an existing batch job definition by its name. + * If name is specified without a revision then the latest active revision is used. + * + * @param scope + * @param id + * @param jobDefinitionName + */ + public static fromJobDefinitionName(scope: Construct, id: string, jobDefinitionName: string): IJobDefinition { + const stack = Stack.of(scope); + const jobDefArn = stack.formatArn({ + service: 'batch', + resource: 'job-definition', + sep: '/', + resourceName: jobDefinitionName, + }); + + class Import extends Resource implements IJobDefinition { + public readonly jobDefinitionArn = jobDefArn; + public readonly jobDefinitionName = jobDefinitionName; + } + + return new Import(scope, id); + } + public readonly jobDefinitionArn: string; public readonly jobDefinitionName: string; private readonly imageConfig: JobDefinitionImageConfig; diff --git a/packages/@aws-cdk/aws-batch/test/job-definition.test.ts b/packages/@aws-cdk/aws-batch/test/job-definition.test.ts index de7dc08aea71d..71129c4906c9e 100644 --- a/packages/@aws-cdk/aws-batch/test/job-definition.test.ts +++ b/packages/@aws-cdk/aws-batch/test/job-definition.test.ts @@ -200,4 +200,14 @@ describe('Batch Job Definition', () => { expect(importedJob.jobDefinitionName).toEqual('job-def-name:1'); expect(importedJob.jobDefinitionArn).toEqual('arn:aws:batch:us-east-1:123456789012:job-definition/job-def-name:1'); }); + + test('can be imported from a name', () => { + // WHEN + const importedJob = batch.JobDefinition.fromJobDefinitionName(stack, 'job-def-clone', 'job-def-name'); + + // THEN + expect(importedJob.jobDefinitionName).toEqual('job-def-name'); + expect(importedJob.jobDefinitionArn) + .toEqual('arn:${Token[AWS.Partition.3]}:batch:${Token[AWS.Region.4]}:${Token[AWS.AccountId.0]}:job-definition/job-def-name'); + }); });