Repository: awslabs/aws-ddk
Pull request #270 (2023-03-02 23:20:01)

fix: move `getEnvConfig()` to static method
We now have two ways of retrieving data from config.

1. Class method

```javascript
// Instantiate Config
const config = new Configurator({ configPath: "./ddk.yaml", environmentId: "dev" });

// Retrive Values from Object
config.getConfigAttribute("foo")
```

2. `static` method -- no object state 
```javascript
// Retrieve data directly from file
const config = Configurator.getEnvConfig({ configPath: "./test/test-config.yaml", environmentId: "dev" });
```

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


malachi-constant: 
### AWS CodeBuild CI Report

* CodeBuild project: DDKIntegrationTestsD8472609-5SCHWjldmWcf
* Commit ID: b96c6d333fb01cd029aef7d07a248fd45d23d663
* Result: SUCCEEDED
* [Build Logs](https://9hm1y4sor9.execute-api.us-east-1.amazonaws.com/Prod/buildlogs?key=5fca0cbc-c9e4-4131-9f7d-6c6c7fc01d84%2Fbuild.log) (available for 30 days)

*Powered by [github-codebuild-logs](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:277187709615:applications~github-codebuild-logs), available on the [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/)*


<!--
CREATED BY GITHUB-CODEBUILD-LOGS
-->



Patch:
diff --git a/API.md b/API.md
index b54e2fe..abe3f9d 100644
--- a/API.md
+++ b/API.md
@@ -6436,6 +6436,45 @@ public readonly s3BucketProps: BucketProps;
 
 ---
 
+### GetEnvConfigProps <a name="GetEnvConfigProps" id="aws-ddk-core.GetEnvConfigProps"></a>
+
+#### Initializer <a name="Initializer" id="aws-ddk-core.GetEnvConfigProps.Initializer"></a>
+
+```typescript
+import { GetEnvConfigProps } from 'aws-ddk-core'
+
+const getEnvConfigProps: GetEnvConfigProps = { ... }
+```
+
+#### Properties <a name="Properties" id="Properties"></a>
+
+| **Name** | **Type** | **Description** |
+| --- | --- | --- |
+| <code><a href="#aws-ddk-core.GetEnvConfigProps.property.configPath">configPath</a></code> | <code>string</code> | *No description.* |
+| <code><a href="#aws-ddk-core.GetEnvConfigProps.property.environmentId">environmentId</a></code> | <code>string</code> | *No description.* |
+
+---
+
+##### `configPath`<sup>Required</sup> <a name="configPath" id="aws-ddk-core.GetEnvConfigProps.property.configPath"></a>
+
+```typescript
+public readonly configPath: string;
+```
+
+- *Type:* string
+
+---
+
+##### `environmentId`<sup>Required</sup> <a name="environmentId" id="aws-ddk-core.GetEnvConfigProps.property.environmentId"></a>
+
+```typescript
+public readonly environmentId: string;
+```
+
+- *Type:* string
+
+---
+
 ### GetSynthActionProps <a name="GetSynthActionProps" id="aws-ddk-core.GetSynthActionProps"></a>
 
 #### Initializer <a name="Initializer" id="aws-ddk-core.GetSynthActionProps.Initializer"></a>
@@ -8465,18 +8504,18 @@ new Configurator(scope: Construct, config: string | object, environmentId?: stri
 
 | **Name** | **Description** |
 | --- | --- |
-| <code><a href="#aws-ddk-core.Configurator.getEnvConfig">getEnvConfig</a></code> | *No description.* |
+| <code><a href="#aws-ddk-core.Configurator.getConfigAttribute">getConfigAttribute</a></code> | *No description.* |
 | <code><a href="#aws-ddk-core.Configurator.tagConstruct">tagConstruct</a></code> | *No description.* |
 
 ---
 
-##### `getEnvConfig` <a name="getEnvConfig" id="aws-ddk-core.Configurator.getEnvConfig"></a>
+##### `getConfigAttribute` <a name="getConfigAttribute" id="aws-ddk-core.Configurator.getConfigAttribute"></a>
 
 ```typescript
-public getEnvConfig(attribute: string): any
+public getConfigAttribute(attribute: string): any
 ```
 
-###### `attribute`<sup>Required</sup> <a name="attribute" id="aws-ddk-core.Configurator.getEnvConfig.parameter.attribute"></a>
+###### `attribute`<sup>Required</sup> <a name="attribute" id="aws-ddk-core.Configurator.getConfigAttribute.parameter.attribute"></a>
 
 - *Type:* string
 
@@ -8500,6 +8539,27 @@ public tagConstruct(scope: Construct, tags: {[ key: string ]: string}): void
 
 ---
 
+#### Static Functions <a name="Static Functions" id="Static Functions"></a>
+
+| **Name** | **Description** |
+| --- | --- |
+| <code><a href="#aws-ddk-core.Configurator.getEnvConfig">getEnvConfig</a></code> | *No description.* |
+
+---
+
+##### `getEnvConfig` <a name="getEnvConfig" id="aws-ddk-core.Configurator.getEnvConfig"></a>
+
+```typescript
+import { Configurator } from 'aws-ddk-core'
+
+Configurator.getEnvConfig(props: GetEnvConfigProps)
+```
+
+###### `props`<sup>Required</sup> <a name="props" id="aws-ddk-core.Configurator.getEnvConfig.parameter.props"></a>
+
+- *Type:* <a href="#aws-ddk-core.GetEnvConfigProps">GetEnvConfigProps</a>
+
+---
 
 #### Properties <a name="Properties" id="Properties"></a>
 
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 98fc4fb..f1b8fdb 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 d894b96..8f21218 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 a8edb68..b51f01a 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;


Test patch:
diff --git a/test/config.test.ts b/test/config.test.ts
index 302ca2a..95344b4 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);
 });