Skip to content

Commit

Permalink
Refactor for consistency with #92
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwinch committed Feb 17, 2021
1 parent 76237c3 commit 599b2bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/constructs/autoscaling/asg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AutoScalingGroup } from "@aws-cdk/aws-autoscaling";
import type { ISecurityGroup, MachineImage, MachineImageConfig } from "@aws-cdk/aws-ec2";
import { InstanceType, OperatingSystemType, UserData } from "@aws-cdk/aws-ec2";
import type { ApplicationTargetGroup } from "@aws-cdk/aws-elasticloadbalancingv2";
import type { GuStack, GuStageVariable } from "../core";
import type { GuStack, GuStageDependentValue } from "../core";
import { GuAmiParameter, GuInstanceTypeParameter } from "../core";

// Since we want to override the types of what gets passed in for the below props,
Expand Down Expand Up @@ -55,24 +55,24 @@ interface AwsAsgCapacityProps {
maxCapacity: number;
}

function wireCapacityProps(scope: GuStack, capacity: GuAsgCapacityProps): AwsAsgCapacityProps {
function wireCapacityProps(stack: GuStack, capacity: GuAsgCapacityProps): AwsAsgCapacityProps {
const minInstancesKey = "minInstances";
const maxInstancesKey = "maxInstances";
const minInstances: GuStageVariable = {
const minInstances: GuStageDependentValue = {
variableName: minInstancesKey,
codeValue: capacity.minimumCodeInstances,
prodValue: capacity.minimumProdInstances,
};
const maxInstances: GuStageVariable = {
const maxInstances: GuStageDependentValue = {
variableName: maxInstancesKey,
codeValue: capacity.maximumCodeInstances ?? capacity.minimumCodeInstances * 2,
prodValue: capacity.maximumProdInstances ?? capacity.minimumProdInstances * 2,
};
scope.mappings.addStageVariable(minInstances);
scope.mappings.addStageVariable(maxInstances);
stack.setStageDependentValue(minInstances);
stack.setStageDependentValue(maxInstances);
return {
minCapacity: (scope.mappings.findInMap(scope.stage, minInstancesKey) as unknown) as number,
maxCapacity: (scope.mappings.findInMap(scope.stage, maxInstancesKey) as unknown) as number,
minCapacity: stack.getStageDependentValue(minInstancesKey),
maxCapacity: stack.getStageDependentValue(maxInstancesKey),
};
}

Expand Down
6 changes: 1 addition & 5 deletions src/constructs/core/mappings.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { CfnMapping } from "@aws-cdk/core";
import type { GuStack } from "./stack";

export interface GuStageVariable {
export interface GuStageDependentValue {
variableName: string;
codeValue: unknown;
prodValue: unknown;
}

export class GuStageMapping extends CfnMapping {
addStageVariable(stageVariable: GuStageVariable): void {
this.setValue("CODE", stageVariable.variableName, stageVariable.codeValue);
this.setValue("PROD", stageVariable.variableName, stageVariable.prodValue);
}
constructor(scope: GuStack, id: string = "stage-mapping") {
super(scope, id);
}
Expand Down
10 changes: 10 additions & 0 deletions src/constructs/core/stack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { App, StackProps } from "@aws-cdk/core";
import { Stack, Tags } from "@aws-cdk/core";
import { TrackingTag } from "../../constants/library-info";
import type { GuStageDependentValue } from "./mappings";
import { GuStageMapping } from "./mappings";
import { GuStackParameter, GuStageParameter } from "./parameters";

Expand Down Expand Up @@ -63,6 +64,15 @@ export class GuStack extends Stack {
return this._mappings ?? (this._mappings = new GuStageMapping(this));
}

setStageDependentValue(stageVariable: GuStageDependentValue): void {
this.mappings.setValue("CODE", stageVariable.variableName, stageVariable.codeValue);
this.mappings.setValue("PROD", stageVariable.variableName, stageVariable.prodValue);
}

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

/**
* A helper function to add a tag to all resources in a stack.
*
Expand Down

0 comments on commit 599b2bf

Please sign in to comment.