Skip to content

Commit

Permalink
fix: added fix to ensure vpc_data is always output after initial ap…
Browse files Browse the repository at this point in the history
…ply (#781)
  • Loading branch information
jor2 committed May 14, 2024
1 parent 75ce6e9 commit 069d418
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion default_security_group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion examples/default/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}

##############################################################################
Expand Down Expand Up @@ -137,14 +137,15 @@ 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]
}

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
}

##############################################################################
Expand Down
4 changes: 4 additions & 0 deletions tests/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 069d418

Please sign in to comment.