Skip to content

Commit

Permalink
Refactor .loadBalancer properties to use getter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
bvtujo committed Feb 12, 2020
1 parent 9ba9d5c commit 57cd88a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
/**
* The Application Load Balancer for the service.
*/
public readonly loadBalancer: ApplicationLoadBalancer;
public get loadBalancer(): ApplicationLoadBalancer {
if (!this._applicationLoadBalancer) {
throw new Error('.loadBalancer can only be accessed if the class was constructed with an owned, not imported, load balancer');
}
return this._applicationLoadBalancer;
}

/**
* The listener for the service.
Expand All @@ -277,6 +282,8 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
*/
public readonly cluster: ICluster;

private readonly _applicationLoadBalancer?: ApplicationLoadBalancer;

/**
* Constructs a new instance of the ApplicationLoadBalancedServiceBase class.
*/
Expand All @@ -300,20 +307,20 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
internetFacing
};

this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer as ApplicationLoadBalancer
const loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer
: new ApplicationLoadBalancer(this, 'LB', lbProps);

if (props.certificate !== undefined && props.protocol !== undefined && props.protocol !== ApplicationProtocol.HTTPS) {
throw new Error('The HTTPS protocol must be used when a certificate is given');
}
const protocol = props.protocol !== undefined ? props.protocol :
(props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP);
(props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP);

const targetProps = {
port: 80
};

this.listener = this.loadBalancer.addListener('PublicListener', {
this.listener = loadBalancer.addListener('PublicListener', {
protocol,
port: props.listenerPort,
open: true
Expand All @@ -338,7 +345,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
this.listener.addCertificates('Arns', [ListenerCertificate.fromCertificateManager(this.certificate)]);
}

let domainName = this.loadBalancer.loadBalancerDnsName;
let domainName = loadBalancer.loadBalancerDnsName;
if (typeof props.domainName !== 'undefined') {
if (typeof props.domainZone === 'undefined') {
throw new Error('A Route53 hosted domain zone name is required to configure the specified domain name');
Expand All @@ -347,13 +354,17 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
const record = new ARecord(this, "DNS", {
zone: props.domainZone,
recordName: props.domainName,
target: RecordTarget.fromAlias(new LoadBalancerTarget(this.loadBalancer)),
target: RecordTarget.fromAlias(new LoadBalancerTarget(loadBalancer)),
});

domainName = record.domainName;
}

new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName });
if (loadBalancer instanceof ApplicationLoadBalancer) {
this._applicationLoadBalancer = loadBalancer;
}

new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: loadBalancer.loadBalancerDnsName });
new cdk.CfnOutput(this, 'ServiceURL', { value: protocol.toLowerCase() + '://' + domainName });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct {
/**
* The Network Load Balancer for the service.
*/
public readonly loadBalancer: NetworkLoadBalancer;
public get loadBalancer(): NetworkLoadBalancer {
if (!this._networkLoadBalancer) {
throw new Error(".loadBalancer can only be accessed if the class was constructed with an owned, not imported, load balancer");
}
return this._networkLoadBalancer;
}

/**
* The listener for the service.
Expand All @@ -247,6 +252,7 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct {
*/
public readonly cluster: ICluster;

private readonly _networkLoadBalancer?: NetworkLoadBalancer;
/**
* Constructs a new instance of the NetworkLoadBalancedServiceBase class.
*/
Expand All @@ -270,15 +276,16 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct {
internetFacing
};

this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer as NetworkLoadBalancer : new NetworkLoadBalancer(this, 'LB', lbProps);
const loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer :
new NetworkLoadBalancer(this, 'LB', lbProps);

const listenerPort = props.listenerPort !== undefined ? props.listenerPort : 80;

const targetProps = {
port: 80
};

this.listener = this.loadBalancer.addListener('PublicListener', { port: listenerPort });
this.listener = loadBalancer.addListener('PublicListener', { port: listenerPort });
this.targetGroup = this.listener.addTargets('ECS', targetProps);

if (typeof props.domainName !== 'undefined') {
Expand All @@ -289,10 +296,14 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct {
new ARecord(this, "DNS", {
zone: props.domainZone,
recordName: props.domainName,
target: RecordTarget.fromAlias(new LoadBalancerTarget(this.loadBalancer)),
target: RecordTarget.fromAlias(new LoadBalancerTarget(loadBalancer)),
});
}

if (loadBalancer instanceof NetworkLoadBalancer) {
this._networkLoadBalancer = loadBalancer;
}

if (props.loadBalancer === undefined) {
new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ export = {
cluster.addCapacity("Capacity", {instanceType: new ec2.InstanceType('t2.micro')});
const nlb = NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, "NLB", {
loadBalancerArn: nlbArn,
loadBalancerVpc: vpc,
vpc,
});
const taskDef = new ecs.Ec2TaskDefinition(stack, 'TaskDef');
const container = taskDef.addContainer('Container', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export = {
// WHEN
const nlb2 = NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack2, "ImportedNLB", {
loadBalancerArn: nlbArn,
loadBalancerVpc: vpc1,
vpc: vpc1,
});
const taskDef = new ecs.FargateTaskDefinition(stack2, 'TaskDef', {
cpu: 1024,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface NetworkLoadBalancerAttributes {
* @default - When not provided, listeners cannot be created on imported load
* balancers.
*/
readonly loadBalancerVpc?: ec2.IVpc;
readonly vpc?: ec2.IVpc;
}

/**
Expand All @@ -57,7 +57,7 @@ export class NetworkLoadBalancer extends BaseLoadBalancer implements INetworkLoa
public static fromNetworkLoadBalancerAttributes(scope: Construct, id: string, attrs: NetworkLoadBalancerAttributes): INetworkLoadBalancer {
class Import extends Resource implements INetworkLoadBalancer {
public readonly loadBalancerArn = attrs.loadBalancerArn;
public readonly vpc?: ec2.IVpc = attrs.loadBalancerVpc;
public readonly vpc?: ec2.IVpc = attrs.vpc;
public addListener(lid: string, props: BaseNetworkListenerProps): NetworkListener {
return new NetworkListener(this, lid, {
loadBalancer: this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export = {
const nlbArn = "arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer";
const nlb = elbv2.NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, 'NLB', {
loadBalancerArn: nlbArn,
loadBalancerVpc: vpc,
vpc,
});
// WHEN
const listener = nlb.addListener('Listener', {port: 80});
Expand Down

0 comments on commit 57cd88a

Please sign in to comment.