Search code examples
terraformterraform-provider-oci

how to create a terraform filter on a data source with wildcards


I wanted to know if terraform filters can use wildcards , I heard about regex hacks online but I'm not sure it's legit.

I have a simple example

variable vcn_prefix {
  default = "vcn-spoke"
}

### data source filtering through 7 possible resource found
data "oci_core_vcns" "spoke_vcns" {
  compartment_id = var.network_comp_id
        
  filter {
    name   = "display_name"
    values = ["${var.vcn_prefix}*"]
  }
}

 output "vcn_names" {
    value = data.oci_core_vcns.spoke_vcns.virtual_networks.*.display_name
} 

Here is the output which shows an empty result with no errors.

  + vcn_names   = {
      + compartment_id   = "ocid1.compartment.oc1..xxx"
      + display_name     = null
      + filter           = [
          + {
              + name   = "display_name"
              + regex  = false
              + values = [
                  + "vcn-spoke*",
                ]
            },
        ]
      + id               = "CoreVcnsDataSource-1760906525"
      + state            = null
      + virtual_networks = [] <--------------- EMPTY
    }

It is possible or I'm just too wishful in this pseudocode?


Solution

  • The interpretation of the arguments in this data block is decided entirely by the oracle/oci provider; Terraform Core is not involved in defining the meaning of these arguments.

    From reading the source code of this provider, it seems that the filters are being applied client-side after the provider has fetched the data from the remote API, and so the interpretation of these arguments is determined by their ApplyFilters function.

    According to that code, it seems that by default the provider performs exact string matching, but you can opt in to regular expression interpretation by setting regex = true:

      filter {
        name   = "display_name"
        values = ["${var.vcn_prefix}.*"]
        regex  = true
      }
    

    Notice that the regular expression syntax requires writing .* rather than just *, because in this provider's regular expression language (which is really the Go standard library regular expression language) * means "zero or more repetitions of the preceding token", while . means "any character". .* together therefore means "zero or more repetitions of any character".