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(ecr): lookup existing repository #33662

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
add unit test
  • Loading branch information
badmintoncryer committed Mar 3, 2025
commit 1aae54992f5844142950a0b73e86db719154f4a9
9 changes: 8 additions & 1 deletion packages/aws-cdk-lib/aws-ecr/lib/repository.ts
Original file line number Diff line number Diff line change
@@ -641,11 +641,18 @@ export class Repository extends RepositoryBase {
throw new Error('At least one of `repositoryName` or `repositoryArn` must be provided.');
}

const identifier = options.repositoryName ??
(options.repositoryArn ? Arn.split(options.repositoryArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName : undefined);

if (!identifier) {
throw new Error('Could not determine repository identifier from provided options.');
}

const response: {[key: string]: any}[] = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.CC_API_PROVIDER,
props: {
typeName: 'AWS::ECR::Repository',
exactIdentifier: options.repositoryName,
exactIdentifier: identifier,
propertiesToReturn: ['Arn'],
} as cxschema.CcApiContextQuery,
dummyValue: [
103 changes: 103 additions & 0 deletions packages/aws-cdk-lib/aws-ecr/test/repository.test.ts
Original file line number Diff line number Diff line change
@@ -3,11 +3,114 @@ import { Annotations, Template } from '../../assertions';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as cdk from '../../core';
import * as cxschema from '../../cloud-assembly-schema';
import * as ecr from '../lib';
import { ContextProvider } from '../../core';

/* eslint-disable quote-props */

describe('repository', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test case to check if provided ECR repo (name or ARN) does not exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

describe('lookup', () => {
test('return correct repository info by name', () => {
// GIVEN
const resultObjs = [
{
'Arn': 'arn:aws:ecr:us-east-1:123456789012:repository/my-repo',
},
];
const value = {
value: resultObjs,
};
const mock = jest.spyOn(ContextProvider, 'getValue').mockReturnValue(value);

// WHEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } });
const repo = ecr.Repository.fromLookup(stack, 'MyRepo', {
repositoryName: 'my-repo',
});

// THEN
expect(repo.repositoryName).toEqual('my-repo');
expect(repo.repositoryArn).toEqual(ecr.Repository.arnForLocalRepository('my-repo', stack));
expect(mock).toHaveBeenCalledWith(stack, {
provider: cxschema.ContextProvider.CC_API_PROVIDER,
props: {
typeName: 'AWS::ECR::Repository',
exactIdentifier: 'my-repo',
propertiesToReturn: [
'Arn',
],
} as cxschema.CcApiContextQuery,
dummyValue: [
{
'Arn': ecr.Repository.arnForLocalRepository('DUMMY_ARN', stack),
},
],
});

mock.mockRestore();
});

test('return correct repository info by arn', () => {
const repoArn = 'arn:aws:ecr::123456789012:repository/my-repo';
// GIVEN
const resultObjs = [
{
'Arn': repoArn,
},
];
const value = {
value: resultObjs,
};
const mock = jest.spyOn(ContextProvider, 'getValue').mockReturnValue(value);

// WHEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } });
const repo = ecr.Repository.fromLookup(stack, 'MyRepo', {
repositoryArn: repoArn,
});

// THEN
expect(repo.repositoryName).toEqual('my-repo');
expect(repo.repositoryArn).toEqual(ecr.Repository.arnForLocalRepository('my-repo', stack));
expect(mock).toHaveBeenCalledWith(stack, {
provider: cxschema.ContextProvider.CC_API_PROVIDER,
props: {
typeName: 'AWS::ECR::Repository',
exactIdentifier: 'my-repo',
propertiesToReturn: [
'Arn',
],
} as cxschema.CcApiContextQuery,
dummyValue: [
{
'Arn': ecr.Repository.arnForLocalRepository('DUMMY_ARN', stack),
},
],
});

mock.mockRestore();
});

test('throw error if repository name is a token', () => {
// GIVEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } });
const tokenName = new cdk.CfnParameter(stack, 'RepoName');
expect(() => ecr.Repository.fromLookup(stack, 'MyRepository', {
repositoryName: tokenName.valueAsString,
})).toThrow('Cannot look up a repository with a tokenized name or ARN.');
});

test('throw error if repository arn is a token', () => {
// GIVEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } });
const tokenName = new cdk.CfnParameter(stack, 'RepoArn');
expect(() => ecr.Repository.fromLookup(stack, 'MyRepository', {
repositoryArn: tokenName.valueAsString,
})).toThrow('Cannot look up a repository with a tokenized name or ARN.');
});
});

test('construct repository', () => {
// GIVEN
const stack = new cdk.Stack();
Loading
Oops, something went wrong.