Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document behavior/validation limitations of nested attributes vs blocks #810

Closed
asaba-hashi opened this issue Jul 25, 2023 · 4 comments
Closed
Labels
documentation Improvements or additions to documentation

Comments

@asaba-hashi
Copy link

Module version

latest

Use-cases

When creating a terraform provider, the current document suggests the blocks should not be used:

Use nested attributes for new schema implementations. Block support is mainly for migrating prior SDK-based providers.

However, there are some validation differences:

type Things struct {
	A     types.Bool `tfsdk:"A"`
	B types.Bool `tfsdk:"B"`
}

type SomeResource struct {
   Things things
}

<snip>
"things": schema.SingleNestedAttribute{
				Attributes: map[string]schema.Attribute{
					"B": schema.BoolAttribute{
						Default:  booldefault.StaticBool(false),
						Optional: true,
						Computed: true,
					},
					"B": schema.BoolAttribute{
						Default:  booldefault.StaticBool(false),
						Optional: true,
						Computed: true,
					},
}
required: true,
}

resource "some_resource" "some_resource_name" {

  things = {
     A = true
     want_this_to_fail_validation = "but doesn't"
  }
}

Attempted Solutions

Reported as a bug here in #805 to terraform in hashicorp/terraform#33570, however, this can't be addressed in the near term for compatibility: hashicorp/terraform#33570 (comment)

Proposal

Documentation should be updated to include notes on the validation behavior of blocks compared to nested attributes.

References

@asaba-hashi asaba-hashi added the enhancement New feature or request label Jul 25, 2023
@bflad bflad added documentation Improvements or additions to documentation and removed enhancement New feature or request labels Jul 27, 2023
@bflad
Copy link
Member

bflad commented Jul 27, 2023

While adding documentation around this is likely valid in some form, since it is an explicit behavior of Terraform, I believe we need to be careful about not confusing provider developers further. It is awkward for provider developers to choose how they believe practitioners should have to design their configurations since working with blocks is explicitly much harder and working with attributes is much easier. This really feels like a concern to be solved in the configuration validation/linting space, if a practitioner feels it is important. Provider developers should not be burdened with concerns outside of defining what is valid for their data input.

If you have proposals for specific wording and/or placement of that wording, it is certainly welcome feedback.

@asaba-hashi
Copy link
Author

Based on the discussion of tradeoffs in hashicorp/terraform#33570, my suggestion would be to add a table to the nested attribute docs and reference it from the block docs, with the tradeoffs discussed listed:

|Nested Attributes | Blocks |
|------------------|--------|
|Easy|Hard|
|Can't validate extra fields/typos in optional fields| .... |
| ... | May require use of dynamic_blocks |

etc.

@bflad
Copy link
Member

bflad commented Aug 2, 2023

The maintainers have discussed this further with the Terraform core team and we all have decided that adding provider developer documentation that seems to discourage nested attributes usage is not preferable. While there is inherent risk that an attribute name typo may be introduced in a practitioner configuration, the resource plan should convey what data values are correctly being handled and the configuration benefits/ease for practitioners is generally worth the tradeoff. To that end, the preference here will be to leave the documentation as-is and monitor for similar developer reports to discover whether additional documentation in this area is warranted. Thank you for raising this. 👍

@bflad bflad closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2023
@github-actions
Copy link

github-actions bot commented Sep 2, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants