hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.66k stars 9.41k forks source link

Remove jsondecode function used in mocks examples #35249

Open dancer1325 opened 1 month ago

dancer1325 commented 1 month ago

Terraform Version

v1.7.0+

Affected Pages

https://developer.hashicorp.com/terraform/language/tests/mocking#overrides-syntax

What is the docs issue?

In the examples provided to support the explanation, you can find

# ./modules/s3_data/main.tf
variable "data_bucket_name" {
  type = string
}
data "aws_s3_object" "data_bucket" {
  bucket = var.data_bucket_name
  key    = "credentials.json"
}
output "data" {
  value = jsondecode(data.aws_s3_object.data_bucket.body)
}

Once you run terraform test you get an error in some of the previous tests defined something like

data.aws_s3_object.data_bucket.body is ....Call to function "jsondecode" failed: extraneous data after JSON object."

because 'data.aws_s3_object.data_bucket.body' is already a JSON string -> unneeded jsondecode

Proposal

I would suggest to remove jsondecode

variable "data_bucket_name" {
  type = string
}
data "aws_s3_object" "data_bucket" {
  bucket = var.data_bucket_name
  key    = "credentials.json"
}
output "data" {
  value = data.aws_s3_object.data_bucket.body
}

References

No response

crw commented 1 month ago

Thanks for this report!