-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.tf
161 lines (128 loc) · 10.8 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
######################################################################
# Default VPC
######################################################################
resource "huaweicloud_vpc" "this" {
count = var.is_vpc_create ? 1 : 0
name = var.vpc_name
cidr = var.vpc_cidr
description = var.vpc_description != "" ? var.vpc_description : null
secondary_cidrs = length(var.vpc_secondary_cidrs) > 0 ? var.vpc_secondary_cidrs : null
tags = length(var.vpc_tags) > 0 ? var.vpc_tags : null
enterprise_project_id = var.enterprise_project_id != "" ? var.enterprise_project_id : null
lifecycle {
precondition {
condition = (var.vpc_name != "" && var.vpc_name != null) && (var.vpc_cidr != "" && var.vpc_cidr != null)
error_message = "Field 'vpc_name' and field 'vpc_cidr' are required."
}
}
}
data "huaweicloud_vpcs" "this" {
count = length(var.query_vpc_names) > 0 ? 1 : 0
}
######################################################################
# All subnets under VPC resource
######################################################################
resource "huaweicloud_vpc_subnet" "this" {
count = var.is_vpc_create ? length(var.subnets_configuration) : 0
vpc_id = huaweicloud_vpc.this[0].id
name = lookup(element(var.subnets_configuration, count.index), "name")
cidr = lookup(element(var.subnets_configuration, count.index), "cidr")
gateway_ip = lookup(element(var.subnets_configuration, count.index), "gateway_ip") != "" ? lookup(element(var.subnets_configuration, count.index), "gateway_ip") : cidrhost(lookup(element(var.subnets_configuration, count.index), "cidr"), 1)
description = lookup(element(var.subnets_configuration, count.index), "description") != "" ? lookup(element(var.subnets_configuration, count.index), "description") : null
ipv6_enable = lookup(element(var.subnets_configuration, count.index), "ipv6_enabled")
dhcp_enable = lookup(element(var.subnets_configuration, count.index), "dhcp_enabled")
dns_list = try(length(lookup(element(var.subnets_configuration, count.index), "dns_list")), 0) > 0 ? lookup(element(var.subnets_configuration, count.index), "dns_list") : null
tags = try(length(lookup(element(var.subnets_configuration, count.index), "tags")), 0) > 0 ? lookup(element(var.subnets_configuration, count.index), "tags") : null
}
data "huaweicloud_vpc_subnets" "this" {
count = length(var.query_subnet_names) > 0 ? 1 : 0
}
######################################################################
# Default security group
######################################################################
resource "huaweicloud_networking_secgroup" "this" {
count = var.is_security_group_create ? 1 : 0
name = var.security_group_name != "" ? var.security_group_name : null
description = var.security_group_description != "" ? var.security_group_description : null
delete_default_rules = true
enterprise_project_id = var.enterprise_project_id != "" ? var.enterprise_project_id : null
}
data "huaweicloud_networking_secgroups" "this" {
count = length(var.query_security_group_names) > 0 ? 1 : 0
}
data "huaweicloud_networking_secgroup_rules" "this" {
depends_on = [
huaweicloud_networking_secgroup_rule.in_v4_self_group,
huaweicloud_networking_secgroup_rule.this,
huaweicloud_networking_secgroup_rule.remote_address_group,
]
count = var.is_security_group_create ? 1 : 0
security_group_id = huaweicloud_networking_secgroup.this[0].id
}
######################################################################
# Default security group rule
######################################################################
# Allow ECSs in the security group to which this rule belongs to communicate with each other
resource "huaweicloud_networking_secgroup_rule" "in_v4_self_group" {
count = var.is_security_group_create ? 1 : 0
security_group_id = huaweicloud_networking_secgroup.this[0].id
ethertype = "IPv4"
direction = "ingress"
remote_group_id = huaweicloud_networking_secgroup.this[0].id
}
######################################################################
# Custom Security Group Rules
######################################################################
locals {
security_group_rules_without_remote_addresses = [for o in var.security_group_rules_configuration: o if try(length(lookup(o, "remote_addresses")), 0) < 1]
security_group_rules_with_remote_addresses = [for o in var.security_group_rules_configuration: o if try(length(lookup(o, "remote_addresses")), 0) > 0]
}
resource "huaweicloud_networking_secgroup_rule" "this" {
count = var.is_security_group_create ? length(local.security_group_rules_without_remote_addresses) : 0
security_group_id = huaweicloud_networking_secgroup.this[0].id
description = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "description") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "description") : null
direction = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "direction") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "direction") : null
ethertype = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "ethertype") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "ethertype") : null
protocol = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "protocol") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "protocol") : null
ports = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "ports") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "ports") : null
remote_ip_prefix = (lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_group_id") == "" || lookup(element(local.security_group_rules_without_remote_addresses, count.index),
"remote_group_id") == null) && (lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_address_group_id") == "" || lookup(element(local.security_group_rules_without_remote_addresses, count.index),
"remote_address_group_id") == null) ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_ip_prefix") : null
remote_group_id = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_group_id")
remote_address_group_id = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_group_id") == "" || lookup(element(local.security_group_rules_without_remote_addresses, count.index),
"remote_group_id") == null ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "remote_address_group_id") : null
action = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "action") != "" ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "action") : null
priority = lookup(element(local.security_group_rules_without_remote_addresses, count.index), "priority") != 0 ? lookup(element(local.security_group_rules_without_remote_addresses, count.index), "priority") : null
}
resource "huaweicloud_vpc_address_group" "security_group_rules_auto_created" {
count = var.is_security_group_create ? length(local.security_group_rules_with_remote_addresses) : 0
name = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "address_group_name")
ip_version = try(regexall("\\d+", lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ethertype"))[0], null)
addresses = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "remote_addresses")
lifecycle {
precondition {
condition = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "address_group_name") != "" && lookup(element(local.security_group_rules_with_remote_addresses, count.index), "address_group_name") != null
error_message = "Field address_group_name is required if field remote_addresses are specified"
}
precondition {
condition = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "remote_addresses") != "" && lookup(element(local.security_group_rules_with_remote_addresses, count.index), "remote_addresses") != null
error_message = "Invalid input for field 'ethertype', should be 'IPv4' or 'IPv6'"
}
precondition {
condition = can(regex("\\d+", lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ethertype")))
error_message = "Invalid input for field 'ethertype', should be 'IPv4' or 'IPv6'"
}
}
}
resource "huaweicloud_networking_secgroup_rule" "remote_address_group" {
count = var.is_security_group_create ? length(local.security_group_rules_with_remote_addresses) : 0
security_group_id = huaweicloud_networking_secgroup.this[0].id
description = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "description") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "description") : null
direction = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "direction") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "direction") : null
ethertype = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ethertype") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ethertype") : null
protocol = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "protocol") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "protocol") : null
ports = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ports") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "ports") : null
remote_address_group_id = huaweicloud_vpc_address_group.security_group_rules_auto_created[count.index].id
action = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "action") != "" ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "action") : null
priority = lookup(element(local.security_group_rules_with_remote_addresses, count.index), "priority") != 0 ? lookup(element(local.security_group_rules_with_remote_addresses, count.index), "priority") : null
}