terraform-plugin-sdk icon indicating copy to clipboard operation
terraform-plugin-sdk copied to clipboard

Change not triggered for optional / computed string field when set to empty string

Open timofurrer opened this issue 1 year ago • 3 comments

In one of my resources I have the following schema:

"some_attr": {
   Type: schema.TypeStr,
   Optional: true,
   Computed: true,
}

When changing from this config:

resource "some_res" "this" {
   some_attr = "foo"
}

to this:

resource "some_res" "this" {
  some_attr = ""
}

it won't trigger a change, because of this:

https://github.com/hashicorp/terraform-plugin-sdk/blob/ac0b9654b5217af313c746f205b98f3278e3f20b/helper/schema/schema.go#L565

What's the proper way to solve this situation?

In this case some_attr is optional and the "infrastructure" may set it to something (hence the computed) or the terraform user can chose to do so.

timofurrer avatar Nov 11 '22 13:11 timofurrer

Hi @timofurrer 👋 Thank you for raising this and sorry you ran into this frustrating behavior.

This SDK unfortunately implements some automatic behaviors which are difficult to workaround in provider implementations. Schema attributes that are set as Optional + Computed will preserve their known prior state value if a configuration value changes to null or a zero-value of the type (e.g. "" for strings). Changing this behavior would require a major version release since many providers are reliant on the existing behavior.

I do not believe there is a workaround for this except to potentially introduce a separate attribute that gets filled in with the infrastructure value so Computed can be removed from the configurable attribute and disable the automatic behavior. It may also be possible to use a Default value if there is an appropriate default infrastructure value to trigger the SDK to treat an empty string as different.

The team that maintains this SDK has been working on a reimagined development experience that does not have this automatic behavior over in the Terraform Plugin Framework. The Which SDK? page can provide some guidance on whether to adopt that SDK is appropriate and the framework documentation is now includes a Migrating from SDK section. It is targeted for a version 1.0.0 release next month.

bflad avatar Nov 11 '22 17:11 bflad

@bflad 👋 Thanks for the answer!

It is targeted for a version 1.0.0 release next month.

What's the best way to follow that release (schedule) ?

timofurrer avatar Nov 12 '22 13:11 timofurrer

To be notified when the release happens, you can watch the terraform-plugin-framework GitHub repository's releases, watch the Plugin Development section in HashiCorp Discuss, or subscribe to the HashiCorp blog.

As for the scheduling, you can see the remaining items drop out of the v1.0.0 milestone as they move into other milestones.

bflad avatar Nov 12 '22 19:11 bflad