diff --git a/README.md b/README.md index 0c1c1ec1..c5598169 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ To attach access management tags to resources in this module, you need the follo | [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 | | [ibm_resource_instance.dns_instance_hub](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/resource_instance) | resource | | [time_sleep.wait_for_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource | +| [time_sleep.wait_for_vpc_creation_data](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource | | [ibm_iam_account_settings.iam_account_settings](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/iam_account_settings) | data source | | [ibm_is_subnet.subnet](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_subnet) | data source | | [ibm_is_vpc.vpc](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_vpc) | data source | diff --git a/default_security_group.tf b/default_security_group.tf index 97b5cb3d..013e085b 100644 --- a/default_security_group.tf +++ b/default_security_group.tf @@ -12,7 +12,7 @@ locals { resource "ibm_is_security_group_rule" "default_vpc_rule" { for_each = local.security_group_rule_object - group = var.create_vpc == true ? ibm_is_vpc.vpc[0].default_security_group : data.ibm_is_vpc.vpc[0].default_security_group + group = var.create_vpc == true ? ibm_is_vpc.vpc[0].default_security_group : data.ibm_is_vpc.vpc.default_security_group direction = each.value.direction remote = each.value.remote diff --git a/examples/default/outputs.tf b/examples/default/outputs.tf index dd3f5814..059de0ac 100644 --- a/examples/default/outputs.tf +++ b/examples/default/outputs.tf @@ -13,10 +13,15 @@ output "vpc_crn" { } output "vpc_name" { - value = module.slz_vpc.vpc_data.name + value = module.slz_vpc.vpc_name description = "VPC name property taken from the larger data element" } +output "vpc_data" { + value = module.slz_vpc.vpc_data + description = "Data of the VPC used in this module, created or existing." +} + output "subnet_ids" { value = module.slz_vpc.subnet_ids description = "list of VPC subnet ids created" diff --git a/main.tf b/main.tf index dcac73f1..ef549fde 100644 --- a/main.tf +++ b/main.tf @@ -47,19 +47,24 @@ locals { ############################################################################## data "ibm_is_vpc" "vpc" { - count = var.create_vpc == false ? 1 : 0 - identifier = var.existing_vpc_id + depends_on = [time_sleep.wait_for_vpc_creation_data] + identifier = local.vpc_id } locals { - vpc_id = var.create_vpc ? ibm_is_vpc.vpc[0].id : var.existing_vpc_id - vpc_data = var.create_vpc ? ibm_is_vpc.vpc[0] : data.ibm_is_vpc.vpc[0] + vpc_id = var.create_vpc ? resource.ibm_is_vpc.vpc[0].id : var.existing_vpc_id } ############################################################################## # Create new VPC ############################################################################## +resource "time_sleep" "wait_for_vpc_creation_data" { + depends_on = [resource.ibm_is_vpc.vpc, resource.ibm_is_subnet.subnet] + count = var.create_vpc == true || var.create_subnets ? 1 : 0 + create_duration = "30s" +} + resource "ibm_is_vpc" "vpc" { count = var.create_vpc == true ? 1 : 0 name = var.prefix != null ? "${var.prefix}-${var.name}-vpc" : var.name diff --git a/outputs.tf b/outputs.tf index d2274bc9..4412e6b5 100644 --- a/outputs.tf +++ b/outputs.tf @@ -4,7 +4,7 @@ output "vpc_name" { description = "Name of VPC created" - value = local.vpc_data.name + value = data.ibm_is_vpc.vpc.resource_name } output "vpc_id" { @@ -14,7 +14,7 @@ output "vpc_id" { output "vpc_crn" { description = "CRN of VPC created" - value = local.vpc_data.crn + value = data.ibm_is_vpc.vpc.crn } ############################################################################## @@ -137,6 +137,7 @@ output "vpc_flow_logs" { } ############################################################################## + output "cidr_blocks" { description = "List of CIDR blocks present in VPC stack" value = [for address in data.ibm_is_vpc_address_prefixes.get_address_prefixes.address_prefixes : address.cidr] @@ -144,7 +145,7 @@ output "cidr_blocks" { output "vpc_data" { description = "Data of the VPC used in this module, created or existing." - value = local.vpc_data + value = data.ibm_is_vpc.vpc } ############################################################################## diff --git a/tests/pr_test.go b/tests/pr_test.go index 893f4854..e9860336 100644 --- a/tests/pr_test.go +++ b/tests/pr_test.go @@ -64,6 +64,10 @@ func TestRunDefaultExample(t *testing.T) { output, err := options.RunTestConsistency() assert.Nil(t, err, "This should not have errored") assert.NotNil(t, output, "Expected some output") + + expectedOutputs := []string{"vpc_data"} + missingOutputs, outputErr := testhelper.ValidateTerraformOutputs(options.LastTestTerraformOutputs, expectedOutputs...) + assert.Empty(t, outputErr, fmt.Sprintf("Missing expected outputs: %s", missingOutputs)) } func TestRunNoPrefixExample(t *testing.T) {