Skip to content

Commit

Permalink
fix(cli): handle bootstrapping of default environments (aws#3060)
Browse files Browse the repository at this point in the history
"cdk bootstrap" without arguments will automatically bootstrap all environments defined in the app. now that stacks can be environment-agnostic (aws://unknown-account/unknown-region), so we need to make sure that those are replaced by the default account/region.

Tests:

Executed integ test: test-cdk-multiple-toolkit-stacks.sh
Performed some manual tests
  • Loading branch information
Elad Ben-Israel authored Jun 25, 2019
1 parent d55069b commit a7a3bde
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ async function initCommandLine() {

const app = configuration.settings.get(['app']);

const environments = app ? await globEnvironmentsFromStacks(appStacks, environmentGlobs) : environmentsFromDescriptors(environmentGlobs);
const environments = app ? await globEnvironmentsFromStacks(appStacks, environmentGlobs, aws) : environmentsFromDescriptors(environmentGlobs);

// Bucket name can be passed using --toolkit-bucket-name or set in cdk.json
const bucketName = configuration.settings.get(['toolkitBucketName']) || toolkitBucketName;
Expand Down
27 changes: 22 additions & 5 deletions packages/aws-cdk/lib/api/cxapp/environments.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import cxapi = require('@aws-cdk/cx-api');
import minimatch = require('minimatch');
import { SDK } from '../util/sdk';
import { AppStacks } from './stacks';

export async function globEnvironmentsFromStacks(appStacks: AppStacks, environmentGlobs: string[]): Promise<cxapi.Environment[]> {
export async function globEnvironmentsFromStacks(appStacks: AppStacks, environmentGlobs: string[], sdk: SDK): Promise<cxapi.Environment[]> {
if (environmentGlobs.length === 0) {
environmentGlobs = [ '**' ]; // default to ALL
}

const stacks = await appStacks.listStacks();

const availableEnvironments = distinct(stacks
.map(stack => stack.environment)
.filter(env => env && env.account !== cxapi.UNKNOWN_ACCOUNT && env.region !== cxapi.UNKNOWN_REGION) as cxapi.Environment[]);
const availableEnvironments = new Array<cxapi.Environment>();
for (const stack of stacks) {
const actual = await parseEnvironment(sdk, stack.environment);
availableEnvironments.push(actual);
}

const environments = availableEnvironments.filter(env => environmentGlobs.find(glob => minimatch(env!.name, glob)));
const environments = distinct(availableEnvironments).filter(env => environmentGlobs.find(glob => minimatch(env!.name, glob)));
if (environments.length === 0) {
const globs = JSON.stringify(environmentGlobs);
const envList = availableEnvironments.length > 0 ? availableEnvironments.map(env => env!.name).join(', ') : '<none>';
Expand All @@ -23,6 +26,20 @@ export async function globEnvironmentsFromStacks(appStacks: AppStacks, environme
return environments;
}

async function parseEnvironment(sdk: SDK, env: cxapi.Environment): Promise<cxapi.Environment> {
const account = env.account === cxapi.UNKNOWN_ACCOUNT ? await sdk.defaultAccount() : env.account;
const region = env.region === cxapi.UNKNOWN_REGION ? await sdk.defaultRegion() : env.region;

if (!account || !region) {
throw new Error(`Unable to determine default account and/or region`);
}

return {
account, region,
name: cxapi.EnvironmentUtils.format(account, region)
};
}

/**
* Given a set of "<account>/<region>" strings, construct environments for them
*/
Expand Down

0 comments on commit a7a3bde

Please sign in to comment.