/
subnet.ts
234 lines (205 loc) · 7.99 KB
/
subnet.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as x from "..";
import * as utils from "../utils";
export class Subnet extends pulumi.ComponentResource {
// tslint:disable-next-line:variable-name
private readonly __isSubnetInstance = true;
public readonly vpc: x.ec2.Vpc;
public readonly subnetName: string;
/**
* Underlying id for the aws subnet. This should be used over [this.subnet.id] as this
* Output will only resolve once the route table and all associations are resolved.
*/
public readonly id: pulumi.Output<string>;
public readonly subnet: aws.ec2.Subnet;
public readonly routeTable: aws.ec2.RouteTable | undefined;
public readonly routeTableAssociation: aws.ec2.RouteTableAssociation | undefined;
public readonly routes: aws.ec2.Route[] = [];
constructor(name: string, vpc: x.ec2.Vpc, args: SubnetArgs, opts?: pulumi.ComponentResourceOptions);
constructor(name: string, vpc: x.ec2.Vpc, args: ExistingSubnetArgs, opts?: pulumi.ComponentResourceOptions);
constructor(name: string, vpc: x.ec2.Vpc, args: SubnetArgs | ExistingSubnetArgs, opts: pulumi.ComponentResourceOptions = {}) {
super("awsx:x:ec2:Subnet", name, {}, { parent: vpc, ...opts });
this.vpc = vpc;
this.subnetName = name;
if (isExistingSubnetArgs(args)) {
this.subnet = args.subnet;
this.id = args.subnet.id;
// TODO(cyrusn): We should be able to find the existing RouteTable and RouteTableAssociation
// when importing a subnet.
}
else {
// Allow the individual subnet to decide it if wants an ipv6 address assigned at
// creation. If not specified, assign by default if the Vpc has ipv6 assigned to
// it, don't assign otherwise.
const assignIpv6AddressOnCreation = utils.ifUndefined(args.assignIpv6AddressOnCreation, vpc.vpc.assignGeneratedIpv6CidrBlock);
this.subnet = new aws.ec2.Subnet(name, {
vpcId: vpc.id,
...args,
assignIpv6AddressOnCreation,
}, {
parent: this,
// See https://github.com/pulumi/pulumi-awsx/issues/398.
ignoreChanges: opts.ignoreChanges,
});
this.routeTable = new aws.ec2.RouteTable(name, {
vpcId: vpc.id,
}, { parent: this });
this.routeTableAssociation = new aws.ec2.RouteTableAssociation(name, {
routeTableId: this.routeTable.id,
subnetId: this.subnet.id,
}, { parent: this, customTimeouts: {
create: "5m",
delete: "5m",
} });
this.id = pulumi.all([this.subnet.id, this.routeTableAssociation.id])
.apply(([id]) => id);
}
this.registerOutputs();
}
/** @internal */
public static isSubnetInstance(obj: any): obj is Subnet {
return !!(<Subnet>obj).__isSubnetInstance;
}
public createRoute(name: string, args: RouteArgs, opts?: pulumi.ComponentResourceOptions): void;
public createRoute(name: string, provider: SubnetRouteProvider, opts?: pulumi.ComponentResourceOptions): void;
public createRoute(name: string, argsOrProvider: RouteArgs | SubnetRouteProvider, opts: pulumi.ComponentResourceOptions = {}): void {
if (!this.routeTable) {
throw new Error("Cannot call [createRoute] on a [Subnet] that doesn't have a [RouteTable]");
}
opts = {
parent: this,
customTimeouts: {
create: "5m",
delete: "5m",
},
...opts,
};
const args = isSubnetRouteProvider(argsOrProvider)
? argsOrProvider.route(name, { parent: this })
: argsOrProvider;
this.routes.push(new aws.ec2.Route(`${this.subnetName}-${name}`, {
...args,
routeTableId: this.routeTable.id,
}, opts));
}
}
utils.Capture(Subnet.prototype).createRoute.doNotCapture = true;
export interface SubnetRouteProvider {
route(name: string, opts: pulumi.ComponentResourceOptions): RouteArgs;
}
function isSubnetRouteProvider(obj: any): obj is SubnetRouteProvider {
return (<SubnetRouteProvider>obj).route instanceof Function;
}
export type SubnetOrId = Subnet | pulumi.Input<string>;
export interface ExistingSubnetArgs {
/**
* Optional existing instance to use to make the awsx Subnet out of. If this is provided No
* RouteTable or RouteTableAssociation will be automatically be created.
*/
subnet: aws.ec2.Subnet;
}
function isExistingSubnetArgs(obj: any): obj is ExistingSubnetArgs {
return !!(<ExistingSubnetArgs>obj).subnet;
}
type RouteArgsShape = utils.Overwrite<aws.ec2.RouteArgs, {
routeTableId?: never;
}>;
/**
* The set of arguments for constructing a Route resource.
*/
export interface RouteArgs {
/**
* The destination CIDR block.
*/
destinationCidrBlock?: pulumi.Input<string>;
/**
* The destination IPv6 CIDR block.
*/
destinationIpv6CidrBlock?: pulumi.Input<string>;
/**
* Identifier of a VPC Egress Only Internet Gateway.
*/
egressOnlyGatewayId?: pulumi.Input<string>;
/**
* Identifier of a VPC internet gateway or a virtual private gateway.
*/
gatewayId?: pulumi.Input<string>;
/**
* Identifier of an EC2 instance.
*/
instanceId?: pulumi.Input<string>;
/**
* Identifier of a VPC NAT gateway.
*/
natGatewayId?: pulumi.Input<string>;
/**
* Identifier of an EC2 network interface.
*/
networkInterfaceId?: pulumi.Input<string>;
/**
* Identifier of an EC2 Transit Gateway.
*/
transitGatewayId?: pulumi.Input<string>;
/**
* Identifier of a VPC peering connection.
*/
vpcPeeringConnectionId?: pulumi.Input<string>;
}
type SubnetArgsShape = utils.Overwrite<aws.ec2.SubnetArgs, {
vpcId?: never;
}>;
export interface SubnetArgs {
/**
* The CIDR block for the subnet.
*/
cidrBlock: pulumi.Input<string>;
/**
* Specify true to indicate that network interfaces created in the specified subnet should be
* assigned an IPv6 address. Default's to `true` if the Vpc this is associated with has
* `assignGeneratedIpv6CidrBlock: true`. `false` otherwise.
*/
assignIpv6AddressOnCreation?: pulumi.Input<boolean>;
/**
* The AZ for the subnet.
*/
availabilityZone?: pulumi.Input<string>;
/**
* The AZ ID of the subnet.
*/
availabilityZoneId?: pulumi.Input<string>;
/**
* The IPv6 network range for the subnet,
* in CIDR notation. The subnet size must use a /64 prefix length.
*/
ipv6CidrBlock?: pulumi.Input<string>;
/**
* Specify true to indicate that instances launched into the subnet should be assigned a public
* IP address. Default is `false`.
*/
mapPublicIpOnLaunch?: pulumi.Input<boolean>;
/**
* A mapping of tags to assign to the resource.
*/
tags?: pulumi.Input<aws.Tags>;
/**
* Ignore changes to any of the specified properties of the Subnet.
*/
ignoreChanges?: string[];
}
// Make sure our exported args shape is compatible with the overwrite shape we're trying to provide.
const test1: string = utils.checkCompat<SubnetArgsShape, SubnetArgs>();
const test2: string = utils.checkCompat<RouteArgsShape, RouteArgs>();