Skip to content

Commit 1bc5e11

Browse files
author
Sean Sundberg
authored
Adds support for more than 3 subnets (#4)
* Updates provider configuration * Increases the subnet count for the test to 6 * Restricts zone names to -1, -2, or -3 using modulo * Only create 1 gateway per zone and share the gateway between subnets in the same zone * Prints subnets during validation Signed-off-by: Sean Sundberg <[email protected]>
1 parent b6383ec commit 1bc5e11

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ if ! ibmcloud is vpc "${VPC_ID}"; then
3939
fi
4040

4141
echo "Retrieving VPC subnets for VPC: ${VPC_NAME}"
42+
ibmcloud is subnets | grep "${VPC_NAME}"
4243
SUBNETS=$(ibmcloud is subnets | grep "${VPC_NAME}")
4344

4445
if [[ -z "${SUBNETS}" ]]; then

main.tf

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
provider "ibm" {
2-
generation = 2
3-
version = ">= 1.8.1"
4-
region = var.region
5-
}
6-
provider "null" {
7-
}
8-
provider "local" {
2+
generation = 2
3+
region = var.region
4+
ibmcloud_api_key = var.ibmcloud_api_key
95
}
106

117
locals {
12-
zone_ids = range(1, var.subnet_count + 1)
13-
vpc_zone_names = [ for id in local.zone_ids: "${var.region}-${id}" ]
8+
zone_count = 3
9+
zone_ids = range(var.subnet_count)
10+
vpc_zone_names = [ for index in local.zone_ids: "${var.region}-${(index % local.zone_count) + 1}" ]
1411
prefix_name = var.name_prefix != "" ? var.name_prefix : var.resource_group_name
1512
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
1613
vpc_id = ibm_is_vpc.vpc.id
1714
subnet_ids = ibm_is_subnet.vpc_subnet[*].id
18-
gateway_ids = var.public_gateway ? ibm_is_public_gateway.vpc_gateway[*].id : [ for val in local.zone_ids: "" ]
15+
gateway_ids = var.public_gateway ? ibm_is_public_gateway.vpc_gateway[*].id : [ for val in range(local.zone_count): "" ]
1916
security_group = ibm_is_vpc.vpc.default_security_group
2017
ipv4_cidr_blocks = ibm_is_subnet.vpc_subnet[*].ipv4_cidr_block
2118
}
@@ -30,7 +27,7 @@ resource ibm_is_vpc vpc {
3027
}
3128

3229
resource ibm_is_public_gateway vpc_gateway {
33-
count = var.public_gateway ? var.subnet_count : 0
30+
count = var.public_gateway ? min(local.zone_count, var.subnet_count) : 0
3431

3532
name = "${local.vpc_name}-gateway-${format("%02s", count.index)}"
3633
vpc = local.vpc_id
@@ -70,7 +67,7 @@ resource ibm_is_subnet vpc_subnet {
7067
name = "${local.vpc_name}-subnet-${format("%02s", count.index)}"
7168
zone = local.vpc_zone_names[count.index]
7269
vpc = local.vpc_id
73-
public_gateway = local.gateway_ids[count.index]
70+
public_gateway = local.gateway_ids[count.index % local.zone_count]
7471
total_ipv4_address_count = 256
7572
resource_group = data.ibm_resource_group.resource_group.id
7673
network_acl = ibm_is_network_acl.network_acl.id

test/stages/stage2-vpc.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module "dev_vpc" {
55
region = var.region
66
name_prefix = var.name_prefix
77
ibmcloud_api_key = var.ibmcloud_api_key
8-
subnet_count = 1
8+
subnet_count = var.vpc_subnet_count
99
public_gateway = var.vpc_public_gateway == "true"
1010
}

test/stages/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ variable "vpc_public_gateway" {
3737
description = "Flag indicating the public gateway should be created"
3838
default = "true"
3939
}
40+
41+
variable "vpc_subnet_count" {
42+
type = number
43+
description = "The number of subnets to create for the VPC instance"
44+
default = 6
45+
}

0 commit comments

Comments
 (0)