hashicorp / terraform-provider-external

Utility provider that exists to provide an interface between Terraform and external programs. Useful for integrating Terraform with a system for which a first-class provider does not exist.
https://registry.terraform.io/providers/hashicorp/external/latest
Mozilla Public License 2.0
179 stars 49 forks source link

Valid JSON output throws error: "json: cannot unmarshal object into Go value of type string" #339

Closed dacamposol closed 3 months ago

dacamposol commented 3 months ago

Terraform CLI and Provider Versions

Terraform v1.5.7 on darwin_arm64

Terraform Configuration

data "external" "ias-application" {
  program = ["bash", "${path.module}/scripts/get_ias_application.sh"]
  query   = {
    tenant   = var.ias_tenant.domain
    username = var.ias_credentials.username
    password = var.ias_credentials.password
    appname  = format("My Application")
  }
}

Expected Behavior

That data.external.ias-application.result contains the unmarshaled object.

Actual Behavior

│ Error: Unexpected External Program Results │ │ with module.joule["development"].data.external.ias-application, │ on modules/joule/main.tf line 77, in data "external" "ias-application": │ 77: program = ["bash", "${path.module}/scripts/get_ias_application.sh"] │ │ The data source received unexpected results after executing the program. │ │ Program output must be a JSON encoded map of string keys and string values. │ │ If the error is unclear, the output can be viewed by enabling Terraform's logging at TRACE level. Terraform documentation on logging: https://www.terraform.io/internals/debugging │ │ Program: /bin/bash │ Result Error: json: cannot unmarshal object into Go value of type string

Steps to Reproduce

  1. Run terraform plan
  2. See the error.

How much impact is this issue causing?

High

Logs

https://gist.github.com/dacamposol/5966e3579846dd0d00ed321c5cf729eb

Additional Information

As it can be seen, in L3963-3991 from the attached GitHub Gist, the output is a valid JSON, as it's the output from piping jq to a cURL command.

Code of Conduct

dacamposol commented 3 months ago

Reading further the documentation, I found that the values of the resulting JSON must be strings, so nested objects aren't valid in this case.

The program must then produce a valid JSON object on stdout, which will be used to populate the result attribute exported to the rest of the Terraform configuration. This JSON object must again have all of its values as strings.

While that would be an useful scenario, it's not exactly a bug, so I'm closing this.