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: add sources #4

Open
wants to merge 3 commits into
base: scanlonp-test-updates
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 51 additions & 0 deletions src/source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//import * as fs from 'fs';
//import * as path from 'path';
import { Fn } from 'aws-cdk-lib';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ecr_assets from 'aws-cdk-lib/aws-ecr-assets';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -52,6 +53,14 @@ export abstract class Source {
return new DirectorySource(path);
}

public static ecr(repository: ecr.IRepository, tag: string): Source {
return new EcrSource(repository, tag);
}

public static asset(asset: ecr_assets.DockerImageAsset): Source { // | ecr_assets.TarballImageAsset): Source {
return new AssetSource(asset);
}

/**
* bind grants the CodeBuild role permissions to pull from a repository if necessary
* bind should be invoked by the caller to get the SourceConfig
Expand Down Expand Up @@ -85,3 +94,45 @@ class DirectorySource extends Source {
};
}
}

class EcrSource extends Source {
private repository: ecr.IRepository;
private tag: string;

constructor(repository: ecr.IRepository, tag: string) {
super();
this.repository = repository;
this.tag = tag;
}

public bind(_scope: Construct, context: SourceContext): SourceConfig {

this.repository.grantPull(context.handlerRole);

return {
imageUri: this.repository.repositoryUriForTag(this.tag),
imageTag: this.tag,
};
}
}

class AssetSource extends Source {
private repository: ecr.IRepository;
private tag: string;

constructor(asset: ecr_assets.DockerImageAsset) {
super();
this.repository = asset.repository;
this.tag = Fn.select(1, Fn.split(':', asset.imageUri));
}

public bind(_scope: Construct, context: SourceContext): SourceConfig {

this.repository.grantPull(context.handlerRole);

return {
imageUri: this.repository.repositoryUriForTag(this.tag),
imageTag: this.tag,
};
}
}
44 changes: 44 additions & 0 deletions test/docker-image-deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as path from 'path';
import { Match, Template } from 'aws-cdk-lib/assertions';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ecr_assets from 'aws-cdk-lib/aws-ecr-assets';
import * as cdk from 'aws-cdk-lib/core';
import * as imagedeploy from '../src';

Expand Down Expand Up @@ -308,6 +309,49 @@ describe('DockerImageDeploy', () => {
});
});

describe('Source: ecr', () => {
// GIVEN
const sourceRepo = ecr.Repository.fromRepositoryName(stack, 'TestRepo', 'testrepo');
const ecrSource = imagedeploy.Source.ecr(sourceRepo, 'testtag');

describe('Destination: ecr', () => {
// GIVEN
const destinationRepo = new ecr.Repository(stack, 'TestDestinationRepository1');
const testDesination = imagedeploy.Destination.ecr(destinationRepo, { tag: 'testtag' });

// WHEN
test('dockerimagedeploy construct', () => {
new imagedeploy.DockerImageDeployment(stack, 'AssetSourceTestDeployment', {
source: ecrSource,
destination: testDesination,
});
});
});

});

describe('Source: local asset', () => {
// GIVEN
const asset = new ecr_assets.DockerImageAsset(stack, 'TestAsset', {
directory: path.join(__dirname, 'assets/test1'),
});
const assetSource = imagedeploy.Source.asset(asset);

describe('Destination: ecr', () => {
// GIVEN
const destinationRepo = new ecr.Repository(stack, 'TestDestinationRepository2');
const testDesination = imagedeploy.Destination.ecr(destinationRepo, { tag: 'testtag' });

// WHEN
test('dockerimagedeploy construct', () => {
new imagedeploy.DockerImageDeployment(stack, 'EcrSourceTestDeployment', {
source: assetSource,
destination: testDesination,
});
});
});
});

describe('Custom Resrouces', () => {
test('onEventHandler has correct permissions', () => {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
Expand Down