terraformavi

how to implement for_each in terraform avi gslb to create and attach multiple pool to gslb?


I have below which creates avi gslbservice with a single pool created and attached to it. I would like to create a second pool created and attached to it. Can any one please guide? I am new to terraform, I saw few tutorials on for_each fn. But not able to figure out, how to apply it for my need. I have highlighted the block which create the gslb pool

resource "avi_gslbservice" "avi_gslbservice" {
name = "helloworldssl-gslb"
tenant_ref = data.avi_tenant.avi_tenant.id
domain_names = ["xxxxxxxxx"]
health_monitor_refs = [avi_healthmonitor.avi_healthmonitor_gslb.id]
enabled = true
pool_algorithm = "GSLB_SERVICE_ALGORITHM_GEO"
ttl = "30"
created_by = "xxxxxx"
description = "xxxxxx"
down_response {
type = "GSLB_SERVICE_DOWN_RESPONSE_ALL_RECORDS"
             }
**groups {
      priority = 10
      members {
              ip {
                type = "V4"
                addr = ""
                 }
         fqdn = "xxxxxxxxxxxxxx"
         vs_uuid = ""
         cluster_uuid = ""
         ratio = 1
         enabled = true
         }              
      name = "helloworldssl-gslb-pool1"
      algorithm = "GSLB_ALGORITHM_TOPOLOGY"
      }**
}

Edit Aug 8th 2021 - For now I have a work around of duplicating whole groups block two times.


Solution

  • Here is how you do it,

    dynamic "groups" {   
       for_each = var.avi_gslbservice_groups
       content {
          dynamic "members" {
            for_each = groups.value.avi_gslbservice_groups_ip
            content {
                ip {
                     type = "V4"
                     addr = ""
                  }
                  fqdn = members.value["host"]
                  vs_uuid = ""
                  cluster_uuid = ""
                  ratio = 1
                  enabled = members.value["enabled"]
                }         
              }                 
            name = groups.value["name"]
            priority = groups.value["priority"]
            algorithm = groups.value["algorithm"]
          }
         }
    

    values will come from json file as below,

    {
        "avi_gslbservice_groups": [
            {
                "name": "us-east-1",
                "priority": 7,
                "algorithm": "GSLB_ALGORITHM_ROUND_ROBIN",
                "avi_gslbservice_groups_ip": [
                    {
                        "host": "host1",
                        "enabled": "true"
                    },
                    {
                        "host": "host2",
                        "enabled": "false"
                    }
                ]
            },
            {
                "name": "us-east-2",
                "priority": 10,
                "algorithm": "GSLB_ALGORITHM_TOPOLOGY",
                "avi_gslbservice_groups_ip": [
                    {
                        "host": "host1",
                        "enabled": "true"
                    },
                    {
                        "host": "host2",
                        "enabled": "false"
                    }
                ]
            }
        ]
    }