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(batch): Importing a JobDefinition from name using fromJobDefinitionName #10448

Merged
merged 3 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved

// With revision
const job = batch.JobDefinition.fromJobDefinitionName(this, 'imported-job-definition', 'my-job-definition:3');
```
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-batch/lib/job-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-batch/test/job-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});