-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathvpc-link.ts
107 lines (91 loc) · 2.68 KB
/
vpc-link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import { IResource, Lazy, Resource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnVpcLink } from './apigateway.generated';
/**
* Represents an API Gateway VpcLink
*/
export interface IVpcLink extends IResource {
/**
* Physical ID of the VpcLink resource
* @attribute
*/
readonly vpcLinkId: string;
}
/**
* Properties for a VpcLink
*/
export interface VpcLinkProps {
/**
* The name used to label and identify the VPC link.
* @default - automatically generated name
*/
readonly vpcLinkName?: string;
/**
* The description of the VPC link.
* @default no description
*/
readonly description?: string;
/**
* The network load balancers of the VPC targeted by the VPC link.
* The network load balancers must be owned by the same AWS account of the API owner.
*
* @default - no targets. Use `addTargets` to add targets
*/
readonly targets?: elbv2.INetworkLoadBalancer[];
}
/**
* Define a new VPC Link
* Specifies an API Gateway VPC link for a RestApi to access resources in an Amazon Virtual Private Cloud (VPC).
*/
export class VpcLink extends Resource implements IVpcLink {
/**
* Import a VPC Link by its Id
*/
public static fromVpcLinkId(scope: Construct, id: string, vpcLinkId: string): IVpcLink {
class Import extends Resource implements IVpcLink {
public vpcLinkId = vpcLinkId;
}
return new Import(scope, id);
}
/**
* Physical ID of the VpcLink resource
* @attribute
*/
public readonly vpcLinkId: string;
private readonly _targets = new Array<elbv2.INetworkLoadBalancer>();
constructor(scope: Construct, id: string, props: VpcLinkProps = {}) {
super(scope, id, {
physicalName: props.vpcLinkName ||
Lazy.string({ produce: () => this.node.uniqueId }),
});
const cfnResource = new CfnVpcLink(this, 'Resource', {
name: this.physicalName,
description: props.description,
targetArns: Lazy.list({ produce: () => this.renderTargets() }),
});
this.vpcLinkId = cfnResource.ref;
if (props.targets) {
this.addTargets(...props.targets);
}
}
public addTargets(...targets: elbv2.INetworkLoadBalancer[]) {
this._targets.push(...targets);
}
/**
* Return the list of DNS names from the target NLBs.
* @internal
* */
public get _targetDnsNames(): string[] {
return this._targets.map(t => t.loadBalancerDnsName);
}
protected validate(): string[] {
if (this._targets.length === 0) {
return ['No targets added to vpc link'];
}
return [];
}
private renderTargets() {
return this._targets.map(nlb => nlb.loadBalancerArn);
}
}