diff --git a/API.md b/API.md index b54e2fe2..abe3f9d8 100644 --- a/API.md +++ b/API.md @@ -6436,6 +6436,45 @@ public readonly s3BucketProps: BucketProps; --- +### GetEnvConfigProps + +#### Initializer + +```typescript +import { GetEnvConfigProps } from 'aws-ddk-core' + +const getEnvConfigProps: GetEnvConfigProps = { ... } +``` + +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| configPath | string | *No description.* | +| environmentId | string | *No description.* | + +--- + +##### `configPath`Required + +```typescript +public readonly configPath: string; +``` + +- *Type:* string + +--- + +##### `environmentId`Required + +```typescript +public readonly environmentId: string; +``` + +- *Type:* string + +--- + ### GetSynthActionProps #### Initializer @@ -8465,18 +8504,18 @@ new Configurator(scope: Construct, config: string | object, environmentId?: stri | **Name** | **Description** | | --- | --- | -| getEnvConfig | *No description.* | +| getConfigAttribute | *No description.* | | tagConstruct | *No description.* | --- -##### `getEnvConfig` +##### `getConfigAttribute` ```typescript -public getEnvConfig(attribute: string): any +public getConfigAttribute(attribute: string): any ``` -###### `attribute`Required +###### `attribute`Required - *Type:* string @@ -8500,6 +8539,27 @@ public tagConstruct(scope: Construct, tags: {[ key: string ]: string}): void --- +#### Static Functions + +| **Name** | **Description** | +| --- | --- | +| getEnvConfig | *No description.* | + +--- + +##### `getEnvConfig` + +```typescript +import { Configurator } from 'aws-ddk-core' + +Configurator.getEnvConfig(props: GetEnvConfigProps) +``` + +###### `props`Required + +- *Type:* GetEnvConfigProps + +--- #### Properties diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 98fc4fb5..f1b8fdb4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -54,7 +54,6 @@ npm install projen npx projen test ``` - ### Integration Testing The integration tests leverage the [`integ-runner` CLI ](https://github.com/aws/aws-cdk/tree/main/packages/%40aws-cdk/integ-runner) diff --git a/src/cicd/pipelines.ts b/src/cicd/pipelines.ts index d894b968..8f21218a 100644 --- a/src/cicd/pipelines.ts +++ b/src/cicd/pipelines.ts @@ -147,7 +147,7 @@ export class CICDPipelineStack extends BaseStack { if (this.pipeline === undefined) { throw new Error("`.buildPipeline()` needs to be called first before adding application stages to the pipeline."); } - const manualApprovals = props.manualApprovals ?? this.config.getEnvConfig("manual_approvals") ?? false; + const manualApprovals = props.manualApprovals ?? this.config.getConfigAttribute("manual_approvals") ?? false; if (manualApprovals) { this.pipeline?.addStage(props.stage, { @@ -164,7 +164,7 @@ export class CICDPipelineStack extends BaseStack { if (this.pipeline === undefined) { throw new Error("`.buildPipeline()` needs to be called first before adding application stages to the pipeline."); } - const manualApprovals = props.manualApprovals ?? this.config.getEnvConfig("manual_approvals") ?? false; + const manualApprovals = props.manualApprovals ?? this.config.getConfigAttribute("manual_approvals") ?? false; var wave = new pipelines.Wave(props.stageId); if (manualApprovals) { @@ -219,11 +219,11 @@ export class CICDPipelineStack extends BaseStack { } const topic = - this.environmentId && this.config.getEnvConfig("notifications_topic_arn") + this.environmentId && this.config.getConfigAttribute("notifications_topic_arn") ? sns.Topic.fromTopicArn( this, "ExecutionFailedNotifications", - this.config.getEnvConfig("notifications_topic_arn"), + this.config.getConfigAttribute("notifications_topic_arn"), ) : new sns.Topic(this, "ExecutionFailedNotifications"); this.notificationRule = diff --git a/src/config/configurator.ts b/src/config/configurator.ts index a8edb687..b51f01a8 100644 --- a/src/config/configurator.ts +++ b/src/config/configurator.ts @@ -48,15 +48,6 @@ export function getConfig(props: getConfigProps): any { return configData; } -interface getEnvConfigProps { - readonly config?: string | object; - readonly environmentId: string; -} -export function getEnvConfig(props: getEnvConfigProps): any { - const config = getConfig({ config: props.config }); - return config[props.environmentId]; -} - interface getStackSynthesizerProps { readonly config?: string | object; readonly environmentId: string; @@ -128,7 +119,15 @@ class ConfiguratorAspect implements cdk.IAspect { } } +export interface GetEnvConfigProps { + readonly configPath: string; + readonly environmentId: string; +} + export class Configurator { + public static getEnvConfig(props: GetEnvConfigProps): any { + return getConfig({ config: props.configPath })[props.environmentId]; + } public readonly config: any; public readonly environmentId?: string; constructor(scope: constructs.Construct, config: string | object, environmentId?: string) { @@ -175,7 +174,7 @@ export class Configurator { Object.entries(tags).forEach(([key, value]) => cdk.Tags.of(scope).add(key, value)); } } - getEnvConfig(attribute: string): any { + getConfigAttribute(attribute: string): any { return this.environmentId && this.config[this.environmentId] && this.config[this.environmentId][attribute] ? this.config[this.environmentId][attribute] : undefined; diff --git a/test/config.test.ts b/test/config.test.ts index 302ca2a0..95344b48 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -13,7 +13,6 @@ import { FirehoseToS3Stage, SqsToLambdaStage, getStackSynthesizer, - getEnvConfig, } from "../src"; test("Config Simple Override", () => { @@ -361,24 +360,49 @@ test("CICDPipeline with Config", () => { }); }); -test("Get Env Config Method", () => { - const devConfig = getEnvConfig({ config: "./test/test-config.json", environmentId: "dev" }); +test("Get Env Config Attribute", () => { + const app = new cdk.App(); + const config = new Configurator(app, "./test/test-config.json", "dev"); + assert(config.getConfigAttribute("account") === "222222222222"); +}); + +test("Get Config : Non-Existent File", () => { + const app = new cdk.App(); + const config = new Configurator(app, "./test/not-real.yaml", "dev"); + const expectedDevConfig = {}; + assert(config.getConfigAttribute("foo") === expectedDevConfig); +}); + +test("Get Env Config Static Method", () => { + const config = Configurator.getEnvConfig({ configPath: "./test/test-config.yaml", environmentId: "dev" }); const expectedDevConfig = { - account: "222222222222", - region: "us-east-1", - resources: { - "AWS::Lambda::Function": { - MemorySize: 128, - Runtime: "python3.8", + tags: { + "global:foo": "bar", + }, + environments: { + dev: { + account: "222222222222", + region: "us-east-1", + resources: { + "AWS::Lambda::Function": { + MemorySize: 128, + Runtime: "python3.8", + }, + }, + tags: { CostCenter: "2014" }, + }, + prod: { + account: "222222222222", + region: "us-east-1", + resources: { + "AWS::Lambda::Function": { + MemorySize: 1024, + Runtime: "python3.8", + }, + }, + tags: { CostCenter: "2015" }, }, }, - tags: { CostCenter: "2014" }, }; - assert(devConfig === expectedDevConfig); -}); - -test("Get Env Config Method: Non-Existent File", () => { - const devConfig = getEnvConfig({ config: "./test/not-real.yaml", environmentId: "dev" }); - const expectedDevConfig = {}; - assert(devConfig === expectedDevConfig); + assert(config === expectedDevConfig); });