Skip to content

Commit

Permalink
fix: move getEnvConfig() to static method (#270)
Browse files Browse the repository at this point in the history
* Moving 'getEnvConfig' to static method

* trigger pipeline
  • Loading branch information
malachi-constant committed Mar 3, 2023
1 parent 77fdf71 commit 06d19e1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 36 deletions.
68 changes: 64 additions & 4 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion CONTRIBUTING.md
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions src/cicd/pipelines.ts
Expand Up @@ -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, {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 =
Expand Down
19 changes: 9 additions & 10 deletions src/config/configurator.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
58 changes: 41 additions & 17 deletions test/config.test.ts
Expand Up @@ -13,7 +13,6 @@ import {
FirehoseToS3Stage,
SqsToLambdaStage,
getStackSynthesizer,
getEnvConfig,
} from "../src";

test("Config Simple Override", () => {
Expand Down Expand Up @@ -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);
});

0 comments on commit 06d19e1

Please sign in to comment.