-
Notifications
You must be signed in to change notification settings - Fork 211
chore: Add custom plan modifier for RequestOnlyRequiredOnCreate Attribute #3977
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
Changes from all commits
0580dc2
7e958de
9c0db50
7b9d1db
2459114
e09468c
7b3a175
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||
| package customplanmodifier | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "fmt" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework/attr" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||||||
| ) | ||||||
|
|
||||||
| // RequestOnlyRequiredOnCreate returns a plan modifier that fails planning if the value is | ||||||
| // missing (null/unknown) during create (i.e., when state is null), but allows omission on read/import. | ||||||
| func RequestOnlyRequiredOnCreate() RequestOnlyRequiredOnCreateModifier { | ||||||
| return &requestOnlyRequiredOnCreateAttributePlanModifier{} | ||||||
| } | ||||||
|
|
||||||
| // Single interface so the modifier can be applied to any attribute type. | ||||||
| type RequestOnlyRequiredOnCreateModifier interface { | ||||||
| planmodifier.String | ||||||
| planmodifier.Bool | ||||||
| planmodifier.Int64 | ||||||
| planmodifier.Float64 | ||||||
| planmodifier.Number | ||||||
| planmodifier.List | ||||||
| planmodifier.Map | ||||||
| planmodifier.Set | ||||||
| planmodifier.Object | ||||||
| } | ||||||
|
|
||||||
| type requestOnlyRequiredOnCreateAttributePlanModifier struct{} | ||||||
|
|
||||||
| func (m *requestOnlyRequiredOnCreateAttributePlanModifier) Description(ctx context.Context) string { | ||||||
| return m.MarkdownDescription(ctx) | ||||||
| } | ||||||
|
|
||||||
| func (m *requestOnlyRequiredOnCreateAttributePlanModifier) MarkdownDescription(ctx context.Context) string { | ||||||
| return "Ensures that create operations fail when attempting to create a resource with a missing required attribute." | ||||||
|
||||||
| return "Ensures that create operations fail when attempting to create a resource with a missing required attribute." | |
| return "Validates that the attribute is provided during resource creation, but allows it to be omitted during import operations." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also throw an error if the user tries to update it? Or is the expectation to always use it in conjunction with customplanmodifier.CreateOnly()?
How is it handled during resource reads? I assume the attribute would still stay in the state even though the GET calls don't return it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user tries to update it, customplanmodifier.CreateOnly() is responsible for blocking the change and surfacing an error. RequestOnlyRequiredOnCreate only enforces that the attribute is present on create, so for attributes that are both required-on-create and immutable we expect to always use it in conjunction with CreateOnly(). However, an attribute could be CreateOnly, without RequestOnlyRequiredOnCreate.
Regarding your second question: you're correct. On resource reads, the attribute stays in the state, so it retains its last known value.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe such a value can only be null/non-null but never unknown right? Since these would be Optional attributes? If yes, I'd remove the Known check in the validation method & only check for null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true yes, addressed in 7b3a175