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

Amend Plan Resource Change To Allow For Optional Blocks #552

Merged
merged 3 commits into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 internal/fwserver/server_planresourcechange.go
Expand Up @@ -291,6 +291,12 @@ func MarkComputedNilsAsUnknown(ctx context.Context, config tftypes.Value, resour
return val, nil
}

if errors.Is(err, tfsdk.ErrPathIsBlock) {
// ignore blocks, they do not have a computed field
logging.FrameworkTrace(ctx, "attribute is a block, not marking unknown")
return val, nil
}

logging.FrameworkError(ctx, "couldn't find attribute in resource schema")

return tftypes.Value{}, fmt.Errorf("couldn't find attribute in resource schema: %w", err)
Expand Down
111 changes: 111 additions & 0 deletions internal/fwserver/server_planresourcechange_test.go
Expand Up @@ -274,6 +274,18 @@ func TestServerPlanResourceChange(t *testing.T) {
},
}

testSchemaBlockType := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_required": tftypes.String,
"test_optional_block": tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional_one": tftypes.String,
"test_optional_two": tftypes.String,
},
},
},
}

testSchemaTypeComputed := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_computed": tftypes.String,
Expand All @@ -293,6 +305,30 @@ func TestServerPlanResourceChange(t *testing.T) {
},
}

testSchemaBlock := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_required": {
Required: true,
Type: types.StringType,
},
},
Blocks: map[string]tfsdk.Block{
"test_optional_block": {
Attributes: map[string]tfsdk.Attribute{
"test_optional_one": {
Type: types.StringType,
Optional: true,
},
"test_optional_two": {
Type: types.StringType,
Optional: true,
},
},
NestingMode: tfsdk.BlockNestingModeSingle,
},
},
}

testEmptyPlan := &tfsdk.Plan{
Raw: tftypes.NewValue(testSchemaType, nil),
Schema: testSchema,
Expand All @@ -308,6 +344,11 @@ func TestServerPlanResourceChange(t *testing.T) {
TestRequired types.String `tfsdk:"test_required"`
}

type testSchemaDataBlock struct {
TestRequired types.String `tfsdk:"test_required"`
TestOptionalBlock types.Object `tfsdk:"test_optional_block"`
}

testSchemaAttributePlanModifierAttributePlan := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_computed": {
Expand Down Expand Up @@ -1935,6 +1976,76 @@ func TestServerPlanResourceChange(t *testing.T) {
PlannedPrivate: testEmptyPrivate,
},
},
"update-resourcewithmodifyplan-request-config-nil-block": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
request: &fwserver.PlanResourceChangeRequest{
Config: &tfsdk.Config{
Raw: tftypes.NewValue(testSchemaBlockType, map[string]tftypes.Value{
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_optional_block": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional_one": tftypes.String,
"test_optional_two": tftypes.String,
},
}, nil),
}),
Schema: testSchemaBlock,
},
ProposedNewState: &tfsdk.Plan{
Raw: tftypes.NewValue(testSchemaBlockType, map[string]tftypes.Value{
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_optional_block": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional_one": tftypes.String,
"test_optional_two": tftypes.String,
},
}, nil),
}),
Schema: testSchemaBlock,
},
PriorState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaBlockType, map[string]tftypes.Value{
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To trigger this new unit test to fail without the fix and therefore verify the fix, there needs to be a difference between the prior state and proposed new state to trigger the call to MarkComputedNilsAsUnknown. Adjusting this line should do the trick.

Suggested change
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-value"),

The reason this issue occurs upstream is mainly because during resource creation, the prior state is null, but the proposed new state contains blocks.

"test_optional_block": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional_one": tftypes.String,
"test_optional_two": tftypes.String,
},
}, nil),
}),
Schema: testSchemaBlock,
},
ResourceSchema: testSchemaBlock,
Resource: &testprovider.ResourceWithModifyPlan{
ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
var data testSchemaDataBlock

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.ValueString() != "test-new-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}
},
},
},
expectedResponse: &fwserver.PlanResourceChangeResponse{
PlannedState: &tfsdk.State{
Raw: tftypes.NewValue(testSchemaBlockType, map[string]tftypes.Value{
"test_required": tftypes.NewValue(tftypes.String, "test-new-value"),
"test_optional_block": tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional_one": tftypes.String,
"test_optional_two": tftypes.String,
},
}, nil),
}),
Schema: testSchemaBlock,
},
PlannedPrivate: testEmptyPrivate,
},
},
"update-resourcewithmodifyplan-request-proposednewstate": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
Expand Down