-
Notifications
You must be signed in to change notification settings - Fork 232
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
d.Set a non-computed schema.Set property during terraform apply
will not take effect
#618
Comments
Yet Another Simplified ExampleGiven following schema: "locations_deprecated": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"locations": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
}, And following code in func resourceBarCreateOrUpdate(d *schema.ResourceData, m interface{}) error {
...
param.Locations = expandLocationSlicePtr(d.Get("locations").(*schema.Set).List())
if param.Locations == nil {
param.Locations = expandLocationSlicePtr(d.Get("locations_deprecated").(*schema.Set).List())
}
...
}
func resourceBarCreateOrUpdate(d *schema.ResourceData, m interface{}) error {
...
if err := d.Set("locations", flattenLocationSlicePtr(resp.Locations)); err != nil {
return fmt.Errorf(`setting "locations": %v`, err)
}
if err := d.Set("locations_deprecated", flattenLocationSlicePtr(resp.Locations)); err != nil {
return fmt.Errorf(`setting "locations_deprecated": %v`, err)
}
...
} Then the issue can be reproduced as below: 💤 cat main.tf
resource "demo_resource_bar" "magodo" {
name = "magodo"
locations = ["a"]
}
💤 tf apply -auto-approve
demo_resource_bar.magodo: Creating...
demo_resource_bar.magodo: Creation complete after 0s [id=magodo]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
💤 tf show
# demo_resource_bar.magodo:
resource "demo_resource_bar" "magodo" {
id = "magodo"
locations = [
"a",
]
locations_deprecated = [
"a",
]
name = "magodo"
phone = 0
}
💤 sed -i 's;"a";"b";' main.tf
💤 tf apply -auto-approve
tf demo_resource_bar.magodo: Refreshing state... [id=magodo]
shdemo_resource_bar.magodo: Modifying... [id=magodo]
odemo_resource_bar.magodo: Modifications complete after 0s [id=magodo]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
💤 tf show
# demo_resource_bar.magodo:
resource "demo_resource_bar" "magodo" {
id = "magodo"
locations = [
"b",
]
locations_deprecated = []
name = "magodo"
phone = 0
}
💤 tf refresh
demo_resource_bar.magodo: Refreshing state... [id=magodo]
💤 tf show
# demo_resource_bar.magodo:
resource "demo_resource_bar" "magodo" {
id = "magodo"
locations = [
"b",
]
locations_deprecated = [
"b",
]
name = "magodo"
phone = 0
} As shown above, after updating the The source code is here: https://github.com/magodo/terraform-provider-demo/tree/d_set_on_set_with_no_diff The Issue doesn't happen for TypeListChanging the type of both properties from The source code is here: https://github.com/magodo/terraform-provider-demo/tree/d_set_on_list AssumptionThe sympton seems like the |
Hi @magodo 👋 Thanks for raising this and sorry you ran into trouble here. Looking at both of these cases, this appears to be expected behavior with how Terraform works between the configuration, plan, and state phases of a resource lifecycle. For reference, this documentation might be helpful: https://github.com/hashicorp/terraform/blob/20e9d8e28c32de78ebb8ae0ba2a3a00b70ee89f4/docs/resource-instance-change-lifecycle.md The gist here is that the provider (via the Schema and potentially CustomizeDiff) must signal to Terraform CLI the proper expectations about a value for an attribute. If the resource schema denotes Recent versions of Terraform CLI when used with this SDK should generate warning logs in these data mismatch scenarios, which can be seen by running with Hope this helps. |
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. |
SDK version
Relevant provider source code
The schema definition of a non-computed Set: link
Another computed set definition: link
Both will be flattend during the Read callback, wihch will be called in Create.
Now changing the flatten function a bit, instead of setting the actual
country
, change it to setting a const valuefoo
:Terraform Configuration Files
Expected Behavior
With a terraform apply, both the
addr.x.country
andoutput_addr.x.country
is set tofoo
.Actual Behavior
Only the
output_addr
is set during the Read in apply phase, theaddr
although is flattend into theResourceData
, but is never finalized to the state.Steps to Reproduce
The text was updated successfully, but these errors were encountered: