-
Notifications
You must be signed in to change notification settings - Fork 21
Set attribute defaults conditionally #259
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
Open
wfdewith
wants to merge
9
commits into
lxc:main
Choose a base branch
from
wfdewith:conditional-default
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b6b382
ci: Configure core.storage_buckets_address
wfdewith 6e816db
common: Add SetDefaultIfAllUndefined plan modifier
wfdewith 696c96c
storage_volume: Set attribute defaults conditionally
wfdewith 7882992
tests: Add tests for importing non-default storage volumes
wfdewith 863b720
storage_bucket: Set attribute defaults conditionally
wfdewith ebaa074
tests: Add test for importing storage buckets from file
wfdewith 35562bd
instance: Set attribute defaults conditionally
wfdewith 1c9dee3
tests: Add non-default property to source instances
wfdewith be031cd
tests: Add instance test with source file and file upload
wfdewith 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,142 @@ | ||
package common | ||
|
||
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" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func SetDefaultStringIfAllUndefined(defaultValue types.String, expression ...path.Expression) planmodifier.String { | ||
return setDefaultStringIfAllUndefinedModifier{ | ||
defaultValue: defaultValue, | ||
expressions: expression, | ||
} | ||
} | ||
|
||
func SetDefaultMapIfAllUndefined(defaultValue types.Map, expression ...path.Expression) planmodifier.Map { | ||
return setDefaultMapIfAllUndefinedModifier{ | ||
defaultValue: defaultValue, | ||
expressions: expression, | ||
} | ||
} | ||
|
||
type setDefaultStringIfAllUndefinedModifier struct { | ||
defaultValue types.String | ||
expressions path.Expressions | ||
} | ||
|
||
func (m setDefaultStringIfAllUndefinedModifier) Description(ctx context.Context) string { | ||
return fmt.Sprintf("Sets the default value %q only if none the following are set: %q", m.defaultValue.String(), m.expressions) | ||
} | ||
|
||
func (m setDefaultStringIfAllUndefinedModifier) MarkdownDescription(ctx context.Context) string { | ||
return m.Description(ctx) | ||
} | ||
|
||
func (m setDefaultStringIfAllUndefinedModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { | ||
if !req.ConfigValue.IsNull() && !req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
expressions := req.PathExpression.MergeExpressions(m.expressions...) | ||
allUndefinedReq := allUndefinedRequest{ | ||
expressions: expressions, | ||
config: req.Config, | ||
} | ||
allUndefinedResp := &allUndefinedResponse{} | ||
|
||
allUndefined(ctx, allUndefinedReq, allUndefinedResp) | ||
|
||
resp.Diagnostics.Append(allUndefinedResp.diagnostics...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if allUndefinedResp.allUndefined { | ||
resp.PlanValue = m.defaultValue | ||
} | ||
} | ||
|
||
type setDefaultMapIfAllUndefinedModifier struct { | ||
defaultValue types.Map | ||
expressions path.Expressions | ||
} | ||
|
||
func (m setDefaultMapIfAllUndefinedModifier) Description(ctx context.Context) string { | ||
return fmt.Sprintf("Sets the default value %q only if none the following are set: %q", m.defaultValue.String(), m.expressions) | ||
} | ||
|
||
func (m setDefaultMapIfAllUndefinedModifier) MarkdownDescription(ctx context.Context) string { | ||
return m.Description(ctx) | ||
} | ||
|
||
func (m setDefaultMapIfAllUndefinedModifier) PlanModifyMap(ctx context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) { | ||
if !req.ConfigValue.IsNull() && !req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
expressions := req.PathExpression.MergeExpressions(m.expressions...) | ||
allUndefinedReq := allUndefinedRequest{ | ||
expressions: expressions, | ||
config: req.Config, | ||
} | ||
allUndefinedResp := &allUndefinedResponse{} | ||
|
||
allUndefined(ctx, allUndefinedReq, allUndefinedResp) | ||
|
||
resp.Diagnostics.Append(allUndefinedResp.diagnostics...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if allUndefinedResp.allUndefined { | ||
resp.PlanValue = m.defaultValue | ||
} | ||
} | ||
|
||
type allUndefinedRequest struct { | ||
expressions path.Expressions | ||
config tfsdk.Config | ||
} | ||
|
||
type allUndefinedResponse struct { | ||
diagnostics diag.Diagnostics | ||
allUndefined bool | ||
} | ||
|
||
func allUndefined(ctx context.Context, req allUndefinedRequest, resp *allUndefinedResponse) { | ||
resp.allUndefined = true | ||
for _, expression := range req.expressions { | ||
matchedPaths, diags := req.config.PathMatches(ctx, expression) | ||
|
||
resp.diagnostics.Append(diags...) | ||
|
||
if diags.HasError() { | ||
continue | ||
} | ||
|
||
for _, mp := range matchedPaths { | ||
var mpVal attr.Value | ||
diags := req.config.GetAttribute(ctx, mp, &mpVal) | ||
resp.diagnostics.Append(diags...) | ||
|
||
if diags.HasError() { | ||
continue | ||
} | ||
|
||
if mpVal.IsUnknown() { | ||
continue | ||
} | ||
|
||
if !mpVal.IsNull() { | ||
resp.allUndefined = false | ||
} | ||
} | ||
} | ||
} |
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
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.