Skip to content

Commit

Permalink
add new GuHttpsApplicationListner construct
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Lynch committed Jan 28, 2021
1 parent ebc619a commit b2146d7
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
105 changes: 104 additions & 1 deletion src/constructs/loadbalancing/elb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { ApplicationProtocol, ListenerAction } from "@aws-cdk/aws-elasticloadbal
import { Stack } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../../test/utils/simple-gu-stack";
import type { SynthedStack } from "../../../test/utils/synthed-stack";
import { GuApplicationListener, GuApplicationLoadBalancer, GuApplicationTargetGroup } from "../loadbalancing";
import {
GuApplicationListener,
GuApplicationLoadBalancer,
GuApplicationTargetGroup,
GuHttpsApplicationListener,
} from "../loadbalancing";

describe("The GuApplicationLoadBalancer class", () => {
const vpc = Vpc.fromVpcAttributes(new Stack(), "VPC", {
Expand Down Expand Up @@ -274,3 +279,101 @@ describe("The GuApplicationListener class", () => {
});
});
});

describe("The GuHttpsApplicationListener class", () => {
const vpc = Vpc.fromVpcAttributes(new Stack(), "VPC", {
vpcId: "test",
availabilityZones: [""],
publicSubnetIds: [""],
});

test("sets default props", () => {
const stack = simpleGuStackForTesting();

const loadBalancer = new GuApplicationLoadBalancer(stack, "ApplicationLoadBalancer", { vpc });
const targetGroup = new GuApplicationTargetGroup(stack, "GrafanaInternalTargetGroup", {
vpc: vpc,
protocol: ApplicationProtocol.HTTP,
});

new GuHttpsApplicationListener(stack, "ApplicationListener", {
loadBalancer,
targetGroup,
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancingV2::Listener", {
Port: 443,
Protocol: "HTTPS",
DefaultActions: [
{
TargetGroupArn: {
Ref: "GrafanaInternalTargetGroup837A1034",
},
Type: "forward",
},
],
});
});

test("creates certificate prop if no value passed in", () => {
const stack = simpleGuStackForTesting();

const loadBalancer = new GuApplicationLoadBalancer(stack, "ApplicationLoadBalancer", { vpc });
const targetGroup = new GuApplicationTargetGroup(stack, "GrafanaInternalTargetGroup", {
vpc: vpc,
protocol: ApplicationProtocol.HTTP,
});

new GuHttpsApplicationListener(stack, "ApplicationListener", {
loadBalancer,
targetGroup,
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.CertificateARN).toEqual({
Type: "String",
AllowedPattern: "arn:aws:[a-z0-9]*:[a-z0-9\\-]*:[0-9]{12}:.*",
ConstraintDescription: "Must be a valid ARN, eg: arn:partition:service:region:account-id:resource-id",
Description: "Certificate ARN for ApplicationListener",
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancingV2::Listener", {
Certificates: [
{
CertificateArn: {
Ref: "CertificateARN",
},
},
],
});
});

test("does not create certificate prop if a value passed in", () => {
const stack = simpleGuStackForTesting();

const loadBalancer = new GuApplicationLoadBalancer(stack, "ApplicationLoadBalancer", { vpc });
const targetGroup = new GuApplicationTargetGroup(stack, "GrafanaInternalTargetGroup", {
vpc: vpc,
protocol: ApplicationProtocol.HTTP,
});

new GuHttpsApplicationListener(stack, "ApplicationListener", {
loadBalancer,
targetGroup,
certificate: "test",
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Parameters)).not.toContain("CertificateARN");

expect(stack).toHaveResource("AWS::ElasticLoadBalancingV2::Listener", {
Certificates: [
{
CertificateArn: "test",
},
],
});
});
});
29 changes: 29 additions & 0 deletions src/constructs/loadbalancing/elb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
ApplicationLoadBalancer,
ApplicationProtocol,
ApplicationTargetGroup,
ListenerAction,
Protocol,
} from "@aws-cdk/aws-elasticloadbalancingv2";
import { Duration } from "@aws-cdk/core";
import type { GuStack } from "../core";
import { GuArnParameter } from "../core";

interface GuApplicationLoadBalancerProps extends ApplicationLoadBalancerProps {
overrideId?: boolean;
Expand Down Expand Up @@ -73,3 +75,30 @@ export class GuApplicationListener extends ApplicationListener {
(this.node.defaultChild as CfnListener).overrideLogicalId(id);
}
}

export interface GuHttpsApplicationListenerProps
extends Omit<GuApplicationListenerProps, "defaultAction" | "certificates"> {
targetGroup: GuApplicationTargetGroup;
certificate?: string;
}

export class GuHttpsApplicationListener extends ApplicationListener {
constructor(scope: GuStack, id: string, props: GuHttpsApplicationListenerProps) {
const mergedProps: GuApplicationListenerProps = {
port: 443,
protocol: ApplicationProtocol.HTTPS,
...props,
certificates: [
{
certificateArn:
props.certificate ??
new GuArnParameter(scope, "CertificateARN", { description: "Certificate ARN for ApplicationListener" })
.valueAsString,
},
],
defaultAction: ListenerAction.forward([props.targetGroup]),
};

super(scope, id, mergedProps);
}
}

0 comments on commit b2146d7

Please sign in to comment.