terraform

Filtering extensive Terraform Data output


is there a way to filter out extensive terraform data source output, that doesn't have "built-in filter"? I am using DNACENTER provider, and specifically this data source.

When I run below:

data "dnacenter_file_namespace_files" "example" {
  provider   = dnacenter
  name_space = "nw_orch"
}

output "dnacenter_file_namespace_files_example" {
  value = data.dnacenter_file_namespace_files.example.items
}

I get output that looks like this:

Changes to Outputs:
  + dnacenter_file_namespace_files_example = {
      + id         = "1720985135"
      + items      = [
          + {
              + attribute_info   = ""
              + download_path    = "/file/a156b130-5c40-4227-a8b9-ab2cd8cc6ed6"
              + encrypted        = ""
              + file_format      = "text/csv"
              + file_size        = "87"
              + id               = "a156b130-5c40-4227-a8b9-ab2cd8cc6ed6"
              + md5_checksum     = "bc37d95c037bbf8f17ff704a9a445694"
              + name             = "site1.csv"
              + name_space       = "nw_orch"
              + sftp_server_list = [
                  + jsonencode(
                        {
                          + createTimeStamp = "05/12/2024 11:44:26"
                          + downloadurl     = "/nw_orch/a156b130-5c40-4227-a8b9-ab2cd8cc6ed6/site1.csv"
                          + id              = "d5bc91e7-58c4-4f65-b865-67e6a26bfa51"
                          + sftpserverid    = "18adcb51-ca12-4a9c-9e55-46e0743ee49e"
                          + status          = "SUCCESS"
                          + updateTimeStamp = "05/12/2024 11:44:26"
                        }
                    ),
                ]
              + sha1_checksum    = "a5b02a92d80850870abbc0a5ac6b169361c0efe9"
              + task_id          = ""
            },
          + {
              + attribute_info   = ""
              + download_path    = "/file/3e2079b8-d9be-4668-8ae2-16345e5b8388"
              + encrypted        = ""
              + file_format      = "text/csv"
              + file_size        = "93"
              + id               = "3e2079b8-d9be-4668-8ae2-16345e5b888"
              + md5_checksum     = "cab5fd8fea4b40e209b346ec76beed92"
              + name             = "site2.csv"
              + name_space       = "nw_orch"
              + sftp_server_list = [
                  + jsonencode(
                        {
                          + createTimeStamp = "07/14/2024 11:57:16"
                          + downloadurl     = "/nw_orch/3e2079b8-d9be-4668-8ae2-16345e5b8388/site2.csv"
                          + id              = "726d6c00-4dbd-4a0f-a8e5-59daba4e3eb4"
                          + sftpserverid    = "18adcb51-ca12-4a9c-9e55-46e0743ee49e"
                          + status          = "SUCCESS"
                          + updateTimeStamp = "07/14/2024 11:57:16"
                        }
                    ),
                ]
              + sha1_checksum    = "d162b5914e9633d16a09830143ffcd5f31a4019d"
              + task_id          = ""
            },
          + {
              + attribute_info   = ""
              + download_path    = "/file/47e0bd91-b415-49e7-aa34-e4879340ea76"
              + encrypted        = ""
              + file_format      = "text/csv"
              + file_size        = "93"
              + id               = "47e0bd91-b415-49e7-aa34-e4879340ea76"
              + md5_checksum     = "cab5fd8fea4b40e209b346ec76beed91"
              + name             = "LP3-lan_auto_device_v2.csv"
              + name_space       = "nw_orch"
              + sftp_server_list = [
                  + jsonencode(
                        {
                          + createTimeStamp = "07/14/2024 12:17:36"
                          + downloadurl     = "/nw_orch/47e0bd91-b415-49e7-aa34-e4879340ea76/LP3-lan_auto_device_v2.csv"
                          + id              = "162046ed-37d9-458a-adaf-3d2b188caf33"
                          + sftpserverid    = "18adcb51-ca12-4a9b-9e55-46e0743ee49e"
                          + status          = "SUCCESS"
                          + updateTimeStamp = "07/14/2024 12:17:36"
                        }
                    ),
                ]
              + sha1_checksum    = "d162b5914e9611d16a09830143ffcd5f31a4018d"
              + task_id          = ""
            },
        ]
      + name_space = "nw_orch"
    }

From this output I am only interested in the id of the file, which has a specific name, e.g. "site1.csv", which has this id "a156b130-5c40-4227-a8b9-ab2cd8cc6ed6". I can export all IDs:

data "dnacenter_file_namespace_files" "example" {
  provider   = dnacenter
  name_space = "nw_orch"
}

output "dnacenter_file_namespace_files_example" {
  value = data.dnacenter_file_namespace_files.example.items.*.id
}

and get something as below.

Changes to Outputs:
  + dnacenter_file_namespace_files_example = [
      + "a156b130-5c40-4227-a8b9-ab2cd8cc6ed6",
      + "47e0bd91-b415-49e7-aa34-e4879340ea76",
    ]

But can I use some filtering, or a condition to get only the ID of a file with a specific name? Thanks!


Solution

  • You should be able to filter output from the data source using the for loop:

    output "dnacenter_file_namespace_files_example" {
      value = [for item in data.dnacenter_file_namespace_files.example.items: item.id if item.name == "site1.csv"]
    }
    

    I can't test it right now for syntax correctness but I hope you will get the idea how it can be done.