-
Notifications
You must be signed in to change notification settings - Fork 114
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
Bump github.com/hashicorp/terraform-plugin-framework from 0.16.0 to 0.17.0 #338
Merged
bendbennett
merged 6 commits into
main
from
dependabot/go_modules/github.com/hashicorp/terraform-plugin-framework-0.17.0
Dec 14, 2022
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e0b7f8
Bump github.com/hashicorp/terraform-plugin-framework
dependabot[bot] 6de8f09
Migrating to new schema, plan modifier and validator packages
bendbennett b594dcf
Remove check that determines whether any of the config, plan or state…
bendbennett 327190a
Refactoring following code review
bendbennett 66f06a7
Removing unneeded plan modifiers
bendbennett 2a1f9c0
Removing unneeded plan modifiers
bendbennett File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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,141 @@ | ||
package boolplanmodifiers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// DefaultValue accepts a types.Bool value and uses the supplied value to set a default | ||
// if the config for the attribute is null. | ||
func DefaultValue(val types.Bool) planmodifier.Bool { | ||
return &defaultValueAttributePlanModifier{val} | ||
} | ||
|
||
type defaultValueAttributePlanModifier struct { | ||
val types.Bool | ||
} | ||
|
||
func (d *defaultValueAttributePlanModifier) Description(ctx context.Context) string { | ||
return "If the config does not contain a value, a default will be set using val." | ||
} | ||
|
||
func (d *defaultValueAttributePlanModifier) MarkdownDescription(ctx context.Context) string { | ||
return d.Description(ctx) | ||
} | ||
|
||
// PlanModifyBool checks that the value of the attribute in the configuration and assigns the default value if | ||
// the value in the config is null. This is a destructive operation in that it will overwrite any value | ||
// present in the plan. | ||
func (d *defaultValueAttributePlanModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { | ||
// Do not set default if the attribute configuration has been set. | ||
if !req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
resp.PlanValue = d.val | ||
} | ||
|
||
// RequiresReplace returns an attribute plan modifier that is identical to resource.RequiresReplace() with | ||
// the exception that there is no check for `configRaw.IsNull && attrSchema.Computed` as a replacement | ||
// needs to be triggered when the attribute has been removed from the config. | ||
func RequiresReplace() planmodifier.Bool { | ||
return RequiresReplaceModifier{} | ||
} | ||
|
||
type RequiresReplaceModifier struct{} | ||
|
||
// Description returns a human-readable description of the plan modifier. | ||
func (r RequiresReplaceModifier) Description(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} | ||
|
||
// MarkdownDescription returns a markdown description of the plan modifier. | ||
func (r RequiresReplaceModifier) MarkdownDescription(ctx context.Context) string { | ||
return r.Description(ctx) | ||
} | ||
|
||
// PlanModifyBool will trigger replacement (i.e., destroy-create) when `configRaw.IsNull && attrSchema.Computed`, | ||
// which differs from the behaviour of `resource.RequiresReplace()`. | ||
func (r RequiresReplaceModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { | ||
if req.State.Raw.IsNull() { | ||
// if we're creating the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
if req.Plan.Raw.IsNull() { | ||
// if we're deleting the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
if req.PlanValue.Equal(req.StateValue) { | ||
// if the plan and the state are in agreement, this attribute | ||
// isn't changing, don't require replace | ||
return | ||
} | ||
|
||
resp.RequiresReplace = true | ||
} | ||
|
||
// NumberNumericAttributePlanModifier returns a plan modifier that keep the values | ||
// held in number and numeric attributes synchronised. | ||
func NumberNumericAttributePlanModifier() planmodifier.Bool { | ||
return &numberNumericAttributePlanModifier{} | ||
} | ||
|
||
type numberNumericAttributePlanModifier struct { | ||
} | ||
|
||
func (d *numberNumericAttributePlanModifier) Description(ctx context.Context) string { | ||
return "Ensures that number and numeric attributes are kept synchronised." | ||
} | ||
|
||
func (d *numberNumericAttributePlanModifier) MarkdownDescription(ctx context.Context) string { | ||
return d.Description(ctx) | ||
} | ||
|
||
func (d *numberNumericAttributePlanModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { | ||
numberConfig := types.Bool{} | ||
diags := req.Config.GetAttribute(ctx, path.Root("number"), &numberConfig) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
numericConfig := types.Bool{} | ||
diags = req.Config.GetAttribute(ctx, path.Root("numeric"), &numericConfig) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if !numberConfig.IsNull() && !numericConfig.IsNull() && (numberConfig.ValueBool() != numericConfig.ValueBool()) { | ||
resp.Diagnostics.AddError( | ||
"Number and numeric are both configured with different values", | ||
"Number is deprecated, use numeric instead", | ||
) | ||
return | ||
} | ||
|
||
// Default to true for both number and numeric when both are null. | ||
if numberConfig.IsNull() && numericConfig.IsNull() { | ||
resp.PlanValue = types.BoolValue(true) | ||
return | ||
} | ||
|
||
// Default to using value for numeric if number is null. | ||
if numberConfig.IsNull() && !numericConfig.IsNull() { | ||
resp.PlanValue = numericConfig | ||
return | ||
} | ||
|
||
// Default to using value for number if numeric is null. | ||
if !numberConfig.IsNull() && numericConfig.IsNull() { | ||
resp.PlanValue = numberConfig | ||
return | ||
} | ||
} |
This file contains 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,81 @@ | ||
package int64planmodifiers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// DefaultValue accepts a types.Bool value and uses the supplied value to set a default | ||
// if the config for the attribute is null. | ||
func DefaultValue(val types.Int64) planmodifier.Int64 { | ||
return &defaultValueAttributePlanModifier{val} | ||
} | ||
|
||
type defaultValueAttributePlanModifier struct { | ||
val types.Int64 | ||
} | ||
|
||
func (d *defaultValueAttributePlanModifier) Description(ctx context.Context) string { | ||
return "If the config does not contain a value, a default will be set using val." | ||
} | ||
|
||
func (d *defaultValueAttributePlanModifier) MarkdownDescription(ctx context.Context) string { | ||
return d.Description(ctx) | ||
} | ||
|
||
// PlanModifyInt64 checks that the value of the attribute in the configuration and assigns the default value if | ||
// the value in the config is null. This is a destructive operation in that it will overwrite any value | ||
// present in the plan. | ||
func (d *defaultValueAttributePlanModifier) PlanModifyInt64(ctx context.Context, req planmodifier.Int64Request, resp *planmodifier.Int64Response) { | ||
// Do not set default if the attribute configuration has been set. | ||
if !req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
resp.PlanValue = d.val | ||
} | ||
|
||
// RequiresReplace returns an attribute plan modifier that is identical to resource.RequiresReplace() with | ||
// the exception that there is no check for `configRaw.IsNull && attrSchema.Computed` as a replacement | ||
// needs to be triggered when the attribute has been removed from the config. | ||
func RequiresReplace() planmodifier.Int64 { | ||
return RequiresReplaceModifier{} | ||
} | ||
|
||
type RequiresReplaceModifier struct{} | ||
|
||
// Description returns a human-readable description of the plan modifier. | ||
func (r RequiresReplaceModifier) Description(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} | ||
|
||
// MarkdownDescription returns a markdown description of the plan modifier. | ||
func (r RequiresReplaceModifier) MarkdownDescription(ctx context.Context) string { | ||
return r.Description(ctx) | ||
} | ||
|
||
// PlanModifyInt64 will trigger replacement (i.e., destroy-create) when `configRaw.IsNull && attrSchema.Computed`, | ||
// which differs from the behaviour of `resource.RequiresReplace()`. | ||
func (r RequiresReplaceModifier) PlanModifyInt64(ctx context.Context, req planmodifier.Int64Request, resp *planmodifier.Int64Response) { | ||
if req.State.Raw.IsNull() { | ||
// if we're creating the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
if req.Plan.Raw.IsNull() { | ||
// if we're deleting the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
if req.PlanValue.Equal(req.StateValue) { | ||
// if the plan and the state are in agreement, this attribute | ||
// isn't changing, don't require replace | ||
return | ||
} | ||
|
||
resp.RequiresReplace = true | ||
} |
This file contains 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,106 @@ | ||
package mapplanmodifiers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
) | ||
|
||
func RequiresReplaceIfValuesNotNull() planmodifier.Map { | ||
return requiresReplaceIfValuesNotNullModifier{} | ||
} | ||
|
||
type requiresReplaceIfValuesNotNullModifier struct{} | ||
|
||
func (r requiresReplaceIfValuesNotNullModifier) PlanModifyMap(ctx context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) { | ||
if req.State.Raw.IsNull() { | ||
// if we're creating the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
if req.Plan.Raw.IsNull() { | ||
// if we're deleting the resource, no need to delete and | ||
// recreate it | ||
return | ||
} | ||
|
||
// If there are no differences, do not mark the resource for replacement | ||
// and ensure the plan matches the configuration. | ||
if req.ConfigValue.Equal(req.StateValue) { | ||
return | ||
} | ||
|
||
if req.StateValue.IsNull() { | ||
// terraform-plugin-sdk would store maps as null if all keys had null | ||
// values. To prevent unintentional replacement plans when migrating | ||
// to terraform-plugin-framework, only trigger replacement when the | ||
// prior state (map) is null and when there are not null map values. | ||
allNullValues := true | ||
|
||
for _, configValue := range req.ConfigValue.Elements() { | ||
if !configValue.IsNull() { | ||
allNullValues = false | ||
} | ||
} | ||
|
||
if allNullValues { | ||
return | ||
} | ||
} else { | ||
// terraform-plugin-sdk would completely omit storing map keys with | ||
// null values, so this also must prevent unintentional replacement | ||
// in that case as well. | ||
allNewNullValues := true | ||
|
||
configMap := req.ConfigValue | ||
|
||
stateMap := req.StateValue | ||
|
||
for configKey, configValue := range configMap.Elements() { | ||
stateValue, ok := stateMap.Elements()[configKey] | ||
|
||
// If the key doesn't exist in state and the config value is | ||
// null, do not trigger replacement. | ||
if !ok && configValue.IsNull() { | ||
continue | ||
} | ||
|
||
// If the state value exists, and it is equal to the config value, | ||
// do not trigger replacement. | ||
if configValue.Equal(stateValue) { | ||
continue | ||
} | ||
|
||
allNewNullValues = false | ||
break | ||
} | ||
|
||
for stateKey := range stateMap.Elements() { | ||
_, ok := configMap.Elements()[stateKey] | ||
|
||
// If the key doesn't exist in the config, but there is a state | ||
// value, trigger replacement. | ||
if !ok { | ||
allNewNullValues = false | ||
break | ||
} | ||
} | ||
|
||
if allNewNullValues { | ||
return | ||
} | ||
} | ||
|
||
resp.RequiresReplace = true | ||
} | ||
|
||
// Description returns a human-readable description of the plan modifier. | ||
func (r requiresReplaceIfValuesNotNullModifier) Description(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} | ||
|
||
// MarkdownDescription returns a markdown description of the plan modifier. | ||
func (r requiresReplaceIfValuesNotNullModifier) MarkdownDescription(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} |
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.
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 think this is ready for the 🪓 now 😄
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.
🤦