Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vpc data lookup #798

Merged
merged 13 commits into from
Jun 6, 2024
Next Next commit
fix: vpc data full output on first apply
Jordan-Williams2 authored and Jordan-Williams2 committed May 8, 2024
commit 1366ab6717bfb88e3d2ef0bb5471a90889e9a83f
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 |
2 changes: 1 addition & 1 deletion default_security_group.tf
Original file line number Diff line number Diff line change
@@ -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 = data.ibm_is_vpc.vpc.default_security_group
direction = each.value.direction
remote = each.value.remote

7 changes: 6 additions & 1 deletion examples/default/outputs.tf
Original file line number Diff line number Diff line change
@@ -13,7 +13,12 @@ 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 = "VPC name property taken from the larger data element"
}

2 changes: 2 additions & 0 deletions examples/default/variables.tf
Original file line number Diff line number Diff line change
@@ -7,11 +7,13 @@ variable "ibmcloud_api_key" {
variable "region" {
description = "The region to which to deploy the VPC"
type = string
default = "us-south"
}

variable "prefix" {
description = "The prefix that you would like to append to your resources"
type = string
default = "test"
}

variable "resource_group" {
13 changes: 9 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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 ? 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
7 changes: 4 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

output "vpc_name" {
description = "Name of VPC created"
value = local.vpc_data.name
value = data.ibm_is_vpc.vpc.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,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
}

##############################################################################