docs: make provider schema gen deterministic #86
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes a seemingly non-deterministic issue with provider docs generation, which caused the
customer
attribute to move from Required to Optional.Background
We’ve observed that running go generate produces different results:
https://github.com/observeinc/terraform-provider-observe/blob/master/docs/index.md#schema
Sometimes customer, the one required attribute, is rendered in the Optional section. There's a GitHub Actions workflow that runs go generate && git diff and fails. The user must then revert this (incorrect) change.
So we need to identify the source of this non-determinism.
Analysis
I was able to identify the root cause via rubber ducking with Konstantin. He mentioned that trying a different terminal produced the expected result and that he’d set
OBSERVE_*
credentials in the first one. Then it clicked for me.tfplugindocs
works by using the terraform providers schema command to get a JSON representation of the provider’s schema:https://developer.hashicorp.com/terraform/cli/commands/providers/schema#block-representation
Notably nowhere in this serialized schema is a
Default
. And that’s done because defaults can be computed at runtime. This is used to allow providers to read defaults from environment variables.terraform-provider-observe/observe/provider.go
Line 29 in 0f5a106
And so it turns out that Terraform will return
optional: true
for that attribute.In that sense, the attribute is optional, since an omitted (at least from the configuration) value is permitted.
Solution
Using
env -u
unsets the relevant environment variable. I'd prefer--unset
but that's not portable to macOS's version ofenv
. We could use a script to discover allOBSERVE_*
variables and construct unset arguments, but this would be more than a one-liner. We cannot useenv -i
(prevents all env inheritance) because that breaks things likePATH
which can preventsgo
andterraform
from being found.I also left a comment alongside the attribute itself in hopes that when someone refactors this to
url
or similar instead ofcustomer
anddomain
they'll notice that similar treatment is needed for their new environment variable.Related
https://observe.atlassian.net/browse/OB-26565