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

SetNew does not work on nested fields #459

Open
marclop opened this issue May 18, 2020 · 5 comments
Open

SetNew does not work on nested fields #459

marclop opened this issue May 18, 2020 · 5 comments
Labels
enhancement New feature or request subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework

Comments

@marclop
Copy link

marclop commented May 18, 2020

SDK version

{
  "Path": "github.com/hashicorp/terraform-plugin-sdk",
  "Version": "v1.12.0"
}

Use-cases

Currently, trying to set any nested (computed) field value in a CustomizeDiff function will fail due to a hardcoded false value on d.checkKey() in schema.ResourceDiff.SetNewComputed() and schema.ResourceDiff.SetNew().

This seems to have been changed in commit 1e08e98. Not entirely sure why this change was made, and in any case, the Elasticsearch property is a list, but not a computed one, so not even that would be possible to Get and Set.

My use case is having global values in resources which are used to override nested values.

func Deployment() *schema.Resource {
	return &schema.Resource{
		Schema: map[string]*schema.Schema{
            "version": {
                Type:     schema.TypeString,
                Required: true,
            },

            // Workloads
            "elasticsearch": {
                Type:     schema.TypeList,
                MinItems: 1,
                MaxItems: 1,
                Required: true,
                Elem: &schema.Resource{
                    Schema: map[string]*schema.Schema{
                        // ...
                        "version": {
                            Type:     schema.TypeString,
                            Computed: true,
                        },
                    },
                },
            },
            // ...
        },
		CustomizeDiff: customdiff.All(
			customdiff.IfValueChange("version", func(old, new, meta interface{}) bool {
				return new.(string) > old.(string)
			}, func(d *schema.ResourceDiff, _ interface{}) error {
                // Succeeds obtaining the key.
                log.Println("[DEBUG]", d.Get("elasticsearch.0.version"))
                // Fails setting ANY nested key.
				if err := d.SetNew("elasticsearch.0.version", d.Get("elasticsearch.0.version")); err != nil {
					return err
				}
				return nil
			}),
        ),
    }
}

Proposal

Make SetNewComputed and SetNew able to set items on a list.

@pdecat
Copy link
Contributor

pdecat commented Jun 8, 2020

Hi, the referenced change 1e08e98#diff-5572654f34bded06e0d3e5eb9ed7d1bf does not really remove any capability to update nested fields, it only catches the error earlier.

Before that change, an error was triggered in the field writer implementation.

Error: Cannot set new diff value for key metadata.0.resource_version: metadata.0.resource_version: can only set full list

https://github.com/hashicorp/terraform-plugin-sdk/blob/v1.13.1/helper/schema/field_writer_map.go#L72

@marclop
Copy link
Author

marclop commented Jun 9, 2020

Right, I see.

I wonder what the recommended approach would be when this use-case is required

@marclop
Copy link
Author

marclop commented Oct 22, 2020

This still doesn't work with the V2 of the SDK.

@paddycarver paddycarver added the subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. label Jan 6, 2021
@bflad bflad added the terraform-plugin-framework Resolved in terraform-plugin-framework label Oct 26, 2022
@joey-squid
Copy link

Can anyone speak to whether this is a limitation in Terraform or in the SDK (or in protocol 5, which seems unlikely)? I'm wondering if porting to use a different plugin framework would help.

@bflad
Copy link
Contributor

bflad commented May 21, 2024

Hi @joey-squid 👋 It very likely is only an issue with terraform-plugin-sdk (also where this specific error message is being generated). Terraform has some data consistency rules around plan modifications (e.g. you cannot change a configured value to a different planned value), but otherwise will let you fill in or update computed values regardless of whether it is under a nested block or not. Provider code based on terraform-plugin-framework performing plan modification should not run into this type of problem unless it is something disallowed by Terraform itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework
Projects
None yet
Development

No branches or pull requests

5 participants