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

Inconsistencies between `ResourceData.GetRaw...()` and `ResourceDiff.GetRaw...()`

Open gdavison opened this issue 3 years ago • 2 comments

SDK version

github.com/hashicorp/terraform-plugin-sdk/v2 v2.20.0

Relevant provider source code

func ResourceEntityRecognizer() *schema.Resource {
	return &schema.Resource{
		...

		Schema: map[string]*schema.Schema{
			...
			"version_name": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
				DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
					...
				},
			},
			...
		},

		CustomizeDiff: customdiff.All(
			...
			func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
				...
			},
		),
	}
}

Terraform Configuration Files

Before:

resource "aws_comprehend_entity_recognizer" "example" {
  version_name = "value"
  ...
}

After:

resource "aws_comprehend_entity_recognizer" "example" {
  version_name = ""
  ...
}

Expected Behavior

Setting version_name = "" will cause both ResourceData.GetRawConfig().GetAttr("version_name") and ResourceDiff.GetRawConfig().GetAttr("version_name") to have the value ""

Actual Behavior

In the CustomizeDiffFunc, ResourceDiff.GetRawConfig().GetAttr("version_name") has the value "value".

In the Update function, ResourceData.GetRawConfig().GetAttr("version_name") has the value "".

References

I assume this is related to how Optional & Computed do not show a diff.

gdavison avatar Aug 09 '22 22:08 gdavison

My understanding with the old internals of the sdk is that the cty value handling (Raw* fields) is more of a resource-level thing than affected by any attributes. Those cty values are fetched early on when an RPC is received and just need to be appropriately propagated through the internal logic to where they get surfaced to provider developers.

In my quick initial investigation, I was able to find one place where the cty values were not being copied as expected: https://github.com/hashicorp/terraform-plugin-sdk/pull/1024 -- can you see if that branch helps resolve your issue?

bflad avatar Aug 10 '22 15:08 bflad

Hey @gdavison 👋 Is there any improvement with using v2.21.0? If not, we'll need to figure out other potential places where the data is not being appropriately copied.

bflad avatar Aug 26 '22 19:08 bflad

Closing due to lack of response. Please reach out if this is still an issue.

bflad avatar Nov 02 '23 08:11 bflad

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Dec 03 '23 02:12 github-actions[bot]