Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move getEnvConfig() to static method #270

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});