11
22locals  {
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        =  . 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             =  . vpc . id 
99  subnet_ids         =  . vpc_subnet [* ]. id 
1010  gateway_ids        =  . public_gateway  ?  ibm_is_public_gateway. vpc_gateway [* ]. id  :  [ for  val  in  range (local. zone_count ):  " " 
1111  security_group_id  =  . vpc . default_security_group 
1212  ipv4_cidr_blocks   =  . 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
1529resource  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
2138data  ibm_resource_group  resource_group  {
@@ -31,7 +48,7 @@ resource ibm_is_vpc vpc {
3148}
3249
3350resource  ibm_is_public_gateway  vpc_gateway  {
34-   count  =  . public_gateway  ?  min (local. zone_count , var . subnet_count ) :  0 
51+   count  =  . public_gateway  ?  min (local. zone_count , local . subnet_count ) :  0 
3552
3653  name            =  " ${ local . vpc_name } -gateway-${ format (" %02s" . index )} " 
3754  vpc             =  . vpc_id 
@@ -66,7 +83,7 @@ resource ibm_is_network_acl network_acl {
6683}
6784
6885resource  ibm_is_subnet  vpc_subnet  {
69-   count                     =  var . subnet_count 
86+   count                     =  local . subnet_count 
7087
7188  name                      =  " ${ local . vpc_name } -subnet-${ format (" %02s" . index )} " 
7289  zone                      =  . vpc_zone_names [count . index ]
@@ -78,7 +95,7 @@ resource ibm_is_subnet vpc_subnet {
7895}
7996
8097resource  ibm_is_security_group_rule  rule_tcp_k8s  {
81-   count      =  var . subnet_count 
98+   count      =  local . subnet_count 
8299
83100  group      =  . security_group_id 
84101  direction  =  " inbound" 
0 commit comments