-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
197 lines (174 loc) · 6.48 KB
/
main.tf
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
locals {
vpc_id = var.create_vpc ? sbercloud_vpc.this[0].id : var.vpc_id
}
#vpc
resource "sbercloud_vpc" "this" {
count = var.create_vpc ? 1 : 0
region = var.region
name = var.name
cidr = var.cidr
description = var.description
tags = merge(var.tags, var.vpc_tags)
enterprise_project_id = var.enterprise_project_id
}
# subnet
resource "sbercloud_vpc_subnet" "this" {
count = var.create_vpc && length(var.subnets) > 0 ? length(var.subnets) : 0
region = var.region
name = try(var.subnet_names[count.index], format("${var.name}-subnet-%s", element(var.azs, count.index)))
cidr = element(concat(var.subnets.*.cidr, [""]), count.index)
gateway_ip = element(concat(var.subnets.*.gateway_ip, [""]), count.index)
vpc_id = local.vpc_id
dhcp_enable = var.dhcp_enable
primary_dns = var.primary_dns
secondary_dns = var.secondary_dns
dns_list = var.dns_list
availability_zone = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) > 0 ? element(var.azs, count.index) : null
tags = merge(var.tags, var.subnet_tags)
}
# routes for default route table
resource "sbercloud_vpc_route" "default_route" {
for_each = { for k, v in var.default_route_table_routes : k => v }
region = var.region
vpc_id = local.vpc_id
destination = lookup(each.value, "destination")
type = lookup(each.value, "type")
nexthop = lookup(each.value, "nexthop")
description = lookup(each.value, "description", null)
route_table_id = lookup(each.value, "route_table_id", null)
}
# public nat gateway
resource "sbercloud_nat_gateway" "this" {
for_each = { for k, v in var.subnets : k => v
if lookup(v, "nat_gw", null) != null
}
region = var.region
name = "${sbercloud_vpc_subnet.this[index(var.subnets, each.value)].name}-natgw"
subnet_id = sbercloud_vpc_subnet.this[index(var.subnets, each.value)].id
vpc_id = local.vpc_id
spec = lookup(each.value.nat_gw, "spec")
description = "For ${sbercloud_vpc_subnet.this[index(var.subnets, each.value)].name}"
enterprise_project_id = lookup(each.value.nat_gw, "enterprise_project_id", null)
}
# nat gateway eip
resource "sbercloud_vpc_eip" "snat" {
for_each = { for k, v in var.subnets : k => v
if lookup(v, "nat_gw", null) != null && lookup(v, "eip", null) != null && lookup(v, "existing_eip", null) == null
}
publicip {
type = lookup(each.value.eip, "type")
}
bandwidth {
share_type = lookup(each.value.eip, "share_type")
name = "${sbercloud_vpc_subnet.this[index(var.subnets, each.value)].name}-nat-gw-eip"
size = lookup(each.value.eip, "size")
charge_mode = lookup(each.value.eip, "charge_mode")
}
}
# nat gateway snat
resource "sbercloud_nat_snat_rule" "this" {
for_each = { for k, v in var.subnets : k => v
if lookup(v, "nat_gw", null) != null && lookup(v, "eip", null) != null || lookup(v, "existing_eip", null) != null
}
nat_gateway_id = sbercloud_nat_gateway.this[index(var.subnets, each.value)].id
subnet_id = sbercloud_vpc_subnet.this[index(var.subnets, each.value)].id
floating_ip_id = lookup(each.value, "existing_eip", sbercloud_vpc_eip.snat[index(var.subnets, each.value)].id)
}
# subnet route table
resource "sbercloud_vpc_route_table" "subnet" {
for_each = { for k, v in var.subnets : k => v }
name = "${sbercloud_vpc_subnet.this[index(var.subnets, each.value)].name}-rtb"
vpc_id = local.vpc_id
subnets = [sbercloud_vpc_subnet.this[index(var.subnets, each.value)].id]
# ECS routes
dynamic "route" {
for_each = lookup(each.value, "ecs_routes", [])
content {
destination = lookup(route.value, "destination")
type = "ecs"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# ENI routes
dynamic "route" {
for_each = lookup(each.value, "eni_routes", [])
content {
destination = lookup(route.value, "destination")
type = "eni"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# Virtual IP routes
dynamic "route" {
for_each = lookup(each.value, "vip_routes", [])
content {
destination = lookup(route.value, "destination")
type = "vip"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# NAT routes
dynamic "route" {
for_each = lookup(each.value, "nat_routes", [])
content {
destination = lookup(route.value, "destination")
type = "nat"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# VPC Peering routes
dynamic "route" {
for_each = lookup(each.value, "peering", [])
content {
destination = lookup(route.value, "destination")
type = "peering"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# VPN routes
dynamic "route" {
for_each = lookup(each.value, "vpn_routes", [])
content {
destination = lookup(route.value, "destination")
type = "vpn"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# Direct Connect routes
dynamic "route" {
for_each = lookup(each.value, "dc_routes", [])
content {
destination = lookup(route.value, "destination")
type = "dc"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# Cloud Connection routes
dynamic "route" {
for_each = lookup(each.value, "cc_routes", [])
content {
destination = lookup(route.value, "destination")
type = "cc"
nexthop = lookup(route.value, "nexthop")
description = lookup(route.value, "description", null)
}
}
# NAT Gateway Route (for nat gw created in this module)
dynamic "route" {
for_each = lookup(each.value, "nat_gw", [])
content {
destination = "0.0.0.0/0"
type = "nat"
nexthop = sbercloud_nat_gateway.this[index(var.subnets, each.value)].id
description = "NAT Gateway SNAT"
}
}
# VPC Peering Route (for vpc peering created in this module) TODO
}