Datta
Datta

Reputation: 149

Terraform multi for loops

I need to use multiple for loop in tf. Here is my code

data "oci_identity_availability_domains" "ad" {
          compartment_id = var.tenancy_ocid
        }
    
output ad {
      value = data.oci_identity_availability_domains.ad.availability_domains[*].name
    }

output of ad: ad = [ "abi:PHX-AD-1", "abi:PHX-AD-2", "abi:PHX-AD-3", ]


my json file:

{
    "compute" : [
        {
        "service" : "standard-e3-core-ad-count",
        "value" : 10000
        },        
        {
        "service" : "standard-e3-memory-count",
        "value" : 10000
        }
    ]
}

I need to use ad in the availability_domain

 local.info = jsondecode(file("${path.module}/info.json"))

data "oci_limits_limit_values" "count" {
    for_each = {
      for index, value in flatten([
        for name, value in local.info : [
          for i in value : { name : name, service : i.serice }
        ]
      ]) : index => value
    }
  
    compartment_id      = var.tenancy_ocid
    service_name        = each.value.service_name
    name                = each.value.limit_name
    **availability_domain =  { I need to run this for all 3 AD}**
  }

I'm stuck on availability_domain. The value will be one or 3. It's based on the data.oci_identity_availability_domains.ad.availability_domains[*].name. Any suggestions?

Upvotes: 1

Views: 1403

Answers (1)

Marcin
Marcin

Reputation: 238847

From what I can see, you can create a flattened data structure, for example:

locals {
    ad_info_product = merge([
          for ad in data.oci_identity_availability_domains.ad:
            {
              for compute in local.info["compute"]: 
                  "${ad}-${compute.service}" => {
                      ad = ad
                      service = compute["service"]
                      limit =  compute["value"]
                  }
            }
        ]...) # please do NOT remove the dots
}

then:

data "oci_limits_limit_values" "count" {

    for_each            = local.ad_info_product
  
    compartment_id      = var.tenancy_ocid

    service_name        = each.value["service"]
    name                = each.value["limit"]
    availability_domain = each.value["ad"]
}

Upvotes: 1

Related Questions