Skip to content
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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -7,8 +7,8 @@ require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v0.16.0
github.com/hashicorp/terraform-plugin-framework-validators v0.6.0
github.com/hashicorp/terraform-plugin-framework v0.17.0
github.com/hashicorp/terraform-plugin-framework-validators v0.7.0
github.com/hashicorp/terraform-plugin-go v0.14.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Expand Up @@ -141,10 +141,10 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smPzZrt1Wlm9koLeKazY=
github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ=
github.com/hashicorp/terraform-plugin-framework v0.16.0 h1:kEHh0d6dp5Ig/ey6PYXkWDZPMLIW8Me41T/Oa7bpO4s=
github.com/hashicorp/terraform-plugin-framework v0.16.0/go.mod h1:Vk5MuIJoE1qksHZawAZr6psx6YXsQBFIKDrWbROrwus=
github.com/hashicorp/terraform-plugin-framework-validators v0.6.0 h1:j8GlgzuTjbbkMnsbiOXerwJTW8Us4saQwo4FfJKd2I0=
github.com/hashicorp/terraform-plugin-framework-validators v0.6.0/go.mod h1:iOYhZQinlZ0R/M3Q3nCXS4dikZ7P41d5b7ezpe9uoIw=
github.com/hashicorp/terraform-plugin-framework v0.17.0 h1:0KUOY/oe1GPLFqaXnKDnd1rhCrnUtt8pV9wGEwNUFlU=
github.com/hashicorp/terraform-plugin-framework v0.17.0/go.mod h1:FV97t2BZOARkL7NNlsc/N25c84MyeSSz72uPp7Vq1lg=
github.com/hashicorp/terraform-plugin-framework-validators v0.7.0 h1:tIYOMNmEMQIc6mwun8nX3e5U3TkgZg1TpXRlBEBQHwY=
github.com/hashicorp/terraform-plugin-framework-validators v0.7.0/go.mod h1:e1RKREyEVdd3FK8Jfgz8L/ThQgcJKLb4ZJxNzsuIH0A=
github.com/hashicorp/terraform-plugin-go v0.14.2 h1:rhsVEOGCnY04msNymSvbUsXfRLKh9znXZmHlf5e8mhE=
github.com/hashicorp/terraform-plugin-go v0.14.2/go.mod h1:Q12UjumPNGiFsZffxOsA40Tlz1WVXt2Evh865Zj0+UA=
github.com/hashicorp/terraform-plugin-log v0.7.0 h1:SDxJUyT8TwN4l5b5/VkiTIaQgY6R+Y2BQ0sRZftGKQs=
Expand Down
141 changes: 141 additions & 0 deletions internal/planmodifiers/bool/boolplanmodifiers.go
@@ -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 {
Copy link
Member

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 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

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
}
}
81 changes: 81 additions & 0 deletions internal/planmodifiers/int64/int64planmodifiers.go
@@ -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
}
106 changes: 106 additions & 0 deletions internal/planmodifiers/map/mapplanmodifiers.go
@@ -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."
}