terraform-plugin-sdk icon indicating copy to clipboard operation
terraform-plugin-sdk copied to clipboard

Get attribute of TypeSet from terraform.State for tests

Open tylerFowler opened this issue 1 year ago • 3 comments

Currently there seems to be no way to retrieve an attribute (specifically a computed attribute but not limited to that) of type schema.TypeSet as a schema.Set given an object of type terraform.State.

Use-cases

The Set methods would be highly useful for use in resource.TestCheckFuncs and result in cleaner, slimmer test check functions.

tylerFowler avatar Feb 25 '23 00:02 tylerFowler

Hi @tylerFowler 👋 Thank you for raising this feature request.

It would be super helpful if you could discuss more about what you are looking to check in your testing?

If you haven't seen already, there are already a few built-in TypeSet specific TestCheckFunc available in the helper/resource package already, such as TestCheckTypeSetElemAttr and TestCheckTypeSetElemNestedAttrs. For checking a set contains expected values, these should cover most use cases.

bflad avatar Feb 28 '23 21:02 bflad

Yeah, I was thinking more in the vein of custom test check funcs, where I want to simply range over set items in a custom function, perhaps calling APIs for each one to verify state, etc...

tylerFowler avatar Mar 01 '23 02:03 tylerFowler

Thank you for that additional context, @tylerFowler.

The baked in terraform.State for TestCheckFunc usage is a vestige from Terraform 0.11 and earlier, where the testing code re-implemented logic from the core Terraform logic and the type system was very limited. I would imagine that it is more likely (due to the effort versus gains) that the testing code would provide a different type of abstraction (e.g. one from the state JSON) rather than refactor/enhance the old Terraform state code specifically for terraform-plugin-sdk concepts. We recently introduced the terraform-plugin-testing Go module and a future major version of that will likely remove terraform-plugin-sdk specifics so it is explicitly generic to work with any Go SDK (terraform-plugin-framework, terraform-plugin-go, terraform-plugin-sdk, or other hypothetical Go SDKs).

Is the API lookup available via data source(s) in the provider? In those cases, either using TestCheckTypeSetElemAttrPair or Terraform configuration postcondition checks may be possible solutions that exist today, but tackle the use case in a different manner.

bflad avatar Mar 02 '23 22:03 bflad