Skip to content

Commit 5a80541

Browse files
authored
fix: create VPE when crn name is unknown at plan time
* fix: create VPE when crn name is unknown at plan time * fix: handle ip reservation and binding for instance vpe
1 parent d31065e commit 5a80541

File tree

6 files changed

+74
-26
lines changed

6 files changed

+74
-26
lines changed

examples/default/main.tf

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ module "vpe_security_group" {
5656
vpc_id = var.vpc_id != null ? var.vpc_id : module.vpc[0].vpc_id
5757
}
5858

59+
60+
##############################################################################
61+
# Create a PostgreSQL instance to demonstrate how to create an instance VPE
62+
##############################################################################
63+
64+
module "postgresql_db" {
65+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-icd-postgresql?ref=v3.1.0"
66+
resource_group_id = module.resource_group.resource_group_id
67+
name = "${var.prefix}-vpe-pg"
68+
region = var.region
69+
}
70+
71+
locals {
72+
cloud_service_by_crn = concat([{
73+
name = "postgresql" # name of the vpe
74+
crn = module.postgresql_db.crn }
75+
], var.cloud_service_by_crn)
76+
}
77+
5978
##############################################################################
6079
# Create VPEs in the VPC
6180
##############################################################################
@@ -69,16 +88,19 @@ module "vpes" {
6988
resource_group_id = module.resource_group.resource_group_id
7089
security_group_ids = var.security_group_ids != null ? var.security_group_ids : [module.vpe_security_group.security_group_id]
7190
cloud_services = var.cloud_services
72-
cloud_service_by_crn = var.cloud_service_by_crn
91+
cloud_service_by_crn = local.cloud_service_by_crn
7392
service_endpoints = var.service_endpoints
74-
# Wait 30secs after security group is destroyed before destroying VPE to workaround timing issue which can produce “Target not found” error on destroy
75-
depends_on = [time_sleep.wait_30_seconds]
93+
# See comments below (resource "time_sleep" "sleep_time") for explaination on why this is needed.
94+
depends_on = [time_sleep.sleep_time]
7695
}
7796

