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

feat: getEnvironment() #307

Merged
merged 2 commits into from
Apr 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions API.md

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

33 changes: 33 additions & 0 deletions src/config/configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,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 });
Expand All @@ -149,6 +166,22 @@ export class Configurator {
? config.tags
: {};
}
public static getEnvironment(props: GetEnvironmentProps): any {
const config = getConfig({ config: props.configPath });
return config.environments && props.environmentId
? {
env: {
account: config.environments[props.environmentId].account,
region: config.environments[props.environmentId].region,
},
}
: {
env: {
account: config.account,
region: config.region,
},
};
}
public readonly config: any;
public readonly environmentId?: string;
constructor(scope: constructs.Construct, config: string | object, environmentId?: string) {
Expand Down
21 changes: 21 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SqsToLambdaStage,
getConfig,
getStackSynthesizer,
getEnvironment,
} from "../src";

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

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" });
Expand Down
2 changes: 2 additions & 0 deletions test/test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"tags": {
"global:foo": "bar"
},
"account": "111111111111",
"region": "us-east-1",
"environments": {
"dev": {
"account": "222222222222",
Expand Down