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

internal/fwserver: Skip automatic object state conversion and nested attributes plan modification if object is null or unknown #995

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240429-165853.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource: Prevented errant collection-based nested object conversion from null/unknown
object to known object'
time: 2024-04-29T16:58:53.593467-04:00
custom:
Issue: "995"
7 changes: 7 additions & 0 deletions .changes/unreleased/NOTES-20240429-170035.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: NOTES
body: 'resource: If plan modification was dependent on nested attribute plan modification
automatically running when the nested object was null/unknown, it may be necessary to
add object-level plan modification to convert the nested object to a known object first.'
time: 2024-04-29T17:00:35.374624-04:00
custom:
Issue: "995"
11 changes: 11 additions & 0 deletions internal/fwserver/attribute_plan_modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,17 @@ func NestedAttributeObjectPlanModify(ctx context.Context, o fwschema.NestedAttri
}
}

// If the nested object itself is null or unknown, skip calling nested
// attribute plan modifiers. Any plan modification from a null or unknown
// object into a known object must occur at the object level to prevent
// the framework from errantly sending a known object when it should remain
// a null/unknown object.
//
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/993
if req.PlanValue.IsNull() || req.PlanValue.IsUnknown() {
return
}

newPlanValueAttributes := req.PlanValue.Attributes()

for nestedName, nestedAttr := range o.GetAttributes() {
Expand Down
196 changes: 193 additions & 3 deletions internal/fwserver/attribute_plan_modification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10837,7 +10837,7 @@ func TestNestedAttributeObjectPlanModify(t *testing.T) {
AttributePlan: fwValue,
},
},
"request-planvalue": {
"request-planvalue-known": {
object: testschema.NestedAttributeObjectWithPlanModifiers{
PlanModifiers: []planmodifier.Object{
testplanmodifier.Object{
Expand Down Expand Up @@ -10872,6 +10872,110 @@ func TestNestedAttributeObjectPlanModify(t *testing.T) {
AttributePlan: fwValue,
},
},
"request-planvalue-null": {
object: testschema.NestedAttributeObjectWithPlanModifiers{
PlanModifiers: []planmodifier.Object{
testplanmodifier.Object{
PlanModifyObjectMethod: func(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
got := req.PlanValue
expected := types.ObjectNull(map[string]attr.Type{"testattr": types.StringType})

if !got.Equal(expected) {
resp.Diagnostics.AddError(
"Unexpected ObjectRequest.PlanValue",
fmt.Sprintf("expected %s, got: %s", expected, got),
)
}
},
},
},
},
request: planmodifier.ObjectRequest{
Config: tfsdk.Config{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
nil, // null object
),
},
),
Schema: fwSchema,
},
ConfigValue: types.ObjectNull(map[string]attr.Type{"testattr": types.StringType}),
Path: path.Root("test"),
PathExpression: path.MatchRoot("test"),
Plan: tfsdk.Plan{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
nil, // null object
),
},
),
Schema: fwSchema,
},
PlanValue: types.ObjectNull(map[string]attr.Type{"testattr": types.StringType}),
State: tfsdk.State{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"testattr": tftypes.String,
},
},
nil, // null object
),
},
),
Schema: fwSchema,
},
StateValue: types.ObjectNull(map[string]attr.Type{"testattr": types.StringType}),
},
response: &ModifyAttributePlanResponse{
AttributePlan: types.ObjectNull(map[string]attr.Type{"testattr": types.StringType}),
},
expected: &ModifyAttributePlanResponse{
AttributePlan: types.ObjectNull(map[string]attr.Type{"testattr": types.StringType}),
},
},
"request-private": {
object: testschema.NestedAttributeObjectWithPlanModifiers{
PlanModifiers: []planmodifier.Object{
Expand Down Expand Up @@ -11261,15 +11365,15 @@ func TestNestedAttributeObjectPlanModify(t *testing.T) {
),
},
},
"response-planvalue-unknown-to-known-nested": {
"response-planvalue-unknown-object": {
object: testschema.NestedAttributeObject{
Attributes: map[string]fwschema.Attribute{
"testattr": testschema.AttributeWithStringPlanModifiers{
Required: true,
PlanModifiers: []planmodifier.String{
testplanmodifier.String{
PlanModifyStringMethod: func(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
resp.PlanValue = types.StringValue("newtestvalue") // should win over object
resp.PlanValue = types.StringValue("newtestvalue") // should not win over unknown object
},
},
},
Expand Down Expand Up @@ -11325,6 +11429,92 @@ func TestNestedAttributeObjectPlanModify(t *testing.T) {
map[string]attr.Type{"testattr": types.StringType},
),
},
expected: &ModifyAttributePlanResponse{
AttributePlan: types.ObjectUnknown(
map[string]attr.Type{
"testattr": types.StringType,
},
),
},
},
"response-planvalue-unknown-object-with-plan-modifiers-to-known-nested": {
object: testschema.NestedAttributeObjectWithPlanModifiers{
Attributes: map[string]fwschema.Attribute{
"testattr": testschema.AttributeWithStringPlanModifiers{
Required: true,
PlanModifiers: []planmodifier.String{
testplanmodifier.String{
PlanModifyStringMethod: func(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
resp.PlanValue = types.StringValue("newtestvalue") // should only work after unknown object is made known
},
},
},
},
},
PlanModifiers: []planmodifier.Object{
testplanmodifier.Object{
PlanModifyObjectMethod: func(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
resp.PlanValue = types.ObjectValueMust(
map[string]attr.Type{
"testattr": types.StringType,
},
map[string]attr.Value{
"testattr": types.StringUnknown(), // ensure string plan modifier is applied later
},
)
},
},
},
},
request: planmodifier.ObjectRequest{
Config: tfsdk.Config{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.Object{AttributeTypes: map[string]tftypes.Type{"testattr": tftypes.String}},
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(
tftypes.Object{AttributeTypes: map[string]tftypes.Type{"testattr": tftypes.String}},
nil,
),
},
),
Schema: fwSchema,
},
ConfigValue: types.ObjectNull(
map[string]attr.Type{"testattr": types.StringType},
),
Path: path.Root("test"),
PathExpression: path.MatchRoot("test"),
Plan: tfsdk.Plan{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.Object{AttributeTypes: map[string]tftypes.Type{"testattr": tftypes.String}},
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(
tftypes.Object{AttributeTypes: map[string]tftypes.Type{"testattr": tftypes.String}},
tftypes.UnknownValue,
),
},
),
Schema: fwSchema,
},
PlanValue: types.ObjectUnknown(
map[string]attr.Type{"testattr": types.StringType},
),
State: testState,
StateValue: fwValue,
},
response: &ModifyAttributePlanResponse{
AttributePlan: types.ObjectUnknown(
map[string]attr.Type{"testattr": types.StringType},
),
},
expected: &ModifyAttributePlanResponse{
AttributePlan: types.ObjectValueMust(
map[string]attr.Type{
Expand Down