Skip to content

Commit

Permalink
refactor(codepipeline): change the API of cross-region replication Bu…
Browse files Browse the repository at this point in the history
…ckets.

Users are now supposed to pass instances of IBucket instead of just Bucket names.

Changed the name and type of the Pipeline.crossRegionScaffold property.

BREAKING CHANGE: the Pipeline construction property crossRegionReplicationBuckets now takes values of type IBucket instead of string.
* The property Pipeline.crossRegionScaffoldStacks has been renamed to crossRegionSupport,
and its type changed from CrossRegionScaffoldStack to CrossRegionSupport.
  • Loading branch information
skinny85 committed Jun 20, 2019
1 parent 67f6de0 commit 65ba73b
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 58 deletions.
106 changes: 92 additions & 14 deletions packages/@aws-cdk/aws-codepipeline-actions/test/test.pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, haveResourceLike, not, SynthUtils } from '@aws-cdk/assert';
import { countResources, expect, haveResource, haveResourceLike, not, SynthUtils } from '@aws-cdk/assert';
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codepipeline = require('@aws-cdk/aws-codepipeline');
Expand Down Expand Up @@ -596,19 +596,19 @@ export = {
account: pipelineAccount,
},
});
const bucket = new s3.Bucket(stack, 'MyBucket');
const pipeline = new codepipeline.Pipeline(stack, 'MyPipeline', {
crossRegionReplicationBuckets: {
'us-west-1': 'sfo-replication-bucket',
'us-west-1': s3.Bucket.fromBucketName(stack, 'ImportedBucket', 'sfo-replication-bucket'),
},
});

const sourceBucket = new s3.Bucket(stack, 'MyBucket');
const sourceOutput = new codepipeline.Artifact('SourceOutput');
const sourceAction = new cpactions.S3SourceAction({
actionName: 'BucketSource',
bucketKey: '/some/key',
output: sourceOutput,
bucket,
bucket: sourceBucket,
});
pipeline.addStage({
stageName: 'Stage1',
Expand Down Expand Up @@ -648,19 +648,25 @@ export = {
"Region": "us-east-1",
"ArtifactStore": {
"Type": "S3",
"Location": "cdk-cross-region-codepipeline-replication-bucket-685c6feea5fb",
},
},
{
"Region": "us-west-1",
"ArtifactStore": {
"Location": "sfo-replication-bucket",
"Type": "S3",
"Location": "sfo-replication-bucket",
},
},
{
"Region": "us-west-2",
"ArtifactStore": {
"Type": "S3",
"EncryptionKey": {
"Type": "KMS",
"Id": {
},
},
},
},
],
Expand All @@ -685,18 +691,90 @@ export = {
},
],
},
]
],
}));

test.equal(pipeline.crossRegionScaffolding[pipelineRegion], undefined);
test.equal(pipeline.crossRegionScaffolding['us-west-1'], undefined);
test.equal(pipeline.crossRegionSupport[pipelineRegion], undefined);
test.equal(pipeline.crossRegionSupport['us-west-1'], undefined);

const usEast1Support = pipeline.crossRegionSupport['us-east-1'];
test.notEqual(usEast1Support, undefined);
test.equal(usEast1Support.stack.region, 'us-east-1');
test.equal(usEast1Support.stack.account, pipelineAccount);
test.ok(usEast1Support.stack.node.id.indexOf('us-east-1') !== -1,
`expected '${usEast1Support.stack.node.id}' to contain 'us-east-1'`);

test.done();
},

'allows specifying only one of artifactBucket and crossRegionReplicationBuckets'(test: Test) {
const stack = new Stack();

test.throws(() => {
new codepipeline.Pipeline(stack, 'Pipeline', {
artifactBucket: new s3.Bucket(stack, 'Bucket'),
crossRegionReplicationBuckets: {
// even an empty map should trigger this validation...
},
});
}, /Only one of artifactBucket and crossRegionReplicationBuckets can be specified!/);
test.done();
},

