Search code examples
kubernetesterraformterraform-provider-awsterraform-provider-azureterraform-provider-github

How to handle unsupported attribute in Terraform


I have a terraform code as given below

   locals {
    file_path = format("%s-%s", var.test1, var.test2)
    test_decode  =  yamldecode((data.github_repository_file.test.content))
   }
    data "github_repository_file" "test" {
     repository = "test-repo"
     branch     = "develop"
    file       = "${local.file_path}/local/test.yaml"
    }
  test_encode = ${yamlencode(local.test_decode.spec.names)}

This is working fine when a ".spec.names" attribute present in the test.yaml file. Since we are selecting the test.yaml based on local.file_path some times attribute .spec.names might not present in the test.yaml and the plan failing with "Error: Unsupported attribute". How to check ".spec.names" attribute present in the test.yaml?

Updating the question to add yaml example

Yaml with names attribute

apiVersion: helm.toolkit.gitops.io/v2beta1
kind: HelmRelease
metadata:
  name: "test"
  namespace: "test-system"
spec:
  chart:
    spec:
      chart: "test-environment"
      version: "0.1.10"
  names:
    key1: "value1"
    key2: "value2"
    key3: "value3"
    key4: "value4"

YAML without names attribute

apiVersion: helm.toolkit.gitops.io/v2beta1
kind: HelmRelease
metadata:
  name: "test"
  namespace: "test-system"
spec:
  chart:
    spec:
      chart: "test-environment"
      version: "0.1.10"

Solution

  • You can use try:

      test_encode = yamlencode(try(local.test_decode.spec.names, "some_default_value"))