From 18fbdacae9cadc6189ae5fba349220673a9575a8 Mon Sep 17 00:00:00 2001 From: lucas hanson Date: Mon, 17 Apr 2023 13:03:29 -0700 Subject: [PATCH 1/2] adding getEnvironment method --- API.md | 15 +++++++++++++++ src/config/configurator.ts | 23 +++++++++++++++++++++++ test/config.test.ts | 12 ++++++++++++ test/test-config.json | 2 ++ 4 files changed, 52 insertions(+) diff --git a/API.md b/API.md index 3db42e36..653b099d 100644 --- a/API.md +++ b/API.md @@ -9522,6 +9522,7 @@ public tagConstruct(scope: Construct, tags: {[ key: string ]: string}): void | **Name** | **Description** | | --- | --- | | getEnvConfig | *No description.* | +| getEnvironment | *No description.* | | getTags | *No description.* | --- @@ -9540,6 +9541,20 @@ Configurator.getEnvConfig(props: GetEnvConfigProps) --- +##### `getEnvironment` + +```typescript +import { Configurator } from 'aws-ddk-core' + +Configurator.getEnvironment(props: GetEnvConfigProps) +``` + +###### `props`Required + +- *Type:* GetEnvConfigProps + +--- + ##### `getTags` ```typescript diff --git a/src/config/configurator.ts b/src/config/configurator.ts index 29509602..41b8caae 100644 --- a/src/config/configurator.ts +++ b/src/config/configurator.ts @@ -48,6 +48,18 @@ export function getConfig(props: getConfigProps): any { return configData; } +export function getEnvironment(config: object | string, environmentId?: string): any { + const configData = getConfig({ config: config }); + return configData.environments && environmentId + ? { + env: { + account: configData.environments[environmentId].account, + region: configData.environments[environmentId].region, + }, + } + : { env: { account: configData.account, region: configData.region } }; +} + interface getStackSynthesizerProps { readonly config?: string | object; readonly environmentId: string; @@ -149,6 +161,17 @@ export class Configurator { ? config.tags : {}; } + public static getEnvironment(props: GetEnvConfigProps): any { + const config = getConfig({ config: props.configPath }); + return config.environments + ? { + env: { + account: config.environments[props.environmentId].account, + region: config.environments[props.environmentId].region, + }, + } + : undefined; + } public readonly config: any; public readonly environmentId?: string; constructor(scope: constructs.Construct, config: string | object, environmentId?: string) { diff --git a/test/config.test.ts b/test/config.test.ts index e1b32778..044e411f 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -15,6 +15,7 @@ import { SqsToLambdaStage, getConfig, getStackSynthesizer, + getEnvironment, } from "../src"; test("Config Simple Override", () => { @@ -450,6 +451,17 @@ test("Get Env Config", () => { assert(getConfig({}) === undefined); }); +test("Get Environment", () => { + assert(getEnvironment("./test/test-config.json", "dev").account === "222222222222"); + assert(getEnvironment("./test/test-config.json", "dev").region === "us-east-1"); + assert(getEnvironment("./test/test-config.json").account === "111111111111"); + assert(getEnvironment("./test/test-config.json").region === "us-east-1"); + const app = new cdk.App(); + new cdk.Stack(app, "MyTestStack", { + ...getEnvironment("./test/test-config.json"), + }); +}); + test("Get Env Config Static Method", () => { const config = Configurator.getEnvConfig({ configPath: "./test/test-config.yaml", environmentId: "dev" }); const nullConfig = Configurator.getEnvConfig({ configPath: "./ddk.json", environmentId: "dev" }); diff --git a/test/test-config.json b/test/test-config.json index 68d50167..13c9b591 100644 --- a/test/test-config.json +++ b/test/test-config.json @@ -2,6 +2,8 @@ "tags": { "global:foo": "bar" }, + "account": "111111111111", + "region": "us-east-1", "environments": { "dev": { "account": "222222222222", From e22debe2cbcff80246f2f48134b7a5b2d01516aa Mon Sep 17 00:00:00 2001 From: lucas hanson Date: Mon, 17 Apr 2023 13:13:25 -0700 Subject: [PATCH 2/2] adding static method fix --- API.md | 43 ++++++++++++++++++++++++++++++++++++-- src/config/configurator.ts | 16 +++++++++++--- test/config.test.ts | 9 ++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index 653b099d..f2b8dcad 100644 --- a/API.md +++ b/API.md @@ -7207,6 +7207,45 @@ public readonly environmentId: string; --- +### GetEnvironmentProps + +#### Initializer + +```typescript +import { GetEnvironmentProps } from 'aws-ddk-core' + +const getEnvironmentProps: GetEnvironmentProps = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| configPath | string | *No description.* | +| environmentId | string | *No description.* | + +--- + +##### `configPath`Required + +```typescript +public readonly configPath: string; +``` + +- *Type:* string + +--- + +##### `environmentId`Optional + +```typescript +public readonly environmentId: string; +``` + +- *Type:* string + +--- + ### GetSynthActionProps #### Initializer @@ -9546,12 +9585,12 @@ Configurator.getEnvConfig(props: GetEnvConfigProps) ```typescript import { Configurator } from 'aws-ddk-core' -Configurator.getEnvironment(props: GetEnvConfigProps) +Configurator.getEnvironment(props: GetEnvironmentProps) ``` ###### `props`Required -- *Type:* GetEnvConfigProps +- *Type:* GetEnvironmentProps --- diff --git a/src/config/configurator.ts b/src/config/configurator.ts index 41b8caae..96395469 100644 --- a/src/config/configurator.ts +++ b/src/config/configurator.ts @@ -146,6 +146,11 @@ export interface GetTagsProps { readonly environmentId?: string; } +export interface GetEnvironmentProps { + readonly configPath: string; + readonly environmentId?: string; +} + export class Configurator { public static getEnvConfig(props: GetEnvConfigProps): any { const config = getConfig({ config: props.configPath }); @@ -161,16 +166,21 @@ export class Configurator { ? config.tags : {}; } - public static getEnvironment(props: GetEnvConfigProps): any { + public static getEnvironment(props: GetEnvironmentProps): any { const config = getConfig({ config: props.configPath }); - return config.environments + return config.environments && props.environmentId ? { env: { account: config.environments[props.environmentId].account, region: config.environments[props.environmentId].region, }, } - : undefined; + : { + env: { + account: config.account, + region: config.region, + }, + }; } public readonly config: any; public readonly environmentId?: string; diff --git a/test/config.test.ts b/test/config.test.ts index 044e411f..998fb704 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -456,6 +456,15 @@ test("Get Environment", () => { assert(getEnvironment("./test/test-config.json", "dev").region === "us-east-1"); assert(getEnvironment("./test/test-config.json").account === "111111111111"); assert(getEnvironment("./test/test-config.json").region === "us-east-1"); + assert( + Configurator.getEnvironment({ configPath: "./test/test-config.json", environmentId: "dev" }).account === + "222222222222", + ); + assert( + Configurator.getEnvironment({ configPath: "./test/test-config.json", environmentId: "dev" }).region === "us-east-1", + ); + assert(Configurator.getEnvironment({ configPath: "./test/test-config.json" }).account === "111111111111"); + assert(Configurator.getEnvironment({ configPath: "./test/test-config.json" }).region === "us-east-1"); const app = new cdk.App(); new cdk.Stack(app, "MyTestStack", { ...getEnvironment("./test/test-config.json"),