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

refactor: remove "export"s and normalize resource names #2580

Merged
merged 39 commits into from
May 18, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
elbv2
Elad Ben-Israel committed May 17, 2019
commit 8aee5e4bdbe510d7881a88719d529170b37a94a9
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { Construct, IResource, Resource, Token } from '@aws-cdk/cdk';
import { BaseListener } from '../shared/base-listener';
import { HealthCheck } from '../shared/base-target-group';
import { ApplicationProtocol, SslPolicy } from '../shared/enums';
@@ -74,13 +74,15 @@ export interface ApplicationListenerProps extends BaseApplicationListenerProps {

/**
* Define an ApplicationListener
*
* @resource AWS::ElasticLoadBalancingV2::Listener
*/
export class ApplicationListener extends BaseListener implements IApplicationListener {
/**
* Import an existing listener
*/
public static import(scope: cdk.Construct, id: string, props: ApplicationListenerImportProps): IApplicationListener {
return new ImportedApplicationListener(scope, id, props);
public static fromApplicationListenerAttributes(scope: Construct, id: string, attrs: ApplicationListenerAttributes): IApplicationListener {
return new ImportedApplicationListener(scope, id, attrs);
}

/**
@@ -103,17 +105,12 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
*/
private readonly protocol: ApplicationProtocol;

/**
* The default port on which this listener is listening
*/
private readonly defaultPort: number;

constructor(scope: cdk.Construct, id: string, props: ApplicationListenerProps) {
constructor(scope: Construct, id: string, props: ApplicationListenerProps) {
const [protocol, port] = determineProtocolAndPort(props.protocol, props.port);

super(scope, id, {
loadBalancerArn: props.loadBalancer.loadBalancerArn,
certificates: new cdk.Token(() => this.certificateArns.map(certificateArn => ({ certificateArn }))),
certificates: new Token(() => this.certificateArns.map(certificateArn => ({ certificateArn }))),
protocol,
port,
sslPolicy: props.sslPolicy,
@@ -123,7 +120,6 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
this.protocol = protocol;
this.certificateArns = [];
this.certificateArns.push(...(props.certificateArns || []));
this.defaultPort = port;

// This listener edits the securitygroup of the load balancer,
// but adds its own default port.
@@ -251,17 +247,6 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
this.connections.allowTo(connectable, portRange, 'Load balancer to target');
}

/**
* Export this listener
*/
public export(): ApplicationListenerImportProps {
return {
listenerArn: new cdk.CfnOutput(this, 'ListenerArn', { value: this.listenerArn }).makeImportValue().toString(),
securityGroupId: this.connections.securityGroups[0]!.export().securityGroupId,
defaultPort: new cdk.CfnOutput(this, 'Port', { value: this.defaultPort }).makeImportValue().toString(),
};
}

/**
* Validate this listener.
*/
@@ -285,9 +270,10 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
/**
* Properties to reference an existing listener
*/
export interface IApplicationListener extends cdk.IConstruct, ec2.IConnectable {
export interface IApplicationListener extends IResource, ec2.IConnectable {
/**
* ARN of the listener
* @attribute
*/
readonly listenerArn: string;

@@ -323,17 +309,12 @@ export interface IApplicationListener extends cdk.IConstruct, ec2.IConnectable {
* Don't call this directly. It is called by ApplicationTargetGroup.
*/
registerConnectable(connectable: ec2.IConnectable, portRange: ec2.IPortRange): void;

/**
* Export this listener
*/
export(): ApplicationListenerImportProps;
}

/**
* Properties to reference an existing listener
*/
export interface ApplicationListenerImportProps {
export interface ApplicationListenerAttributes {
/**
* ARN of the listener
*/
@@ -347,34 +328,30 @@ export interface ApplicationListenerImportProps {
/**
* The default port on which this listener is listening
*/
readonly defaultPort?: string;
readonly defaultPort?: number;
}

class ImportedApplicationListener extends cdk.Construct implements IApplicationListener {
class ImportedApplicationListener extends Resource implements IApplicationListener {
public readonly connections: ec2.Connections;

/**
* ARN of the listener
*/
public readonly listenerArn: string;

constructor(scope: cdk.Construct, id: string, private readonly props: ApplicationListenerImportProps) {
constructor(scope: Construct, id: string, props: ApplicationListenerAttributes) {
super(scope, id);

this.listenerArn = props.listenerArn;

const defaultPortRange = props.defaultPort !== undefined ? new ec2.TcpPortFromAttribute(props.defaultPort) : undefined;
const defaultPortRange = props.defaultPort !== undefined ? new ec2.TcpPort(props.defaultPort) : undefined;

this.connections = new ec2.Connections({
securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(this, 'SecurityGroup', props.securityGroupId)],
defaultPortRange,
});
}

public export() {
return this.props;
}

/**
* Add one or more certificates to this listener.
*/
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { Construct, IResource, Token } from '@aws-cdk/cdk';
import { BaseLoadBalancer, BaseLoadBalancerProps } from '../shared/base-load-balancer';
import { IpAddressType } from '../shared/enums';
import { ApplicationListener, BaseApplicationListenerProps } from './application-listener';
@@ -44,22 +44,26 @@ export interface ApplicationLoadBalancerProps extends BaseLoadBalancerProps {

/**
* Define an Application Load Balancer
*
* @resource AWS::ElasticLoadBalancingV2::LoadBalancer
*/
export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplicationLoadBalancer {
/**
* Import an existing Application Load Balancer
*/
public static import(scope: cdk.Construct, id: string, props: ApplicationLoadBalancerImportProps): IApplicationLoadBalancer {
return new ImportedApplicationLoadBalancer(scope, id, props);
public static fromApplicationLoadBalancerAttributes(
scope: Construct, id: string, attrs: ApplicationLoadBalancerAttributes): IApplicationLoadBalancer {

return new ImportedApplicationLoadBalancer(scope, id, attrs);
}

public readonly connections: ec2.Connections;
private readonly securityGroup: ec2.ISecurityGroup;

constructor(scope: cdk.Construct, id: string, props: ApplicationLoadBalancerProps) {
constructor(scope: Construct, id: string, props: ApplicationLoadBalancerProps) {
super(scope, id, props, {
type: "application",
securityGroups: new cdk.Token(() => [this.securityGroup.securityGroupId]),
securityGroups: new Token(() => [this.securityGroup.securityGroupId]),
ipAddressType: props.ipAddressType,
});

@@ -105,16 +109,6 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
});
}

/**
* Export this load balancer
*/
public export(): ApplicationLoadBalancerImportProps {
return {
loadBalancerArn: new cdk.CfnOutput(this, 'LoadBalancerArn', { value: this.loadBalancerArn }).makeImportValue().toString(),
securityGroupId: this.securityGroup.export().securityGroupId,
};
}

/**
* Return the given named metric for this Application Load Balancer
*
@@ -124,7 +118,7 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
return new cloudwatch.Metric({
namespace: 'AWS/ApplicationELB',
metricName,
dimensions: { LoadBalancer: this.fullName },
dimensions: { LoadBalancer: this.loadBalancerFullName },
...props
});
}
@@ -474,7 +468,7 @@ export enum HttpCodeTarget {
/**
* An application load balancer
*/
export interface IApplicationLoadBalancer extends cdk.IConstruct, ec2.IConnectable {
export interface IApplicationLoadBalancer extends IResource, ec2.IConnectable {
/**
* The ARN of this load balancer
*/
@@ -483,23 +477,18 @@ export interface IApplicationLoadBalancer extends cdk.IConstruct, ec2.IConnectab
/**
* The VPC this load balancer has been created in (if available)
*/
readonly vpc?: ec2.IVpcNetwork;
readonly vpc?: ec2.IVpc;

/**
* Add a new listener to this load balancer
*/
addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener;

/**
* Export this load balancer
*/
export(): ApplicationLoadBalancerImportProps;
}

/**
* Properties to reference an existing load balancer
*/
export interface ApplicationLoadBalancerImportProps {
export interface ApplicationLoadBalancerAttributes {
/**
* ARN of the load balancer
*/
@@ -537,7 +526,7 @@ const ELBV2_ACCOUNTS: {[region: string]: string } = {
/**
* An ApplicationLoadBalancer that has been defined elsewhere
*/
class ImportedApplicationLoadBalancer extends cdk.Construct implements IApplicationLoadBalancer {
class ImportedApplicationLoadBalancer extends Construct implements IApplicationLoadBalancer {
/**
* Manage connections for this load balancer
*/
@@ -553,9 +542,9 @@ class ImportedApplicationLoadBalancer extends cdk.Construct implements IApplicat
*
* Always undefined.
*/
public readonly vpc?: ec2.IVpcNetwork;
public readonly vpc?: ec2.IVpc;

constructor(scope: cdk.Construct, id: string, private readonly props: ApplicationLoadBalancerImportProps) {
constructor(scope: Construct, id: string, props: ApplicationLoadBalancerAttributes) {
super(scope, id);

this.loadBalancerArn = props.loadBalancerArn;
@@ -564,10 +553,6 @@ class ImportedApplicationLoadBalancer extends cdk.Construct implements IApplicat
});
}

public export() {
return this.props;
}

public addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener {
return new ApplicationListener(this, id, {
loadBalancer: this,
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { Construct, IConstruct } from '@aws-cdk/cdk';
import { BaseTargetGroupProps, ITargetGroup, loadBalancerNameFromListenerArn, LoadBalancerTargetProps,
TargetGroupBase, TargetGroupImportProps } from '../shared/base-target-group';
import { ApplicationProtocol } from '../shared/enums';
@@ -66,14 +66,14 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
/**
* Import an existing target group
*/
public static import(scope: cdk.Construct, id: string, props: TargetGroupImportProps): IApplicationTargetGroup {
public static import(scope: Construct, id: string, props: TargetGroupImportProps): IApplicationTargetGroup {
return new ImportedApplicationTargetGroup(scope, id, props);
}

private readonly connectableMembers: ConnectableMember[];
private readonly listeners: IApplicationListener[];

constructor(scope: cdk.Construct, id: string, props: ApplicationTargetGroupProps) {
constructor(scope: Construct, id: string, props: ApplicationTargetGroupProps) {
const [protocol, port] = determineProtocolAndPort(props.protocol, props.port);

super(scope, id, props, {
@@ -119,13 +119,7 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
* Don't call this directly. It will be called by load balancing targets.
*/
public registerConnectable(connectable: ec2.IConnectable, portRange?: ec2.IPortRange) {
if (portRange === undefined) {
if (cdk.Token.isToken(this.defaultPort)) {
portRange = new ec2.TcpPortFromAttribute(this.defaultPort);
} else {
portRange = new ec2.TcpPort(parseInt(this.defaultPort, 10));
}
}
portRange = portRange || new ec2.TcpPort(this.defaultPort);

// Notify all listeners that we already know about of this new connectable.
// Then remember for new listeners that might get added later.
@@ -140,7 +134,7 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
*
* Don't call this directly. It will be called by listeners.
*/
public registerListener(listener: IApplicationListener, associatingConstruct?: cdk.IConstruct) {
public registerListener(listener: IApplicationListener, associatingConstruct?: IConstruct) {
// Notify this listener of all connectables that we know about.
// Then remember for new connectables that might get added later.
for (const member of this.connectableMembers) {
@@ -324,14 +318,14 @@ export interface IApplicationTargetGroup extends ITargetGroup {
*
* Don't call this directly. It will be called by listeners.
*/
registerListener(listener: IApplicationListener, associatingConstruct?: cdk.IConstruct): void;
registerListener(listener: IApplicationListener, associatingConstruct?: IConstruct): void;
}

/**
* An imported application target group
*/
class ImportedApplicationTargetGroup extends ImportedTargetGroupBase implements IApplicationTargetGroup {
public registerListener(_listener: IApplicationListener, _associatingConstruct?: cdk.IConstruct) {
public registerListener(_listener: IApplicationListener, _associatingConstruct?: IConstruct) {
// Nothing to do, we know nothing of our members
}
}
Loading