Skip to content

Commit 3dc79b8

Browse files
authored
feat: Allow existing subnets to be attached to public gateways.<br>* input variable existing_subnet_ids renamed to existing_subnets<br>* type of input changed from list(string) to list(object)<br>* existing subnet object contains ID of subnet and boolean for public gateway attachment (#709)
1 parent 760689b commit 3dc79b8

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ To attach access management tags to resources in this module, you need the follo
119119
| [ibm_is_public_gateway.gateway](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_public_gateway) | resource |
120120
| [ibm_is_security_group_rule.default_vpc_rule](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_security_group_rule) | resource |
121121
| [ibm_is_subnet.subnet](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_subnet) | resource |
122+
| [ibm_is_subnet_public_gateway_attachment.exist_subnet_gw](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_subnet_public_gateway_attachment) | resource |
122123
| [ibm_is_vpc.vpc](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc) | resource |
123124
| [ibm_is_vpc_address_prefix.address_prefixes](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_address_prefix) | resource |
124125
| [ibm_is_vpc_address_prefix.subnet_prefix](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_address_prefix) | resource |
@@ -158,7 +159,7 @@ To attach access management tags to resources in this module, you need the follo
158159
| <a name="input_existing_cos_instance_guid"></a> [existing\_cos\_instance\_guid](#input\_existing\_cos\_instance\_guid) | GUID of the COS instance to create Flow log collector | `string` | `null` | no |
159160
| <a name="input_existing_dns_instance_id"></a> [existing\_dns\_instance\_id](#input\_existing\_dns\_instance\_id) | Id of an existing dns instance in which the custom resolver is created. Only relevant if enable\_hub is set to true. | `string` | `null` | no |
160161
| <a name="input_existing_storage_bucket_name"></a> [existing\_storage\_bucket\_name](#input\_existing\_storage\_bucket\_name) | Name of the COS bucket to collect VPC flow logs | `string` | `null` | no |
161-
| <a name="input_existing_subnet_ids"></a> [existing\_subnet\_ids](#input\_existing\_subnet\_ids) | The IDs of the existing subnets. Required if 'create\_subnets' is false. | `list(string)` | `null` | no |
162+
| <a name="input_existing_subnets"></a> [existing\_subnets](#input\_existing\_subnets) | The detail of the existing subnets and required mappings to other resources. Required if 'create\_subnets' is false. | <pre>list(object({<br> id = string<br> public_gateway = optional(bool, false)<br> }))</pre> | `[]` | no |
162163
| <a name="input_existing_vpc_id"></a> [existing\_vpc\_id](#input\_existing\_vpc\_id) | The ID of the existing vpc. Required if 'create\_vpc' is false. | `string` | `null` | no |
163164
| <a name="input_hub_vpc_crn"></a> [hub\_vpc\_crn](#input\_hub\_vpc\_crn) | Indicates the crn of the hub VPC for DNS resolution. See https://cloud.ibm.com/docs/vpc?topic=vpc-hub-spoke-model. Mutually exclusive with hub\_vpc\_id. | `string` | `null` | no |
164165
| <a name="input_hub_vpc_id"></a> [hub\_vpc\_id](#input\_hub\_vpc\_id) | Indicates the id of the hub VPC for DNS resolution. See https://cloud.ibm.com/docs/vpc?topic=vpc-hub-spoke-model. Mutually exclusive with hub\_vpc\_crn. | `string` | `null` | no |

examples/existing_vpc/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ module "slz_vpc" {
1818
create_subnets = false
1919
name = var.name
2020
public_gateway_name = var.public_gateway_name
21-
existing_subnet_ids = var.subnet_ids
21+
existing_subnets = [for id in var.subnet_ids : { "id" : id, "public_gateway" : false }]
2222
}

main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ locals {
1010
validate_existing_vpc_id = !var.create_vpc && var.existing_vpc_id == null ? tobool("If var.create_vpc is false, then provide a value for var.existing_vpc_id to create vpc.") : true
1111

1212
# tflint-ignore: terraform_unused_declarations
13-
validate_existing_subnet_id = !var.create_subnets && var.existing_subnet_ids == null ? tobool("If var.create_subnet is false, then provide a value for var.existing_subnet_ids to create subnets.") : true
14-
13+
validate_existing_subnet_id = !var.create_subnets && length(var.existing_subnets) == 0 ? tobool("If var.create_subnet is false, then provide a value for var.existing_subnets to create subnets.") : true
1514
# tflint-ignore: terraform_unused_declarations
1615
validate_existing_vpc_and_subnet = var.create_vpc == true && var.create_subnets == false ? tobool("If user is not providing a vpc then they should also not be providing a subnet") : true
1716

subnet.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@ resource "ibm_is_subnet" "subnet" {
4646
}
4747

4848
data "ibm_is_subnet" "subnet" {
49-
count = var.create_subnets == false ? length(var.existing_subnet_ids) : 0
50-
identifier = var.existing_subnet_ids[count.index]
49+
for_each = var.create_subnets == false ? { for subnet in var.existing_subnets : subnet.id => subnet } : {}
50+
identifier = each.key
5151
}
52+
53+
# if using existing subnets, attach public gateways as configured
54+
resource "ibm_is_subnet_public_gateway_attachment" "exist_subnet_gw" {
55+
# only choose subnets marked for gateways
56+
for_each = var.create_subnets == false ? { for subnet in var.existing_subnets : subnet.id => subnet if subnet.public_gateway } : {}
57+
subnet = each.key
58+
# find gateway detail using format of 'zone-#', determine '#' by getting last character of the 'zone' value of an existing subnet
59+
public_gateway = ibm_is_public_gateway.gateway["zone-${substr(data.ibm_is_subnet.subnet[each.key].zone, length(data.ibm_is_subnet.subnet[each.key].zone) - 1, 1)}"].id
60+
}
61+
5262
##############################################################################

variables.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,14 @@ variable "create_subnets" {
382382
default = true
383383
}
384384

385-
variable "existing_subnet_ids" {
386-
description = "The IDs of the existing subnets. Required if 'create_subnets' is false."
387-
type = list(string)
388-
default = null
385+
variable "existing_subnets" {
386+
description = "The detail of the existing subnets and required mappings to other resources. Required if 'create_subnets' is false."
387+
type = list(object({
388+
id = string
389+
public_gateway = optional(bool, false)
390+
}))
391+
default = []
392+
nullable = false
389393
}
390394

391395
##############################################################################

0 commit comments

Comments
 (0)