-
Notifications
You must be signed in to change notification settings - Fork 881
/
vdc.py
348 lines (319 loc) · 10.7 KB
/
vdc.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from pulumi import ResourceOptions
from pulumi.resource import CustomTimeouts
import pulumi_azure_native.resources as resources
import pulumi_azure_native.network as network
# Variables that may need to be injected before calling functions:
# vdc.location = props.location
# vdc.resource_group_name = props.resource_group_name
# vdc.s = props.separator
# vdc.self = self
# vdc.suffix = props.suffix
# vdc.tags = props.tags
def bastion_host(stem, virtual_network_name, address_prefix, depends_on=None):
ab_sn = network.Subnet(
'AzureBastionSubnet',
resource_group_name=resource_group_name,
virtual_network_name=virtual_network_name,
address_prefix=address_prefix,
opts=ResourceOptions(
parent=self,
delete_before_replace=True,
depends_on=depends_on,
),
)
ab_pip = network.PublicIPAddress(
f'{stem}{s}ab{s}pip{s}{suffix}',
resource_group_name=resource_group_name,
sku=network.PublicIPAddressSkuArgs(
name=network.PublicIPAddressSkuName.STANDARD,
),
public_ip_allocation_method=network.IPAllocationMethod.STATIC,
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
ab = network.BastionHost(
f'{stem}{s}ab{s}{suffix}',
resource_group_name=resource_group_name,
ip_configurations=[network.BastionHostIPConfigurationArgs(
name=f'{stem}{s}ab{s}ipc',
public_ip_address=network.PublicIPAddressArgs(
id=ab_pip.id,
),
subnet=network.SubnetArgs(
id=ab_sn.id,
),
)],
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
return ab
def expressroute_gateway(stem, subnet_id, depends_on=None):
er_gw_pip = network.PublicIPAddress(
f'{stem}{s}er{s}gw{s}pip{s}{suffix}',
resource_group_name=resource_group_name,
public_ip_allocation_method=network.IPAllocationMethod.DYNAMIC,
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
er_gw = network.VirtualNetworkGateway(
f'{stem}{s}er{s}gw{s}{suffix}',
resource_group_name=resource_group_name,
sku=network.VirtualNetworkGatewaySkuArgs(
name=network.VirtualNetworkGatewaySkuName.STANDARD,
tier=network.VirtualNetworkGatewaySkuTier.STANDARD,
),
gateway_type=network.VirtualNetworkGatewayType.EXPRESS_ROUTE,
vpn_type=network.VpnType.ROUTE_BASED,
enable_bgp=True,
ip_configurations=[network.VirtualNetworkGatewayIPConfigurationArgs(
name=f'{stem}{s}er{s}gw{s}ipc',
public_ip_address=network.PublicIPAddressArgs(
id=er_gw_pip.id,
),
subnet=network.SubnetArgs(
id=subnet_id,
),
)],
tags=tags,
opts=ResourceOptions(
parent=self,
depends_on=depends_on,
custom_timeouts=CustomTimeouts(
create='1h',
update='1h',
delete='1h',
),
),
)
return er_gw
def firewall(stem, fw_sn_id, fwm_sn_id, private_ranges, depends_on=None):
fw_pip = network.PublicIPAddress(
f'{stem}{s}fw{s}pip{s}{suffix}',
resource_group_name=resource_group_name,
sku=network.PublicIPAddressSkuArgs(
name=network.PublicIPAddressSkuName.STANDARD,
),
public_ip_allocation_method='Static',
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
fwm_pip = network.PublicIPAddress(
f'{stem}{s}fwm{s}pip{s}{suffix}',
resource_group_name=resource_group_name,
sku=network.PublicIPAddressSkuArgs(
name=network.PublicIPAddressSkuName.STANDARD,
),
public_ip_allocation_method=network.IPAllocationMethod.STATIC,
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
fw = network.AzureFirewall(
f'{stem}{s}fw{s}{suffix}',
resource_group_name=resource_group_name,
additional_properties={
"Network.SNAT.PrivateRanges": private_ranges,
},
sku=network.AzureFirewallSkuArgs(
name='AZFW_VNet', # TODO: this enum name is busted
tier=network.AzureFirewallSkuTier.STANDARD,
),
ip_configurations=[network.AzureFirewallIPConfigurationArgs(
name=f'{stem}{s}fw{s}ipc',
public_ip_address=network.PublicIPAddressArgs(
id=fw_pip.id,
),
subnet=network.SubnetArgs(
id=fw_sn_id,
),
)],
management_ip_configuration=network.AzureFirewallIPConfigurationArgs(
name=f'{stem}{s}fwm{s}ipc',
public_ip_address=network.PublicIPAddressArgs(
id=fwm_pip.id,
),
subnet=network.SubnetArgs(
id=fwm_sn_id,
),
),
tags=tags,
opts=ResourceOptions(
parent=self,
depends_on=depends_on,
custom_timeouts=CustomTimeouts(
create='1h',
update='1h',
delete='1h',
),
),
)
return fw
def resource_group(stem):
rg = resources.ResourceGroup(
f'{stem}{s}vdc{s}rg{s}{suffix}',
tags=tags,
)
return rg.name
def route_table(stem, disable_bgp_route_propagation=None, depends_on=None):
rt = network.RouteTable(
f'{stem}{s}rt{s}{suffix}',
resource_group_name=resource_group_name,
disable_bgp_route_propagation=disable_bgp_route_propagation,
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
return rt
def route_to_internet(stem, route_table_name):
r_i = network.Route(
f'{stem}{s}r',
route_name='FirewallDefaultRoute', # name required
resource_group_name=resource_group_name,
address_prefix='0.0.0.0/0',
next_hop_type=network.RouteNextHopType.INTERNET,
route_table_name=route_table_name,
opts=ResourceOptions(parent=self, delete_before_replace=True),
)
return r_i
def route_to_virtual_appliance(
stem,
route_table_name,
address_prefix,
next_hop_ip_address,
):
r_va = network.Route(
f'{stem}{s}r',
resource_group_name=resource_group_name,
address_prefix=address_prefix,
next_hop_type=network.RouteNextHopType.VIRTUAL_APPLIANCE,
next_hop_ip_address=next_hop_ip_address,
route_table_name=route_table_name,
opts=ResourceOptions(parent=self),
)
return r_va
def route_to_virtual_network(stem, route_table_name, address_prefix):
r_vn = network.Route(
f'{stem}{s}r',
resource_group_name=resource_group_name,
address_prefix=address_prefix,
next_hop_type=network.RouteNextHopType.VNET_LOCAL,
route_table_name=route_table_name,
opts=ResourceOptions(parent=self),
)
return r_vn
def subnet(
stem,
virtual_network_name,
address_prefix,
route_table_id,
depends_on=None,
):
sn = network.Subnet(
f'{stem}{s}sn',
resource_group_name=resource_group_name,
virtual_network_name=virtual_network_name,
address_prefix=address_prefix,
route_table=network.RouteTableArgs(
id=route_table_id,
),
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
return sn
def subnet_special(
stem,
name,
virtual_network_name,
address_prefix,
route_table_id,
depends_on=None,
):
sn = network.Subnet(
f'{stem}{s}sn',
subnet_name=name,
resource_group_name=resource_group_name,
virtual_network_name=virtual_network_name,
address_prefix=address_prefix,
route_table=network.RouteTableArgs(
id=route_table_id,
),
opts=ResourceOptions(
parent=self,
delete_before_replace=True,
depends_on=depends_on,
),
)
return sn
def virtual_network(stem, address_spaces):
vn = network.VirtualNetwork(
f'{stem}{s}vn{s}{suffix}',
resource_group_name=resource_group_name,
address_space=network.AddressSpaceArgs(
address_prefixes=address_spaces,
),
tags=tags,
opts=ResourceOptions(parent=self),
)
return vn
def vnet_peering(
stem,
virtual_network_name,
peer,
remote_virtual_network_id,
allow_forwarded_traffic=None,
allow_gateway_transit=None,
use_remote_gateways=None,
depends_on=None,
):
vnp = network.VirtualNetworkPeering(
f'{stem}{s}{peer}{s}vnp{s}{suffix}',
resource_group_name=resource_group_name,
virtual_network_name=virtual_network_name,
remote_virtual_network=network.SubResourceArgs(
id=remote_virtual_network_id
),
allow_forwarded_traffic=allow_forwarded_traffic,
allow_gateway_transit=allow_gateway_transit,
use_remote_gateways=use_remote_gateways,
allow_virtual_network_access=True,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
return vnp
def vpn_gateway(stem, subnet_id, depends_on=None):
vpn_gw_pip = network.PublicIPAddress(
f'{stem}{s}vpn{s}gw{s}pip{s}{suffix}',
resource_group_name=resource_group_name,
public_ip_allocation_method=network.IPAllocationMethod.DYNAMIC,
tags=tags,
opts=ResourceOptions(parent=self, depends_on=depends_on),
)
vpn_gw = network.VirtualNetworkGateway(
f'{stem}{s}vpn{s}gw{s}{suffix}',
resource_group_name=resource_group_name,
sku=network.VirtualNetworkGatewaySkuArgs(
name=network.VirtualNetworkGatewaySkuName.VPN_GW1,
tier=network.VirtualNetworkGatewaySkuTier.VPN_GW1,
),
gateway_type=network.VirtualNetworkGatewayType.VPN,
vpn_type=network.VpnType.ROUTE_BASED,
enable_bgp=True,
ip_configurations=[network.VirtualNetworkGatewayIPConfigurationArgs(
name=f'{stem}{s}vpn{s}gw{s}ipc',
public_ip_address=network.PublicIPAddressArgs(
id=vpn_gw_pip.id,
),
subnet=network.SubnetArgs(
id=subnet_id,
),
)],
tags=tags,
opts=ResourceOptions(
parent=self,
depends_on=depends_on,
custom_timeouts=CustomTimeouts(
create='1h',
update='1h',
delete='1h',
),
),
)
return vpn_gw
if __name__ == "__main__":
print(dir())