问题
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