Skip to content

Commit 277085d

Browse files
argeigerocofaigh
andauthored
feat: utilize new routes resource (#121)
Co-authored-by: Adam Geiger <[email protected]> Co-authored-by: Conall Ó Cofaigh <[email protected]>
1 parent 4d561b9 commit 277085d

File tree

9 files changed

+109
-35
lines changed

9 files changed

+109
-35
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ You need the following permissions to run this module.
152152
| [ibm_is_vpc.vpc](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc) | resource |
153153
| [ibm_is_vpc_address_prefix.address_prefixes](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_address_prefix) | resource |
154154
| [ibm_is_vpc_address_prefix.subnet_prefix](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_address_prefix) | resource |
155-
| [ibm_is_vpc_route.route](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_route) | resource |
155+
| [ibm_is_vpc_routing_table.route_table](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_routing_table) | resource |
156+
| [ibm_is_vpc_routing_table_route.routing_table_routes](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_routing_table_route) | resource |
156157

157158
## Inputs
158159

@@ -169,7 +170,7 @@ You need the following permissions to run this module.
169170
| <a name="input_prefix"></a> [prefix](#input\_prefix) | The prefix that you would like to append to your resources | `string` | n/a | yes |
170171
| <a name="input_region"></a> [region](#input\_region) | The region to which to deploy the VPC | `string` | n/a | yes |
171172
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the VPC to be created | `string` | n/a | yes |
172-
| <a name="input_routes"></a> [routes](#input\_routes) | OPTIONAL - Allows you to specify the next hop for packets based on their destination address | <pre>list(<br> object({<br> name = string<br> zone = number<br> destination = string<br> next_hop = string<br> })<br> )</pre> | `[]` | no |
173+
| <a name="input_routes"></a> [routes](#input\_routes) | OPTIONAL - Allows you to specify the next hop for packets based on their destination address | <pre>list(<br> object({<br> name = string<br> route_direct_link_ingress = optional(bool)<br> route_transit_gateway_ingress = optional(bool)<br> route_vpc_zone_ingress = optional(bool)<br> routes = optional(<br> list(<br> object({<br> action = optional(string)<br> zone = number<br> destination = string<br> next_hop = string<br> })<br> ))<br> })<br> )</pre> | `[]` | no |
173174
| <a name="input_security_group_rules"></a> [security\_group\_rules](#input\_security\_group\_rules) | A list of security group rules to be added to the default vpc security group | <pre>list(<br> object({<br> name = string<br> direction = string<br> remote = string<br> tcp = optional(<br> object({<br> port_max = optional(number)<br> port_min = optional(number)<br> })<br> )<br> udp = optional(<br> object({<br> port_max = optional(number)<br> port_min = optional(number)<br> })<br> )<br> icmp = optional(<br> object({<br> type = optional(number)<br> code = optional(number)<br> })<br> )<br> })<br> )</pre> | <pre>[<br> {<br> "direction": "inbound",<br> "name": "default-sgr",<br> "remote": "10.0.0.0/8"<br> }<br>]</pre> | no |
174175
| <a name="input_subnets"></a> [subnets](#input\_subnets) | List of subnets for the vpc. For each item in each array, a subnet will be created. Items can be either CIDR blocks or total ipv4 addressess. Public gateways will be enabled only in zones where a gateway has been created | <pre>object({<br> zone-1 = list(object({<br> name = string<br> cidr = string<br> public_gateway = optional(bool)<br> acl_name = string<br> }))<br> zone-2 = list(object({<br> name = string<br> cidr = string<br> public_gateway = optional(bool)<br> acl_name = string<br> }))<br> zone-3 = list(object({<br> name = string<br> cidr = string<br> public_gateway = optional(bool)<br> acl_name = string<br> }))<br> })</pre> | <pre>{<br> "zone-1": [<br> {<br> "acl_name": "vpc-acl",<br> "cidr": "10.10.10.0/24",<br> "name": "subnet-a",<br> "public_gateway": true<br> }<br> ],<br> "zone-2": [<br> {<br> "acl_name": "vpc-acl",<br> "cidr": "10.20.10.0/24",<br> "name": "subnet-b",<br> "public_gateway": true<br> }<br> ],<br> "zone-3": [<br> {<br> "acl_name": "vpc-acl",<br> "cidr": "10.30.10.0/24",<br> "name": "subnet-c",<br> "public_gateway": false<br> }<br> ]<br>}</pre> | no |
175176
| <a name="input_tags"></a> [tags](#input\_tags) | List of Tags for the resource created | `list(string)` | `null` | no |

dynamic_values.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ module "unit_tests" {
3434
}
3535
routes = [
3636
{
37-
name = "test-route"
38-
zone = 1
39-
destination = "test"
40-
next_hop = "test"
37+
name = "test-route"
38+
routes = [
39+
{
40+
zone = 1
41+
destination = "10.2.14.1/32"
42+
next_hop = "1.1.1.1"
43+
}
44+
]
4145
}
4246
]
4347
use_public_gateways = {

dynamic_values.unit_tests.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ locals {
2323
# tflint-ignore: terraform_unused_declarations
2424
assert_route_key_exists = lookup(module.unit_tests.routes, "test-route")
2525
# tflint-ignore: terraform_unused_declarations
26-
assert_route_has_correct_next_hop = regex("test", module.unit_tests.routes["test-route"].next_hop)
26+
assert_route_has_route_table = lookup(module.unit_tests.routing_table_route_map, "ut-test-route-route-1")
2727
}
2828

2929
##############################################################################

dynamic_values/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,17 @@ output "subnet_map" {
7979
}
8080

8181
##############################################################################
82+
83+
##############################################################################
84+
# Routing table
85+
##############################################################################
86+
87+
output "routing_table_map" {
88+
description = "Routing table as map"
89+
value = module.routes.value
90+
}
91+
92+
output "routing_table_route_map" {
93+
description = "Routing table routes as map"
94+
value = local.routing_table_route_map
95+
}

dynamic_values/routes.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module "routes" {
2+
source = "./config_modules/list_to_map"
3+
list = var.routes
4+
}
5+
6+
locals {
7+
routing_table_route_list = flatten(
8+
[for route_table in module.routes.value : [
9+
for rt in(lookup(route_table, "routes", null) == null ? [] : route_table.routes) :
10+
merge(rt, { route_table = route_table.name, route_index = index(route_table.routes, rt) + 1 })
11+
]
12+
]
13+
)
14+
15+
routing_table_route_map = {
16+
for route in local.routing_table_route_list :
17+
("${var.prefix}-${route.route_table}-route-${route.route_index}") => route
18+
}
19+
}

dynamic_values/variables.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,19 @@ variable "routes" {
3737
description = "direct reference to routes variable"
3838
type = list(
3939
object({
40-
name = string
41-
zone = number
42-
destination = string
43-
next_hop = string
40+
name = string
41+
route_direct_link_ingress = optional(bool)
42+
route_transit_gateway_ingress = optional(bool)
43+
route_vpc_zone_ingress = optional(bool)
44+
routes = optional(
45+
list(
46+
object({
47+
action = optional(string)
48+
zone = number
49+
destination = string
50+
next_hop = string
51+
})
52+
))
4453
})
4554
)
4655
}

main.tf

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,27 @@ resource "ibm_is_vpc_address_prefix" "address_prefixes" {
4040

4141

4242
##############################################################################
43-
# ibm_is_vpc_route: Create vpc route resource
43+
# Create vpc route resource
4444
##############################################################################
4545

46-
locals {
47-
routes_map = {
48-
# Convert routes from list to map
49-
for route in var.routes :
50-
(route.name) => route
51-
}
46+
resource "ibm_is_vpc_routing_table" "route_table" {
47+
for_each = module.dynamic_values.routing_table_map
48+
name = "${var.prefix}-${var.name}-route-${each.value.name}"
49+
vpc = ibm_is_vpc.vpc.id
50+
route_direct_link_ingress = each.value.route_direct_link_ingress
51+
route_transit_gateway_ingress = each.value.route_transit_gateway_ingress
52+
route_vpc_zone_ingress = each.value.route_vpc_zone_ingress
5253
}
5354

54-
resource "ibm_is_vpc_route" "route" {
55-
for_each = local.routes_map
56-
name = "${var.prefix}-${var.name}-route-${each.value.name}"
57-
vpc = ibm_is_vpc.vpc.id
58-
zone = each.value.zone
59-
destination = each.value.destination
60-
next_hop = each.value.next_hop
55+
resource "ibm_is_vpc_routing_table_route" "routing_table_routes" {
56+
for_each = module.dynamic_values.routing_table_route_map
57+
vpc = ibm_is_vpc.vpc.id
58+
routing_table = ibm_is_vpc_routing_table.route_table[each.value.route_table].routing_table
59+
zone = "${var.region}-${each.value.zone}"
60+
name = each.key
61+
destination = each.value.destination
62+
action = each.value.action
63+
next_hop = each.value.next_hop
6164
}
6265

6366
##############################################################################

module-metadata.json

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
},
136136
"routes": {
137137
"name": "routes",
138-
"type": "list(\n object({\n name = string\n zone = number\n destination = string\n next_hop = string\n })\n )",
138+
"type": "list(\n object({\n name = string\n route_direct_link_ingress = optional(bool)\n route_transit_gateway_ingress = optional(bool)\n route_vpc_zone_ingress = optional(bool)\n routes = optional(\n list(\n object({\n action = optional(string)\n zone = number\n destination = string\n next_hop = string\n })\n ))\n })\n )",
139139
"description": "OPTIONAL - Allows you to specify the next hop for packets based on their destination address",
140140
"default": [],
141141
"pos": {
@@ -336,7 +336,7 @@
336336
},
337337
"pos": {
338338
"filename": "main.tf",
339-
"line": 78
339+
"line": 81
340340
}
341341
},
342342
"ibm_is_security_group_rule.default_vpc_rule": {
@@ -413,10 +413,10 @@
413413
"line": 16
414414
}
415415
},
416-
"ibm_is_vpc_route.route": {
416+
"ibm_is_vpc_routing_table.route_table": {
417417
"mode": "managed",
418-
"type": "ibm_is_vpc_route",
419-
"name": "route",
418+
"type": "ibm_is_vpc_routing_table",
419+
"name": "route_table",
420420
"attributes": {
421421
"name": "prefix"
422422
},
@@ -425,7 +425,22 @@
425425
},
426426
"pos": {
427427
"filename": "main.tf",
428-
"line": 54
428+
"line": 46
429+
}
430+
},
431+
"ibm_is_vpc_routing_table_route.routing_table_routes": {
432+
"mode": "managed",
433+
"type": "ibm_is_vpc_routing_table_route",
434+
"name": "routing_table_routes",
435+
"attributes": {
436+
"zone": "region"
437+
},
438+
"provider": {
439+
"name": "ibm"
440+
},
441+
"pos": {
442+
"filename": "main.tf",
443+
"line": 55
429444
}
430445
}
431446
},

variables.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,19 @@ variable "routes" {
399399
description = "OPTIONAL - Allows you to specify the next hop for packets based on their destination address"
400400
type = list(
401401
object({
402-
name = string
403-
zone = number
404-
destination = string
405-
next_hop = string
402+
name = string
403+
route_direct_link_ingress = optional(bool)
404+
route_transit_gateway_ingress = optional(bool)
405+
route_vpc_zone_ingress = optional(bool)
406+
routes = optional(
407+
list(
408+
object({
409+
action = optional(string)
410+
zone = number
411+
destination = string
412+
next_hop = string
413+
})
414+
))
406415
})
407416
)
408417
default = []

0 commit comments

Comments
 (0)