0
votes

If you can help me a nested dynamic group I am trying to solve, I have the values coming from a json like below

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

and dynamic code snippet like this,

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"]
      }
     }

problem I am facing is, name, priority and algorithm all get picked up correctly, but I am not able to select fqdn (host value from json) and enabled (enabled from json) from the second layer in json

Error message I get at terraform plan is,

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 25, in resource "avi_gslbservice" "avi_gslbservice":
  25:               fqdn = members.value["host"]
    ├────────────────
    │ members.value is "false"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 25, in resource "avi_gslbservice" "avi_gslbservice":
  25:               fqdn = members.value["host"]
    ├────────────────
    │ members.value is "url2"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 25, in resource "avi_gslbservice" "avi_gslbservice":
  25:               fqdn = members.value["host"]
    ├────────────────
    │ members.value is "false"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 25, in resource "avi_gslbservice" "avi_gslbservice":
  25:               fqdn = members.value["host"]
    ├────────────────
    │ members.value is "url1"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 29, in resource "avi_gslbservice" "avi_gslbservice":
  29:               enabled = members.value["enabled"]
    ├────────────────
    │ members.value is "false"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 29, in resource "avi_gslbservice" "avi_gslbservice":
  29:               enabled = members.value["enabled"]
    ├────────────────
    │ members.value is "url2"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 29, in resource "avi_gslbservice" "avi_gslbservice":
  29:               enabled = members.value["enabled"]
    ├────────────────
    │ members.value is "false"

This value does not have any indices.

Error: Invalid index

  on modules\avi\avi_gslbservice.tf line 29, in resource "avi_gslbservice" "avi_gslbservice":
  29:               enabled = members.value["enabled"]
    ├────────────────
    │ members.value is "url1"

This value does not have any indices.
1
Can you provide the error message?Jordan
I added error in the original postuser2068179

1 Answers

3
votes

There is nothing to iterate over your in your avi_gslbservice_groups_ip, thus you get the error. You should only have one level of dynamic:

 dynamic "groups" {   
   for_each = var.avi_gslbservice_groups

    content {
        ip {
             type = "V4"
             addr = ""
        }
        fqdn = groups.value.avi_gslbservice_groups_ip["host"]
        vs_uuid = ""
        cluster_uuid = ""
        ratio = 1
        enabled = groups.value.avi_gslbservice_groups_ip["enabled"]

        name = groups.value["name"]
        priority = groups.value["priority"]
        algorithm = groups.value["algorithm"]
    }
  }

Thank you , that was big help. That resolved the issue. Small correction was needed on the content block as below,

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

Also for people who want to have multiple host entries added in a single group can format the json as below, and use my original post's nested dynamic block. In below even you could provide single host within [] and still original post's nested dynamic block would work.

{
    "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"
                }
            ]
        }
    ]
}