Skip to content

Commit

Permalink
Using UnmarshalWithOpts in order to ignore undefined attributes (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jul 27, 2022
1 parent 9f2a0e7 commit 4b83e22
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/google/go-cmp v0.5.8
github.com/hashicorp/terraform-plugin-go v0.12.0
github.com/hashicorp/terraform-plugin-log v0.6.0
github.com/hashicorp/terraform-plugin-log v0.7.0
)

require (
Expand Down
13 changes: 11 additions & 2 deletions internal/fwserver/server_upgraderesourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

// UpgradeResourceStateRequest is the framework server request for the
Expand Down Expand Up @@ -57,12 +59,19 @@ func (s *Server) UpgradeResourceState(ctx context.Context, req *UpgradeResourceS
// version which would never get called, the framework can introduce a
// unit test helper.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/113
//
// UnmarshalWithOpts allows optionally ignoring instances in which elements being
// do not have a corresponding attribute within the schema.
if req.Version == req.ResourceSchema.Version {
logging.FrameworkTrace(ctx, "UpgradeResourceState request version matches current Schema version, using framework defined passthrough implementation")

resourceSchemaType := req.ResourceSchema.TerraformType(ctx)

rawStateValue, err := req.RawState.Unmarshal(resourceSchemaType)
rawStateValue, err := req.RawState.UnmarshalWithOpts(resourceSchemaType, tftypes.UnmarshalOpts{
JSONOpts: tftypes.JSONOpts{
IgnoreUndefinedAttributes: true,
},
})

if err != nil {
resp.Diagnostics.AddError(
Expand Down
29 changes: 15 additions & 14 deletions internal/fwserver/server_upgraderesourcestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestServerUpgradeResourceState(t *testing.T) {
Expand Down Expand Up @@ -599,9 +600,11 @@ func TestServerUpgradeResourceState(t *testing.T) {
Provider: &testprovider.Provider{},
},
request: &fwserver.UpgradeResourceStateRequest{
RawState: &tfprotov6.RawState{
JSON: []byte(`{"nonexistent_attribute":"value"}`),
},
RawState: testNewRawState(t, map[string]interface{}{
"id": "test-id-value",
"required_attribute": "true",
"nonexistent_attribute": "value",
}),
ResourceSchema: schema,
ResourceType: &testprovider.ResourceType{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
Expand All @@ -615,15 +618,13 @@ func TestServerUpgradeResourceState(t *testing.T) {
Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation
},
expectedResponse: &fwserver.UpgradeResourceStateResponse{
Diagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Unable to Read Previously Saved State for UpgradeResourceState",
"There was an error reading the saved resource state using the current resource schema.\n\n"+
"If this resource state was last refreshed with Terraform CLI 0.11 and earlier, it must be refreshed or applied with an older provider version first. "+
"If you manually modified the resource state, you will need to manually modify it to match the current resource schema. "+
"Otherwise, please report this to the provider developer:\n\n"+
"ElementKeyValue(tftypes.String<unknown>): unsupported attribute \"nonexistent_attribute\"",
),
UpgradedState: &tfsdk.State{
Raw: tftypes.NewValue(schemaType, map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "test-id-value"),
"optional_attribute": tftypes.NewValue(tftypes.String, nil),
"required_attribute": tftypes.NewValue(tftypes.String, "true"),
}),
Schema: schema,
},
},
},
Expand Down

0 comments on commit 4b83e22

Please sign in to comment.