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

Multiple computed BoolAttributes with defaults in SetNestedAttribute cause flip-flopping plan on every apply #867

Open
henryrecker-pingidentity opened this issue Oct 27, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@henryrecker-pingidentity

Module version

1.4.2

Relevant provider source code

func (r *setNestedExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"set": schema.SetNestedAttribute{
				Required: true,
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"name": schema.StringAttribute{
							Required: true,
						},
						"bool_one": schema.BoolAttribute{
							Optional: true,
							Computed: true,
							Default:  booldefault.StaticBool(false),
						},
						"bool_two": schema.BoolAttribute{
							Optional: true,
							Computed: true,
							Default:  booldefault.StaticBool(false),
						},
					},
				},
			},
		},
	}
}

func (r *setNestedExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	resp.State.Raw = req.Plan.Raw
}

func (r *setNestedExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
	resp.State.Raw = req.State.Raw
}

func (r *setNestedExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
	resp.State.Raw = req.Plan.Raw
}

Terraform Configuration Files

resource "example_set_nested_example" "myExample" {
  set = [
    {
      name = "test2"
      bool_two = true
    }
  ]
}

Debug Output

Expected Behavior

The provider would not produce any plan after the first apply.

Actual Behavior

The first apply succeeds, and everything looks right in the state. On every subsequent apply, the generated plan flips the bool_two value to its opposite value.

First plan for example:

# example_set_nested_example.myExample will be created
  + resource "example_set_nested_example" "myExample" {
      + set = [
          + {
              + bool_one = false
              + bool_two = true
              + name     = "test2"
            },
        ]
    }

Second plan with no changes to HCL:

# example_set_nested_example.myExample will be updated in-place
  ~ resource "example_set_nested_example" "myExample" {
      ~ set = [
          - {
              - bool_one = false -> null
              - bool_two = true -> null
              - name     = "test2" -> null
            },
          + {
              + bool_one = false
              + bool_two = false
              + name     = "test2"
            },
        ]
    }

Third plan:

# example_set_nested_example.myExample will be updated in-place
  ~ resource "example_set_nested_example" "myExample" {
      ~ set = [
          - {
              - bool_one = false -> null
              - bool_two = false -> null
              - name     = "test2" -> null
            },
          + {
              + bool_one = false
              + bool_two = true
              + name     = "test2"
            },
        ]
    }

The plans will continue to cycle between setting bool_two to true and to false.

Steps to Reproduce

Apply the provided HCL multiple times

@henryrecker-pingidentity henryrecker-pingidentity added the bug Something isn't working label Oct 27, 2023
@austinvalle
Copy link
Member

I believe this bug report is a different result of a similar root cause from #783 and #709, a known issue with Set identity and plan modification (default values).

@thiagoarrais
Copy link

I have been bitten by this. My workaround was to re-implement the defaults using a custom plan modifier on the set. I'll subscribe to this in hopes of removing that horrible cludge.

See thiagoarrais/terraform-provider-twoflag-issue-example@adacdb6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants