Skip to content

Commit

Permalink
Merge branch 'master' into swar8080/cli-change-set-name-AND-retain-em…
Browse files Browse the repository at this point in the history
…pty-change-set
  • Loading branch information
mergify[bot] committed Feb 9, 2021
2 parents c8ffcb0 + 4f17f92 commit cd3e62b
Show file tree
Hide file tree
Showing 14 changed files with 608 additions and 147 deletions.
89 changes: 44 additions & 45 deletions packages/@aws-cdk/aws-codepipeline/test/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as codepipeline from '../lib';
import { FakeBuildAction } from './fake-build-action';
import { FakeSourceAction } from './fake-source-action';

/* eslint-disable quote-props */

nodeunitShim({
'Pipeline': {
'can be passed an IAM role during pipeline creation'(test: Test) {
describe('', () => {
describe('Pipeline', () => {
test('can be passed an IAM role during pipeline creation', () => {
const stack = new cdk.Stack();
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('codepipeline.amazonaws.com'),
Expand All @@ -32,23 +31,23 @@ nodeunitShim({
},
}));

test.done();
},

'can be imported by ARN'(test: Test) {
});

test('can be imported by ARN', () => {
const stack = new cdk.Stack();

const pipeline = codepipeline.Pipeline.fromPipelineArn(stack, 'Pipeline',
'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');

test.equal(pipeline.pipelineArn, 'arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');
test.equal(pipeline.pipelineName, 'MyPipeline');
expect(pipeline.pipelineArn).toEqual('arn:aws:codepipeline:us-east-1:123456789012:MyPipeline');
expect(pipeline.pipelineName).toEqual('MyPipeline');


test.done();
},
});

'that is cross-region': {
'validates that source actions are in the same region as the pipeline'(test: Test) {
describe('that is cross-region', () => {
test('validates that source actions are in the same region as the pipeline', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack', { env: { region: 'us-west-1', account: '123456789012' } });
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');
Expand All @@ -61,14 +60,14 @@ nodeunitShim({
region: 'ap-southeast-1',
});

test.throws(() => {
expect(() => {
sourceStage.addAction(sourceAction);
}, /Source action 'FakeSource' must be in the same region as the pipeline/);
}).toThrow(/Source action 'FakeSource' must be in the same region as the pipeline/);


test.done();
},
});

'allows passing an Alias in place of the KMS Key in the replication Bucket'(test: Test) {
test('allows passing an Alias in place of the KMS Key in the replication Bucket', () => {
const app = new cdk.App();

const replicationRegion = 'us-west-1';
Expand Down Expand Up @@ -173,10 +172,10 @@ nodeunitShim({
},
});

test.done();
},

"generates ArtifactStores with the alias' ARN as the KeyID"(test: Test) {
});

test('generates ArtifactStores with the alias ARN as the KeyID', () => {
const app = new cdk.App();
const replicationRegion = 'us-west-1';

Expand Down Expand Up @@ -239,10 +238,10 @@ nodeunitShim({
'UpdateReplacePolicy': 'Delete',
}, ResourcePart.CompleteDefinition);

test.done();
},

'allows passing an imported Bucket and Key for the replication Bucket'(test: Test) {
});

test('allows passing an imported Bucket and Key for the replication Bucket', () => {
const replicationRegion = 'us-west-1';

const pipelineRegion = 'us-west-2';
Expand Down Expand Up @@ -296,10 +295,10 @@ nodeunitShim({
],
});

test.done();
},

'generates the support stack containing the replication Bucket without the need to bootstrap in that environment'(test: Test) {
});

test('generates the support stack containing the replication Bucket without the need to bootstrap in that environment', () => {
const app = new cdk.App({
treeMetadata: false, // we can't set the context otherwise, because App will have a child
});
Expand Down Expand Up @@ -331,17 +330,17 @@ nodeunitShim({

const assembly = app.synth();
const supportStackArtifact = assembly.getStackByName('PipelineStack-support-eu-south-1');
test.equal(supportStackArtifact.assumeRoleArn,
expect(supportStackArtifact.assumeRoleArn).toEqual(
'arn:${AWS::Partition}:iam::123456789012:role/cdk-hnb659fds-deploy-role-123456789012-us-west-2');
test.equal(supportStackArtifact.cloudFormationExecutionRoleArn,
expect(supportStackArtifact.cloudFormationExecutionRoleArn).toEqual(
'arn:${AWS::Partition}:iam::123456789012:role/cdk-hnb659fds-cfn-exec-role-123456789012-us-west-2');

test.done();
},
},

'that is cross-account': {
'does not allow passing a dynamic value in the Action account property'(test: Test) {
});
});

describe('that is cross-account', () => {
test('does not allow passing a dynamic value in the Action account property', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack', { env: { account: '123456789012' } });
const sourceOutput = new codepipeline.Artifact();
Expand All @@ -355,18 +354,18 @@ nodeunitShim({
});
const buildStage = pipeline.addStage({ stageName: 'Build' });

test.throws(() => {
expect(() => {
buildStage.addAction(new FakeBuildAction({
actionName: 'FakeBuild',
input: sourceOutput,
account: cdk.Aws.ACCOUNT_ID,
}));
}, /The 'account' property must be a concrete value \(action: 'FakeBuild'\)/);
}).toThrow(/The 'account' property must be a concrete value \(action: 'FakeBuild'\)/);


test.done();
},
});

'does not allow an env-agnostic Pipeline Stack if an Action account has been provided'(test: Test) {
test('does not allow an env-agnostic Pipeline Stack if an Action account has been provided', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'PipelineStack');
const sourceOutput = new codepipeline.Artifact();
Expand All @@ -380,18 +379,18 @@ nodeunitShim({
});
const buildStage = pipeline.addStage({ stageName: 'Build' });

test.throws(() => {
expect(() => {
buildStage.addAction(new FakeBuildAction({
actionName: 'FakeBuild',
input: sourceOutput,
account: '123456789012',
}));
}, /Pipeline stack which uses cross-environment actions must have an explicitly set account/);
}).toThrow(/Pipeline stack which uses cross-environment actions must have an explicitly set account/);


test.done();
},
},
},
});
});
});
});

describe('test with shared setup', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ examples of things you might want to use:
> `cdk.context.json`, or use the `cdk context` command. For more information, see
> [Runtime Context](https://docs.aws.amazon.com/cdk/latest/guide/context.html) in the CDK
> developer guide.
>
> `MachineImage.genericLinux()`, `MachineImage.genericWindows()` will use `CfnMapping` in
> an agnostic stack.
## Special VPC configurations

Expand Down
54 changes: 36 additions & 18 deletions packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ssm from '@aws-cdk/aws-ssm';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { ContextProvider, Stack, Token } from '@aws-cdk/core';
import { ContextProvider, CfnMapping, Aws, Stack, Token } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { UserData } from './user-data';
import { WindowsVersion } from './windows-versions';
Expand Down Expand Up @@ -367,24 +367,33 @@ export interface GenericWindowsImageProps {
* manually specify an AMI map.
*/
export class GenericLinuxImage implements IMachineImage {
constructor(private readonly amiMap: {[region: string]: string}, private readonly props: GenericLinuxImageProps = {}) {
constructor(private readonly amiMap: { [region: string]: string }, private readonly props: GenericLinuxImageProps = {}) {
}

public getImage(scope: Construct): MachineImageConfig {
const userData = this.props.userData ?? UserData.forLinux();
const osType = OperatingSystemType.LINUX;
const region = Stack.of(scope).region;
if (Token.isUnresolved(region)) {
throw new Error('Unable to determine AMI from AMI map since stack is region-agnostic');
const mapping: { [k1: string]: { [k2: string]: any } } = {};
for (const [rgn, ami] of Object.entries(this.amiMap)) {
mapping[rgn] = { ami };
}
const amiMap = new CfnMapping(scope, 'AmiMap', { mapping });
return {
imageId: amiMap.findInMap(Aws.REGION, 'ami'),
userData,
osType,
};
}

const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!ami) {
const imageId = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!imageId) {
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
}

return {
imageId: ami,
userData: this.props.userData ?? UserData.forLinux(),
osType: OperatingSystemType.LINUX,
imageId,
userData,
osType,
};
}
}
Expand All @@ -399,20 +408,29 @@ export class GenericWindowsImage implements IMachineImage {
}

public getImage(scope: Construct): MachineImageConfig {
const userData = this.props.userData ?? UserData.forWindows();
const osType = OperatingSystemType.WINDOWS;
const region = Stack.of(scope).region;
if (Token.isUnresolved(region)) {
throw new Error('Unable to determine AMI from AMI map since stack is region-agnostic');
const mapping: { [k1: string]: { [k2: string]: any } } = {};
for (const [rgn, ami] of Object.entries(this.amiMap)) {
mapping[rgn] = { ami };
}
const amiMap = new CfnMapping(scope, 'AmiMap', { mapping });
return {
imageId: amiMap.findInMap(Aws.REGION, 'ami'),
userData,
osType,
};
}

const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!ami) {
const imageId = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!imageId) {
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
}

return {
imageId: ami,
userData: this.props.userData ?? UserData.forWindows(),
osType: OperatingSystemType.WINDOWS,
imageId,
userData,
osType,
};
}
}
Expand Down
Loading

0 comments on commit cd3e62b

Please sign in to comment.