Repository navigation
feature(ske): Show a warning if an attribute is set without being enabled #1864
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
Merged
Manuelvaas
merged 7 commits into
main
from
feature/throw-warning-if-ske-extensions-are-disabled
Nov 5, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b021bd8
feature(ske): Show a warning if an attribute is set without being ena…
Manuelvaas a0782bf
add tests
Manuelvaas 3f0f9fc
moved out of ske module
Manuelvaas 1d0440a
moved string validators
Manuelvaas 3be136b
fixed deprecation date
Manuelvaas 2c2405a
remove generic
Manuelvaas d338bfe
Merge branch 'main' into feature/throw-warning-if-ske-extensions-are-…
Manuelvaas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package validate | ||
|
|
||
| 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/schema/validator" | ||
| ) | ||
|
|
||
| // Bool validator factory | ||
| type BoolValidator struct { | ||
| description string | ||
| markdownDescription string | ||
| validate BoolValidationFn | ||
| } | ||
|
|
||
| type BoolValidationFn func(context.Context, validator.BoolRequest, *validator.BoolResponse) | ||
|
|
||
| var _ = validator.Bool(&BoolValidator{}) | ||
|
|
||
| func (v *BoolValidator) Description(_ context.Context) string { | ||
| return v.description | ||
| } | ||
|
|
||
| func (v *BoolValidator) MarkdownDescription(_ context.Context) string { | ||
| return v.markdownDescription | ||
| } | ||
|
|
||
| func (v *BoolValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) { // nolint:gocritic // function signature required by Terraform | ||
| if req.ConfigValue.IsUnknown() { | ||
| return | ||
| } | ||
| v.validate(ctx, req, resp) | ||
| } | ||
|
|
||
| // "enables" returns a bool validator that prevents another attribute from being set if | ||
| // the own value is not true or not set. This attributed enables another attribute. | ||
| func enables(target path.Expression, callback func(*diag.Diagnostics, path.Path, path.Path)) *BoolValidator { | ||
| description := "this attribute enables another attribute to be set by being true" | ||
|
|
||
| return &BoolValidator{ | ||
| description: description, | ||
| validate: func(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) { | ||
| expression := req.PathExpression.Merge(target) | ||
|
|
||
| matchedPaths, diags := req.Config.PathMatches(ctx, expression) | ||
| resp.Diagnostics.Append(diags...) | ||
|
|
||
| for _, targetPath := range matchedPaths { | ||
| var target attr.Value | ||
|
|
||
| diags := req.Config.GetAttribute(ctx, targetPath, &target) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| if target.IsNull() || target.IsUnknown() { | ||
| continue | ||
| } | ||
|
|
||
| if req.ConfigValue.IsNull() || !req.ConfigValue.ValueBool() { | ||
| callback(&resp.Diagnostics, req.Path, targetPath) | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Returns a bool validator based on "enables", which throws a deprecation warning instead of an error | ||
| func EnablesElseDeprecationWarning(target path.Expression, deprecationDate string) *BoolValidator { | ||
| return enables(target, func(diagnostics *diag.Diagnostics, ownPath path.Path, targetPath path.Path) { | ||
| diagnostics.AddAttributeWarning( | ||
| targetPath, | ||
| "Deprecation: Attribute can not be set", | ||
| fmt.Sprintf("This attribute %q can only be configured when %q is true. Will be enforced after %s.", targetPath.String(), ownPath.String(), deprecationDate), | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| // Returns a bool validator based on "enables", which throws an error | ||
| func EnablesElseError(target path.Expression) *BoolValidator { | ||
| return enables(target, func(diagnostics *diag.Diagnostics, ownPath path.Path, targetPath path.Path) { | ||
| diagnostics.AddAttributeError( | ||
| targetPath, | ||
| "Attribute can not be set", | ||
| fmt.Sprintf("This attribute %q can only be configured when %q is true.", targetPath.String(), ownPath.String()), | ||
| ) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.