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

fix(cli): conversion of "tags" filter for EC2 DescribeVpcs call #3393

Merged
merged 8 commits into from
Jul 29, 2019
Merged
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
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-backup/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ dist
.LAST_PACKAGE
.jsii

*.tsbuildinfo
*.tsbuildinfo

# Include .jsii
!.jsii
13 changes: 12 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ export class Vpc extends VpcBase {
* Import an existing VPC from by querying the AWS environment this stack is deployed to.
*/
public static fromLookup(scope: Construct, id: string, options: VpcLookupOptions): IVpc {
const filter: {[key: string]: string} = options.tags || {};
const filter: {[key: string]: string} = makeTagFilter(options.tags);

// We give special treatment to some tags
if (options.vpcId) { filter['vpc-id'] = options.vpcId; }
Expand All @@ -701,6 +701,17 @@ export class Vpc extends VpcBase {
});

return this.fromVpcAttributes(scope, id, attributes);

/**
* Prefixes all keys in the argument with `tag:`.`
*/
function makeTagFilter(tags: { [name: string]: string } | undefined): { [name: string]: string } {
const result: { [name: string]: string } = {};
for (const [name, value] of Object.entries(tags || {})) {
result[`tag:${name}`] = value;
}
return result;
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-medialive/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ dist
.LAST_PACKAGE
.jsii

*.tsbuildinfo
*.tsbuildinfo

# Include .jsii
!.jsii
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-securityhub/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ dist
.LAST_PACKAGE
.jsii

*.tsbuildinfo
*.tsbuildinfo

# Include .jsii
!.jsii
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/context-providers/vpcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class VpcNetworkContextProviderPlugin implements ContextProviderPlugin {

private async findVpc(ec2: AWS.EC2, args: cxapi.VpcContextQuery): Promise<string> {
// Build request filter (map { Name -> Value } to list of [{ Name, Values }])
const filters: AWS.EC2.Filter[] = Object.entries(args.filter).map(x => ({ Name: x[0], Values: [x[1]] }));
const filters: AWS.EC2.Filter[] = Object.entries(args.filter).map(([tag, value]) => ({ Name: tag, Values: [value] }));

debug(`Listing VPCs in ${args.account}:${args.region}`);
const response = await ec2.describeVpcs({ Filters: filters }).promise();
Expand Down
31 changes: 31 additions & 0 deletions packages/aws-cdk/test/integ/cli/app/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const cdk = require('@aws-cdk/core');
const ec2 = require('@aws-cdk/aws-ec2');
const ssm = require('@aws-cdk/aws-ssm');
const iam = require('@aws-cdk/aws-iam');
const sns = require('@aws-cdk/aws-sns');
Expand Down Expand Up @@ -101,6 +102,28 @@ class DockerStack extends cdk.Stack {
}
}

const VPC_TAG_NAME = 'custom-tag';
const VPC_TAG_VALUE = 'bazinga!';

class DefineVpcStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);

new ec2.Vpc(this, 'VPC', {
maxAzs: 1,
}).node.applyAspect(new cdk.Tag(VPC_TAG_NAME, VPC_TAG_VALUE));
}
}

class ImportVpcStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);

ec2.Vpc.fromLookup(this, 'DefaultVPC', { isDefault: true });
ec2.Vpc.fromLookup(this, 'ByTag', { tags: { [VPC_TAG_NAME]: VPC_TAG_VALUE } });
}
}

const stackPrefix = process.env.STACK_NAME_PREFIX || 'cdk-toolkit-integration';

const app = new cdk.App();
Expand All @@ -123,4 +146,12 @@ new MissingSSMParameterStack(app, `${stackPrefix}-missing-ssm-parameter`, { env:
new LambdaStack(app, `${stackPrefix}-lambda`);
new DockerStack(app, `${stackPrefix}-docker`);

if (process.env.ENABLE_VPC_TESTING) { // Gating so we don't do context fetching unless that's what we are here for
const env = { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION };
if (process.env.ENABLE_VPC_TESTING === 'DEFINE')
new DefineVpcStack(app, `${stackPrefix}-define-vpc`, { env });
if (process.env.ENABLE_VPC_TESTING === 'IMPORT')
new ImportVpcStack(app, `${stackPrefix}-import-vpc`, { env });
}

app.synth();
1 change: 1 addition & 0 deletions packages/aws-cdk/test/integ/cli/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function setup() {
install_dep @aws-cdk/aws-lambda
install_dep @aws-cdk/aws-ssm
install_dep @aws-cdk/aws-ecr-assets
install_dep @aws-cdk/aws-ec2

echo "| setup complete at: $PWD"
echo "| 'cdk' is: $(which cdk)"
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk/test/integ/cli/test-vpc-lookup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -euo pipefail
scriptdir=$(cd $(dirname $0) && pwd)
source ${scriptdir}/common.bash
# ----------------------------------------------------------

setup

echo "Setting up: creating a VPC with known tags"
ENABLE_VPC_TESTING="DEFINE" cdk deploy ${STACK_NAME_PREFIX}-define-vpc
echo "Setup complete!"

# verify we can synth the importing stack now
echo "Verifying we can now import that VPC"
ENABLE_VPC_TESTING="IMPORT" cdk synth -v ${STACK_NAME_PREFIX}-import-vpc

# destroy
echo "Cleaning up..."
ENABLE_VPC_TESTING="DEFINE" cdk destroy -f ${STACK_NAME_PREFIX}-define-vpc

echo "✅ success"