Skip to content

UseStateForUnknown doesn't preserve known null prior state values #1117

Open
@austinvalle

Description

@austinvalle

Module version

v1.14.1 (latest)

Relevant provider source code

"computed_attr": schema.StringAttribute{
	Computed: true,
	PlanModifiers: []planmodifier.String{
		stringplanmodifier.UseStateForUnknown(),
	},
},

Expected Behavior

If the prior state value for computed_attr is null, that should be preserved and stored in the plan (if the attribute is unconfigured) because null is a known value.

What I believe the current logic is attempting to do, is prevent this plan modifier from running when the resource is creating (i.e. the entire state is null), which it is achieving, but a little too aggressively since it checks the specific attribute's state value instead:

// Do nothing if there is no state value.
if req.StateValue.IsNull() {
return
}

We should consider updating this logic to:

func (m useStateForUnknownModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
	// NOTE: This is the bug fix, do nothing if we are creating
	if req.State.Raw.IsNull() {
		return
	}

	// Do nothing if there is a known planned value.
	if !req.PlanValue.IsUnknown() {
		return
	}

	// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
	if req.ConfigValue.IsUnknown() {
		return
	}

	resp.PlanValue = req.StateValue
}

Actual Behavior

If the prior state value for computed_attr is null, an unknown value is being planned during update.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions