I have a CSV and JSON data to parse the input values to the Terraform main file. Here I can able to parse the data using JSON but not with CSV

喜欢而已 提交于 2020-05-24 07:30:50

问题


I have a azure_subnet snippet to create resources like below.

locals {
    csvlist = fileexists(var.csvfile) ? csvdecode(file(var.csvfile)) : []
    subnets = length(var.subnets) > 0 ? var.subnets : local.csvlist
}

resource "azurerm_subnet" "subnet" {
  lifecycle {
        ignore_changes = [network_security_group_id]
  }
  count                   = length(local.subnets) > 0 && var.create ? length(local.subnets) : 0
  name                    = local.subnets[count.index].name
  resource_group_name     = var.resource_group
  virtual_network_name    = var.vnet
  address_prefix          = local.subnets[count.index].address_prefix
  dynamic "delegation" {
    for_each = local.subnets[count.index].delegations
    content{
      name = delegation.value["delegation_name"]

      service_delegation {
        name    = delegation.value["service_delegation_name"]
        actions = delegation.value["service_delegation_actions"]
      }
    }
  }
  service_endpoints       = [lookup(local.subnets[count.index],"endpoints")] == [""] ? [] : flatten([split(",",lookup(local.subnets[count.index],"endpoints"))])
}

Here, I can create the resources based on the JSON input like below

subnets = [{
    name = "Active_Directory"
    address_prefix = "10.101.4.128/27"
    endpoints = "Microsoft.EventHub"
    #delegations = []
    delegations = [
        {
            delegation_name = "acctestdelegation"
            service_delegation_name = "Microsoft.ContainerInstance/containerGroups"
            service_delegation_actions = ["Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action"]
        }
    ]
}]

But I can not create the resources based on CSV becuase its looking for the key of "delegations". My CSV will be look like below

name,address_prefix,endpoints,delegation_name,service_delegation_name,service_delegation_actions
Active_Directory,"10.101.4.128/27","Microsoft.EventHub",acctestdelegation,"Microsoft.ContainerInstance/containerGroups","Microsoft.Network/virtualNetworks/subnets/join/action"
sql,"10.101.4.160/27","Microsoft.EventHub",acctestdelegation,"Microsoft.ContainerInstance/containerGroups","Microsoft.Network/virtualNetworks/subnets/join/action"

How should I make CSV to support with my main.tf ? Here, I am planning to provide two options like JSON and CSV. If user wants CSV then they will use the CSV otherwise JSON.

来源:https://stackoverflow.com/questions/60736237/i-have-a-csv-and-json-data-to-parse-the-input-values-to-the-terraform-main-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!