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

A better AttributePlanModifier for Computed,Optional,ForceNew attributes #222

Closed
ewbankkit opened this issue Oct 1, 2021 · 1 comment · Fixed by #306
Closed

A better AttributePlanModifier for Computed,Optional,ForceNew attributes #222

ewbankkit opened this issue Oct 1, 2021 · 1 comment · Fixed by #306
Labels
upstream-plugin-framework Unable to proceed due to missing or broken functionality from terraform-plugin-framework

Comments

@ewbankkit
Copy link
Contributor

ewbankkit commented Oct 1, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
  • The resources and data sources in this provider are generated from the CloudFormation schema, so they can only support the actions that the underlying schema supports. For this reason submitted bugs should be limited to defects in the generation and runtime code of the provider. Customizing behavior of the resource, or noting a gap in behavior are not valid bugs and should be submitted as enhancements to AWS via the Cloudformation Open Coverage Roadmap.

Relates hashicorp/terraform-plugin-framework#187.

A better (than tfsdk.RequiresReplace()) AttributePlan modifier for Computed, Optional, ForceNew attributes would be of the form:

PlanModifiers: []tfsdk.AttributePlanModifier{
	tfsdk.RequiresReplaceIf(func(ctx context.Context, state, config attr.Value, path *tftypes.AttributePath) (bool, diag.Diagnostics) {
		if config.Equal(state) || config.Equal(types.String{Null: true}) {
			return false, nil
		}
		return true, nil
	}, "require replacement if configuration value changes", "require replacement if configuration value changes"),
},

with config.Equal(types.String{Null: true}) replaced by the attribute's type.
Once hashicorp/terraform-plugin-framework#193 is resolved this can be implemented generically.

@bflad
Copy link
Contributor

bflad commented Oct 1, 2021

with config.Equal(types.String{Null: true}) replaced by the attribute's type.

Another option to keep it generic until hashicorp/terraform-plugin-framework#193 is implemented would be to enumerate all the framework type cases, e.g.

func ThisIsTheForceNewYouAreLookingFor() tfsdk.AttributePlanModifier {
	return tfsdk.RequiresReplaceIf(func(ctx context.Context, state, config attr.Value, path *tftypes.AttributePath) (bool, diag.Diagnostics) {
		if config.Equal(state) {
			return false, nil
		}

		// TODO: This can be replaced with a single config.IsNull() conditional when available
		// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/193
		switch config.(type) {
		case types.Bool:
			return !config.Equal(types.Bool{Null: true}), nil
		case types.Float64:
			return !config.Equal(types.Float64{Null: true}), nil
		case types.Int64:
			return !config.Equal(types.Int64{Null: true}), nil
		case types.List:
			return !config.Equal(types.List{Null: true}), nil
		case types.Map:
			return !config.Equal(types.Map{Null: true}), nil
		case types.Object:
			return !config.Equal(types.Object{Null: true}), nil
		case types.Number:
			return !config.Equal(types.Number{Null: true}), nil
		case types.Set:
			return !config.Equal(types.Set{Null: true}), nil
		case types.String:
			return !config.Equal(types.String{Null: true}), nil
		}

		return true, nil
}, "require replacement if configuration value changes", "require replacement if configuration value changes")
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream-plugin-framework Unable to proceed due to missing or broken functionality from terraform-plugin-framework
Projects
None yet
3 participants