Skip to content

Commit 87b5250

Browse files
author
Sean Sundberg
authored
Updates subnet structure for module (#9)
- Adds `subnets` input list containing labels for the subnets - Leaves subnet_count for backwards compatibility - Adds `subnet_label_counts` output that gives subnet counts for each label - Adds `subnets` output that returns list of objects containing subnet id and label - Splits test cases into two, subnet count and subnets object, and runs tests in parallel Signed-off-by: Sean Sundberg <[email protected]>
1 parent 14c1561 commit 87b5250

File tree

12 files changed

+104
-13
lines changed

12 files changed

+104
-13
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ elif [[ "${PUBLIC_GATEWAY}" == "false" ]] && [[ -n "${PGS}" ]]; then
5959
exit 1
6060
fi
6161

62+
cat "./subnet_label_counts.json" | jq '.'
63+
cat "./subnets.json" | jq '.'
64+
6265
exit 0

.github/workflows/verify.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
platform: [ocp4_latest]
21-
# max-parallel: 1
20+
platform:
21+
- vpc_count
22+
- vpc_subnets
2223
fail-fast: false
2324

2425
env:

main.tf

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11

22
locals {
33
zone_count = 3
4-
zone_ids = range(var.subnet_count)
5-
vpc_zone_names = [ for index in local.zone_ids: "${var.region}-${(index % local.zone_count) + 1}" ]
4+
subnet_count = length(var.subnets) > 0 ? length(var.subnets) : var.subnet_count
5+
vpc_zone_names = [ for index in range(local.subnet_count): "${var.region}-${(index % local.zone_count) + 1}" ]
66
prefix_name = var.name_prefix != "" ? var.name_prefix : var.resource_group_name
77
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
88
vpc_id = ibm_is_vpc.vpc.id
99
subnet_ids = ibm_is_subnet.vpc_subnet[*].id
1010
gateway_ids = var.public_gateway ? ibm_is_public_gateway.vpc_gateway[*].id : [ for val in range(local.zone_count): "" ]
1111
security_group_id = ibm_is_vpc.vpc.default_security_group
1212
ipv4_cidr_blocks = ibm_is_subnet.vpc_subnet[*].ipv4_cidr_block
13+
distinct_subnet_labels = distinct([ for val in var.subnets: val.label ])
14+
# creates an intermediate object where the key is the label and the value is an array of labels, one for each appearance
15+
# e.g. [{label = "basic"}, {label = "basic"}, {label = "test"}] would yield {basic = ["basic", "basic"], test = ["test"]}
16+
subnet_labels_tmp = { for subnet in var.subnets: subnet.label => subnet.label... }
17+
# creates an object where the key is the label and the value is number of times the label appears in the original list
18+
# e.g. {basic = ["basic", "basic"], test = ["test"]} would yield {basic = 2, test = 1}
19+
subnet_label_counts = length(var.subnets) > 0 ? [ for val in local.distinct_subnet_labels:
20+
{
21+
label = val
22+
count = length(local.subnet_labels_tmp[val])
23+
} ] : [ {
24+
label = "default"
25+
count = local.subnet_count
26+
} ]
1327
}
1428

1529
resource null_resource print_names {
1630
provisioner "local-exec" {
1731
command = "echo 'Resource group: ${var.resource_group_name}'"
1832
}
33+
provisioner "local-exec" {
34+
command = "echo 'Subnets: ${jsonencode(local.subnet_labels_tmp)}'"
35+
}
1936
}
2037

2138
data ibm_resource_group resource_group {
@@ -31,7 +48,7 @@ resource ibm_is_vpc vpc {
3148
}
3249

3350
resource ibm_is_public_gateway vpc_gateway {
34-
count = var.public_gateway ? min(local.zone_count, var.subnet_count) : 0
51+
count = var.public_gateway ? min(local.zone_count, local.subnet_count) : 0
3552

3653
name = "${local.vpc_name}-gateway-${format("%02s", count.index)}"
3754
vpc = local.vpc_id
@@ -66,7 +83,7 @@ resource ibm_is_network_acl network_acl {
6683
}
6784

6885
resource ibm_is_subnet vpc_subnet {
69-
count = var.subnet_count
86+
count = local.subnet_count
7087

7188
name = "${local.vpc_name}-subnet-${format("%02s", count.index)}"
7289
zone = local.vpc_zone_names[count.index]
@@ -78,7 +95,7 @@ resource ibm_is_subnet vpc_subnet {
7895
}
7996

8097
resource ibm_is_security_group_rule rule_tcp_k8s {
81-
count = var.subnet_count
98+
count = local.subnet_count
8299

83100
group = local.security_group_id
84101
direction = "inbound"

module.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ versions:
2525
- name: ibmcloud_api_key
2626
scope: global
2727
- name: subnet_count
28+
scope: ignore
29+
- name: subnets
2830
scope: module
2931
- name: public_gateway
3032
scope: module

outputs.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ output "id" {
1212
}
1313

1414
output "subnet_count" {
15-
value = var.subnet_count
16-
description = "The number of subnets for the vpc"
15+
value = local.subnet_count
16+
description = "The total number of subnets for the vpc"
17+
}
18+
19+
output "subnet_label_counts" {
20+
value = local.subnet_label_counts
21+
description = "The number of subnets for each label. e.g. [{label = 'default', count = 2}, {label = 'test', count = 1}]"
1722
}
1823

1924
output "zone_names" {
@@ -27,3 +32,15 @@ output "subnet_ids" {
2732
depends_on = [ibm_is_subnet.vpc_subnet]
2833
description = "The list of subnet ids"
2934
}
35+
36+
output "subnets" {
37+
value = [
38+
for subnet in ibm_is_subnet.vpc_subnet:
39+
{
40+
id = subnet.id
41+
label = length(var.subnets) > 0 ? var.subnets[index(ibm_is_subnet.vpc_subnet, subnet)].label : "default"
42+
}
43+
]
44+
depends_on = [ibm_is_subnet.vpc_subnet]
45+
description = "List of subnet objects that contain the subnet id and label, e.g. [{label='', id=''}]"
46+
}

test/stages/print-module/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
locals {
2+
subnet_label_counts_file = "${path.cwd}/subnet_label_counts.json"
3+
subnets_file = "${path.cwd}/subnets.json"
4+
}
5+
6+
resource local_file subnet_label_counts {
7+
filename = local.subnet_label_counts_file
8+
9+
content = jsonencode(var.subnet_label_counts)
10+
}
11+
12+
resource local_file subnets {
13+
filename = local.subnets_file
14+
15+
content = jsonencode(var.subnets)
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
variable "subnet_label_counts" {
3+
type = list(object({
4+
label = string
5+
count = number
6+
}))
7+
}
8+
9+
variable "subnets" {
10+
type = list(object({
11+
id = string
12+
label = string
13+
}))
14+
}

test/stages/stage1-resource-group.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ module "resource_group" {
22
source = "github.com/cloud-native-toolkit/terraform-ibm-resource-group.git"
33

44
resource_group_name = var.resource_group_name
5-
provision = false
5+
provision = true
66
}

test/stages/stage2-vpc.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module "dev_vpc" {
66
name_prefix = var.name_prefix
77
ibmcloud_api_key = var.ibmcloud_api_key
88
subnet_count = var.vpc_subnet_count
9+
subnets = jsondecode(var.vpc_subnets)
910
public_gateway = var.vpc_public_gateway == "true"
1011
}

test/stages/stage3-print.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module "print-result" {
2+
source = "./print-module"
3+
4+
subnet_label_counts = module.dev_vpc.subnet_label_counts
5+
subnets = module.dev_vpc.subnets
6+
}

0 commit comments

Comments
 (0)