'does not create a new artifact Bucket if one was provided in the cross-region Buckets for the Pipeline region'(test: Test) {
const pipelineRegion = 'us-west-2';

const stack = new Stack(undefined, undefined, {
env: {
region: pipelineRegion,
},
});
const sourceOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(stack, 'Pipeline', {
crossRegionReplicationBuckets: {
[pipelineRegion]: new s3.Bucket(stack, 'Bucket', {
bucketName: PhysicalName.of('my-pipeline-bucket'),
})
},
stages: [
{
stageName: 'Source',
actions: [
new cpactions.CodeCommitSourceAction({
actionName: 'Source',
output: sourceOutput,
repository: new codecommit.Repository(stack, 'Repo', { repositoryName: PhysicalName.of('Repo'), }),
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'Build',
input: sourceOutput,
project: new codebuild.PipelineProject(stack, 'Project'),
}),
],
},
],
});

expect(stack).to(countResources('AWS::S3::Bucket', 1));

const usEast1ScaffoldStack = pipeline.crossRegionScaffolding['us-east-1'];
test.notEqual(usEast1ScaffoldStack, undefined);
test.equal(usEast1ScaffoldStack.region, 'us-east-1');
test.equal(usEast1ScaffoldStack.account, pipelineAccount);
test.ok(usEast1ScaffoldStack.node.id.indexOf('us-east-1') !== -1,
`expected '${usEast1ScaffoldStack.node.id}' to contain 'us-east-1'`);
expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"ArtifactStores": [
{
"Region": pipelineRegion,
"ArtifactStore": {
"Type": "S3",
"Location": {
"Ref": "Bucket83908E77",
},
},
},
],
}));

test.done();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import crypto = require('crypto');
import { CrossRegionScaffolding } from './pipeline';

/**
* Construction properties for {@link CrossRegionScaffoldStack}.
* Construction properties for {@link CrossRegionSupportStack}.
* This interface is private to the aws-codepipeline package.
*/
export interface CrossRegionScaffoldStackProps {
export interface CrossRegionSupportStackProps {
/**
* The AWS region this Stack resides in.
*/
Expand All @@ -22,14 +22,15 @@ export interface CrossRegionScaffoldStackProps {

/**
* A Stack containing resources required for the cross-region CodePipeline functionality to work.
* This class is private to the aws-codepipeline package.
*/
export class CrossRegionScaffoldStack extends CrossRegionScaffolding {
export class CrossRegionSupportStack extends cdk.Stack {
/**
* The name of the S3 Bucket used for replicating the Pipeline's artifacts into the region.
*/
public readonly replicationBucketName: string;
public readonly replicationBucket: s3.IBucket;

constructor(scope: cdk.Construct, id: string, props: CrossRegionScaffoldStackProps) {
constructor(scope: cdk.Construct, id: string, props: CrossRegionSupportStackProps) {
super(scope, id, {
stackName: generateStackName(props),
env: {
Expand All @@ -41,14 +42,13 @@ export class CrossRegionScaffoldStack extends CrossRegionScaffolding {
const replicationBucketName = generateUniqueName('cdk-cross-region-codepipeline-replication-bucket-',
props.region, props.account, false, 12);

new s3.Bucket(this, 'CrossRegionCodePipelineReplicationBucket', {
this.replicationBucket = new s3.Bucket(this, 'CrossRegionCodePipelineReplicationBucket', {
bucketName: cdk.PhysicalName.of(replicationBucketName),
});
this.replicationBucketName = replicationBucketName;
}
}

function generateStackName(props: CrossRegionScaffoldStackProps): string {
function generateStackName(props: CrossRegionSupportStackProps): string {
return `aws-cdk-codepipeline-cross-region-scaffolding-${props.region}`;
}

Expand Down
Loading

0 comments on commit 65ba73b

Please sign in to comment.