Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Extending Terraform - state representation of TypeSet #1495

Closed
dak1n1 opened this issue Nov 11, 2020 · 2 comments · Fixed by #1796
Closed

Extending Terraform - state representation of TypeSet #1495

dak1n1 opened this issue Nov 11, 2020 · 2 comments · Fixed by #1796
Assignees
Labels
clarification Something could be explained more usefully plugin Docs for extending terraform and writing providers

Comments

@dak1n1
Copy link

dak1n1 commented Nov 11, 2020

I was looking for some documentation to help guide a user through accessing elements of a TypeSet resource attribute, and I found this page that shows the Terraform SDK v1 representation of TypeSet. It will probably need to be updated with the new SDK v2 representation of TypeSet.

This is the page: https://www.terraform.io/docs/extend/schemas/schema-types.html#typeset

The code block shows the index value calculated from a hash:

"ingress.1061987227.cidr_blocks.#": "1",
"ingress.1061987227.cidr_blocks.0": "10.0.0.0/8",

With Terraform SDK v2, it now can be accessed like a list instead:

"ingress.0.cidr_blocks.#": "1",
"ingress.0.cidr_blocks.0": "10.0.0.0/8",

But there might be a better way to represent it, using the TypeSet helper. It would probably be helpful to show users:

  1. How to test TypeSet using the TypeSet helper.
  2. How to access these elements as an end user referencing them in a Terraform config.
@bflad bflad added clarification Something could be explained more usefully plugin Docs for extending terraform and writing providers labels Jul 27, 2021
@bflad bflad self-assigned this Jul 27, 2021
@bflad
Copy link
Contributor

bflad commented Jul 27, 2021

Hey @dak1n1 👋

Broadly speaking, set types in Terraform after Terraform CLI 0.12 are non-indexable by design. It is invalid to attempt to index into a set and Terraform CLI will throw a configuration error.

For practitioners, the choice of how to handle a set typed attribute will be configuration and goal specific. For example, using for_each or for ... if may be appropriate. It is likely better that practitioner configuration examples remain with provider or tutorial documentation, which can give the necessary context for practitioners rather than the provider development documentation making any assertions or recommendations.

I think the best way to describe this from the provider acceptance testing side is that the Terraform CLI 0.12 shims in the Terraform Plugin SDK no longer know how to handle the TypeSet hash (the integer after ingress in the above example) when working in flatmap syntax, which is a bug that will likely not be resolved before the new framework is finished because it might encourage the older, error-prone behavior of indexing into those addresses. As you've probably also run into by now, depending on the "list" indexing in testing is inaccurate after the first element for sets. 😄

It would certainly be good to mention the TypeSet acceptance testing helpers on the TestStep page since that is the most desirable option today. I'm going to leave this open to address that specifically. 👍

bflad added a commit that referenced this issue Jul 28, 2021
Reference: #1495

Call out most common `TestCheckFunc` and add `TypeSet` functions. Also call out list/set/map element count syntax.
bflad added a commit that referenced this issue Jul 28, 2021
Reference: #1495

Call out most common `TestCheckFunc` and add `TypeSet` functions. Also call out list/set/map element count syntax.
@bflad
Copy link
Contributor

bflad commented Jul 28, 2021

Submitted #1796 to revamp that section a bit.

bflad added a commit that referenced this issue Jul 29, 2021
…#1796)

Reference: #1495

Call out most common `TestCheckFunc` and add `TypeSet` functions. Also call out list/set/map element count syntax.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
clarification Something could be explained more usefully plugin Docs for extending terraform and writing providers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants