Skip to content

Commit

Permalink
refactor!: Improve Mappings helper function(s) (#387)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `setStageDependentValue` and `getStageDependentValue` helper functions have been removed. In order to reduce boilerplate, the same functionality is now provided via a single function (`withStageDependentValue`).
  • Loading branch information
jacobwinch authored Apr 7, 2021
1 parent 25d9da5 commit e001123
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
36 changes: 15 additions & 21 deletions src/constructs/autoscaling/asg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ISecurityGroup, MachineImage, MachineImageConfig } from "@aws-cdk/
import { InstanceType, OperatingSystemType, UserData } from "@aws-cdk/aws-ec2";
import type { ApplicationTargetGroup } from "@aws-cdk/aws-elasticloadbalancingv2";
import { Stage } from "../../constants";
import type { GuStack, GuStageDependentValue } from "../core";
import type { GuStack } from "../core";
import { GuAmiParameter, GuInstanceTypeParameter } from "../core";
import type { AppIdentity } from "../core/identity";
import { GuHttpsEgressSecurityGroup } from "../ec2";
Expand Down Expand Up @@ -59,27 +59,21 @@ interface AwsAsgCapacityProps {
}

function wireStageDependentProps(stack: GuStack, stageDependentProps: GuStageDependentAsgProps): AwsAsgCapacityProps {
const minInstancesKey = "minInstances";
const maxInstancesKey = "maxInstances";
const minInstances: GuStageDependentValue<number> = {
variableName: minInstancesKey,
stageValues: {
[Stage.CODE]: stageDependentProps.CODE.minimumInstances,
[Stage.PROD]: stageDependentProps.PROD.minimumInstances,
},
};
const maxInstances: GuStageDependentValue<number> = {
variableName: maxInstancesKey,
stageValues: {
[Stage.CODE]: stageDependentProps.CODE.maximumInstances ?? stageDependentProps.CODE.minimumInstances * 2,
[Stage.PROD]: stageDependentProps.PROD.maximumInstances ?? stageDependentProps.PROD.minimumInstances * 2,
},
};
stack.setStageDependentValue(minInstances);
stack.setStageDependentValue(maxInstances);
return {
minCapacity: stack.getStageDependentValue(minInstancesKey),
maxCapacity: stack.getStageDependentValue(maxInstancesKey),
minCapacity: stack.withStageDependentValue({
variableName: "minInstances",
stageValues: {
[Stage.CODE]: stageDependentProps.CODE.minimumInstances,
[Stage.PROD]: stageDependentProps.PROD.minimumInstances,
},
}),
maxCapacity: stack.withStageDependentValue({
variableName: "maxInstances",
stageValues: {
[Stage.CODE]: stageDependentProps.CODE.maximumInstances ?? stageDependentProps.CODE.minimumInstances * 2,
[Stage.PROD]: stageDependentProps.PROD.maximumInstances ?? stageDependentProps.PROD.minimumInstances * 2,
},
}),
};
}

Expand Down
15 changes: 10 additions & 5 deletions src/constructs/core/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ export class GuStack extends Stack implements StackStageIdentity {
return this._mappings ?? (this._mappings = new GuStageMapping(this));
}

setStageDependentValue<T extends string | number | boolean>(stageDependentValue: GuStageDependentValue<T>): void {
/**
* A helper function to switch between different values depending on the Stage being CloudFormed (e.g.
* use 1 in `CODE` or 3 in `PROD`).
*
* Note: Standard conditional logic which references a CloudFormation Parameter's value will not work
* as Parameters are not resolved at synth-time. This helper function creates CloudFormation
* Mappings to work around this limitation.
*/
withStageDependentValue<T extends string | number | boolean>(stageDependentValue: GuStageDependentValue<T>): T {
this.mappings.setValue(Stage.CODE, stageDependentValue.variableName, stageDependentValue.stageValues.CODE);
this.mappings.setValue(Stage.PROD, stageDependentValue.variableName, stageDependentValue.stageValues.PROD);
}

getStageDependentValue<T>(key: string): T {
return (this.mappings.findInMap(this.stage, key) as unknown) as T;
return (this.mappings.findInMap(this.stage, stageDependentValue.variableName) as unknown) as T;
}

/**
Expand Down

0 comments on commit e001123

Please sign in to comment.