Skip to content

Commit 7507427

Browse files
author
Sean Sundberg
authored
Fixes handling of address_prefixes (#28)
- Updates condition for ipv4_cidr_provided flag to include a greater than zero test - Reorders resources according to execution - Updates output dependencies to wait for address prefixes to be provisioned and the default flag to be set Signed-off-by: Sean Sundberg <[email protected]>
1 parent c966988 commit 7507427

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

main.tf

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ locals {
99
security_group_id = data.ibm_is_vpc.vpc.default_security_group
1010
acl_id = data.ibm_is_vpc.vpc.default_network_acl
1111
crn = data.ibm_is_vpc.vpc.resource_crn
12-
ipv4_cidr_provided = length(var.address_prefixes) >= var.address_prefix_count
12+
ipv4_cidr_provided = var.address_prefix_count > 0 && length(var.address_prefixes) >= var.address_prefix_count
1313
ipv4_cidr_block = local.ipv4_cidr_provided ? var.address_prefixes : [ for val in range(var.address_prefix_count): "" ]
14-
address_prefix_management = local.ipv4_cidr_provided ? "manual" : "auto"
1514
provision_cidr = var.provision && local.ipv4_cidr_provided
1615
}
1716

@@ -24,34 +23,40 @@ resource null_resource print_values {
2423
}
2524
}
2625

27-
resource ibm_is_vpc_address_prefix cidr_prefix {
28-
count = local.provision_cidr ? var.address_prefix_count : 0
29-
30-
name = "${local.vpc_name}-cidr-${format("%02s", count.index)}"
31-
zone = local.vpc_zone_names[count.index]
32-
vpc = data.ibm_is_vpc.vpc.id
33-
cidr = local.ipv4_cidr_block[count.index]
34-
}
35-
3626
resource ibm_is_vpc vpc {
3727
count = var.provision ? 1 : 0
3828

3929
name = local.vpc_name
4030
resource_group = var.resource_group_id
41-
address_prefix_management = local.address_prefix_management
31+
address_prefix_management = local.ipv4_cidr_provided ? "manual" : "auto"
4232
default_security_group_name = "${local.vpc_name}-security-group"
4333
default_network_acl_name = "${local.vpc_name}-acl"
4434
default_routing_table_name = "${local.vpc_name}-routing"
4535
}
4636

37+
data ibm_is_vpc vpc {
38+
depends_on = [ibm_is_vpc.vpc]
39+
40+
name = local.vpc_name
41+
}
42+
43+
resource ibm_is_vpc_address_prefix cidr_prefix {
44+
count = local.provision_cidr ? var.address_prefix_count : 0
45+
46+
name = "${local.vpc_name}-cidr-${format("%02s", count.index)}"
47+
zone = local.vpc_zone_names[count.index]
48+
vpc = data.ibm_is_vpc.vpc.id
49+
cidr = local.ipv4_cidr_block[count.index]
50+
}
51+
4752
# Set the address prefixes as the default. This will allow us to specify the number of ips required
4853
# in each subnet, instead of figuring out specific cidrs.
4954
# Note the "split" function call - this is because the id returned from creating the address
5055
# comes back as <vpc_id>/<address_range_id> and the update call wants these passed as separate
5156
# arguments. I suspect this is actually a defect in what is returned from ibm_is_vpc_address_prefix
5257
# and it may one day be fixed and trip up this code.
5358
resource null_resource post_vpc_address_pfx_default {
54-
count = local.provision_cidr ? var.address_prefix_count : 0
59+
count = local.provision_cidr ? 1 : 0
5560
depends_on = [ibm_is_vpc_address_prefix.cidr_prefix]
5661

5762
provisioner "local-exec" {
@@ -64,12 +69,6 @@ resource null_resource post_vpc_address_pfx_default {
6469
}
6570
}
6671

67-
data ibm_is_vpc vpc {
68-
depends_on = [ibm_is_vpc.vpc]
69-
70-
name = local.vpc_name
71-
}
72-
7372
resource ibm_is_security_group_rule rule_icmp_ping {
7473
count = var.provision ? 1 : 0
7574

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
output "name" {
33
value = local.vpc_name
4-
depends_on = [ibm_is_vpc.vpc]
4+
depends_on = [null_resource.post_vpc_address_pfx_default]
55
description = "The name of the vpc instance"
66
}
77

88
output "id" {
99
value = local.vpc_id
10-
depends_on = [ibm_is_vpc.vpc]
10+
depends_on = [null_resource.post_vpc_address_pfx_default]
1111
description = "The id of the vpc instance"
1212
}
1313

@@ -18,6 +18,6 @@ output "acl_id" {
1818

1919
output "crn" {
2020
value = local.crn
21-
depends_on = [ibm_is_vpc.vpc]
21+
depends_on = [null_resource.post_vpc_address_pfx_default]
2222
description = "The CRN for the vpc instance"
2323
}

0 commit comments

Comments
 (0)