78-
resource "time_sleep" "wait_30_seconds" {
79-
depends_on = [data.ibm_is_security_group.default_sg]
80-
81-
destroy_duration = "30s"
97+
## This sleep serve two purposes:
98+
# 1. Give some extra time after postgresql db creation, and before creating the VPE targetting it. This works around the error "Service does not support VPE extensions."
99+
# 2. Give time on deletion between the VPE destruction and the destruction of the SG that is attached to the VPE. This works around the error "Target not found"
100+
resource "time_sleep" "sleep_time" {
101+
depends_on = [module.vpe_security_group.security_group_id, module.postgresql_db]
102+
create_duration = "120s"
103+
destroy_duration = "120s"
82104
}
83105

84106

examples/default/moved.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
moved {
2+
from = time_sleep.wait_30_seconds
3+
to = time_sleep.sleep_time
4+
}

main.tf

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,30 @@ locals {
2727
endpoint_ip_list = flatten([
2828
# Create object for each subnet
2929
for subnet in var.subnet_zone_list :
30-
[
30+
concat([
3131
for service in var.cloud_services :
3232
{
3333
ip_name = "${subnet.name}-${service}-gateway-${replace(subnet.zone, "/${var.region}-/", "")}-ip"
3434
subnet_id = subnet.id
35-
gateway_name = "${var.vpc_name}-${service}"
35+
gateway_name = "${var.prefix}-${var.vpc_name}-${service}"
3636
}
37-
]
37+
],
38+
[
39+
for service in var.cloud_service_by_crn :
40+
{
41+
ip_name = "${subnet.name}-${service.name}-gateway-${replace(subnet.zone, "/${var.region}-/", "")}-ip"
42+
subnet_id = subnet.id
43+
gateway_name = "${var.prefix}-${var.vpc_name}-${service.name}"
44+
}
45+
])
3846
])
3947

48+
# Convert the virtual_endpoint_gateway output from list to a map
49+
vpe_map = {
50+
for gateway in ibm_is_virtual_endpoint_gateway.vpe :
51+
(gateway.name) => gateway
52+
}
53+
4054
# Map of Services to endpoints
4155
service_to_endpoint_map = {
4256
kms = "crn:v1:bluemix:public:kms:${var.region}:::endpoint:${var.service_endpoints}.${var.region}.kms.cloud.ibm.com"
@@ -68,18 +82,13 @@ resource "ibm_is_subnet_reserved_ip" "ip" {
6882
##############################################################################
6983

7084
resource "ibm_is_virtual_endpoint_gateway" "vpe" {
71-
for_each = {
72-
# Create map based on gateway name if enabled
73-
for gateway in local.gateway_list :
74-
(gateway.name) => gateway
75-
}
76-
77-
name = "${var.prefix}-${each.key}"
85+
count = length(local.gateway_list)
86+
name = "${var.prefix}-${local.gateway_list[count.index].name}"
7887
vpc = var.vpc_id
7988
resource_group = var.resource_group_id
8089
security_groups = var.security_group_ids
8190
target {
82-
crn = each.value.service == null ? each.value.crn : local.service_to_endpoint_map[each.value.service]
91+
crn = local.gateway_list[count.index].service == null ? local.gateway_list[count.index].crn : local.service_to_endpoint_map[local.gateway_list[count.index].service]
8392
resource_type = "provider_cloud_service"
8493
}
8594
}
@@ -96,7 +105,7 @@ resource "ibm_is_virtual_endpoint_gateway_ip" "endpoint_gateway_ip" {
96105
for gateway_ip in local.endpoint_ip_list :
97106
(gateway_ip.ip_name) => gateway_ip
98107
}
99-
gateway = ibm_is_virtual_endpoint_gateway.vpe[each.value.gateway_name].id
108+
gateway = local.vpe_map[each.value.gateway_name].id
100109
reserved_ip = ibm_is_subnet_reserved_ip.ip[each.key].reserved_ip
101110
}
102111

module-metadata.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
},
151151
"pos": {
152152
"filename": "main.tf",
153-
"line": 55
153+
"line": 69
154154
}
155155
},
156156
"ibm_is_virtual_endpoint_gateway.vpe": {
@@ -168,7 +168,7 @@
168168
},
169169
"pos": {
170170
"filename": "main.tf",
171-
"line": 70
171+
"line": 84
172172
}
173173
},
174174
"ibm_is_virtual_endpoint_gateway_ip.endpoint_gateway_ip": {
@@ -180,7 +180,7 @@
180180
},
181181
"pos": {
182182
"filename": "main.tf",
183-
"line": 93
183+
"line": 102
184184
}
185185
}
186186
},

moved.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
moved {
2+
from = ibm_is_virtual_endpoint_gateway.vpe["vpc-instance-cloud-object-storage"]
3+
to = ibm_is_virtual_endpoint_gateway.vpe[1]
4+
}
5+
6+
moved {
7+
from = ibm_is_virtual_endpoint_gateway.vpe["vpc-instance-kms"]
8+
to = ibm_is_virtual_endpoint_gateway.vpe[0]
9+
}

tests/pr_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ const defaultExampleTerraformDir = "examples/default"
1414

1515
func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptions {
1616
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
17-
Testing: t,
18-
TerraformDir: dir,
19-
Prefix: prefix,
17+
Testing: t,
18+
TerraformDir: dir,
19+
Prefix: prefix,
20+
IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check
21+
List: []string{
22+
"time_sleep.sleep_time",
23+
},
24+
},
2025
ResourceGroup: resourceGroup,
2126
})
2227
return options
@@ -32,7 +37,6 @@ func TestRunDefaultExample(t *testing.T) {
3237
}
3338

3439
func TestRunUpgradeExample(t *testing.T) {
35-
t.Skip()
3640
t.Parallel()
3741

3842
options := setupOptions(t, "vpe-upgrade", defaultExampleTerraformDir)

0 commit comments

Comments
 (0)