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

ResourceData.Partial does not work for TypeSet of primtives

Open paultyng opened this issue 5 years ago • 2 comments

SDK version

v2.0.0 (although able to repro in 1.15.0 and 1.0.0)

Relevant provider source code

I used an update that sets d.Partial and schema with a TypeSet (nested or root) and then errors to reproduce:

package main

import (
	"fmt"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)

func main() {
	plugin.Serve(&plugin.ServeOpts{ProviderFunc: Provider})
}

func Provider() *schema.Provider {
	return &schema.Provider{
		ResourcesMap: map[string]*schema.Resource{
			"example_example": resourceServer(),
		},
	}
}

func resourceServer() *schema.Resource {
	return &schema.Resource{
		Create: func(d *schema.ResourceData, m interface{}) error {
			d.SetId("123")
			return nil
		},
		Read: func(d *schema.ResourceData, m interface{}) error {
			return nil
		},
		Update: func(d *schema.ResourceData, m interface{}) error {
			d.Partial(true)
			return fmt.Errorf("something happened!")
		},
		Delete: func(d *schema.ResourceData, m interface{}) error {
			return nil
		},

		Schema: map[string]*schema.Schema{
			"number": {
				Type:     schema.TypeSet,
				Optional: true,
				MinItems: 1,
				Elem: &schema.Schema{
					Type: schema.TypeInt,
				},
			},
		},
	}
}

Terraform Configuration Files

Apply a create:

resource "example_example" "test" {
  number  = [24]
}

Modify the number attribute:

resource "example_example" "test" {
  number  = [26]
}

When you apply you will get an error from the update, but the value is still written, even though d.Partial(true) was called.

Debug Output

Expected Behavior

State should still show number = [24]

Actual Behavior

State shows number = [26]

Steps to Reproduce

See above

References

Somewhat related to https://github.com/hashicorp/terraform-plugin-sdk/issues/476

paultyng avatar Aug 07 '20 20:08 paultyng

I've only confirmed this with primitive type sets (where the hash will not match), its possible that when the hashing matches nested schema.Resource information may be unaffected as well.

paultyng avatar Aug 07 '20 20:08 paultyng

Hello, do you have any update on this issue? Maybe there is some other option how to refresh state in code without ResourceData.Partial ?

Slonimskaia avatar Jun 02 '22 09:06 Slonimskaia