From ab97382251626f8285365f5e5d90bd7b097957d4 Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 28 Apr 2025 07:53:24 -0400 Subject: [PATCH 01/11] Adding the upgradeRPC for resource identity. Still requires some tests. --- .../fromproto5/upgraderesourceidentity.go | 47 ++ .../upgraderesourceidentity_test.go | 104 +++ .../fromproto6/upgraderesourceidentity.go | 46 ++ .../upgraderesourceidentity_test.go | 104 +++ .../server_upgraderesourceidentity.go | 240 +++++++ .../server_upgraderesourceidentity_test.go | 672 ++++++++++++++++++ .../server_upgraderesourceidentity.go | 42 +- .../server_upgraderesourceidentity_test.go | 296 ++++++++ .../server_upgraderesourceidentity.go | 42 +- .../server_upgraderesourceidentity_test.go | 296 ++++++++ ...resourcewithconfigureandupgradeidentity.go | 43 ++ .../resourcewithupgradeidentity.go | 42 ++ internal/toproto5/upgraderesourceidentity.go | 30 + .../toproto5/upgraderesourceidentity_test.go | 149 ++++ internal/toproto6/upgraderesourceidentity.go | 30 + .../toproto6/upgraderesourceidentity_test.go | 149 ++++ resource/identity_upgrader.go | 49 ++ resource/resource.go | 15 + resource/upgrade_identity.go | 37 + 19 files changed, 2431 insertions(+), 2 deletions(-) create mode 100644 internal/fromproto5/upgraderesourceidentity.go create mode 100644 internal/fromproto5/upgraderesourceidentity_test.go create mode 100644 internal/fromproto6/upgraderesourceidentity.go create mode 100644 internal/fromproto6/upgraderesourceidentity_test.go create mode 100644 internal/fwserver/server_upgraderesourceidentity.go create mode 100644 internal/fwserver/server_upgraderesourceidentity_test.go create mode 100644 internal/proto5server/server_upgraderesourceidentity_test.go create mode 100644 internal/proto6server/server_upgraderesourceidentity_test.go create mode 100644 internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go create mode 100644 internal/testing/testprovider/resourcewithupgradeidentity.go create mode 100644 internal/toproto5/upgraderesourceidentity.go create mode 100644 internal/toproto5/upgraderesourceidentity_test.go create mode 100644 internal/toproto6/upgraderesourceidentity.go create mode 100644 internal/toproto6/upgraderesourceidentity_test.go create mode 100644 resource/identity_upgrader.go create mode 100644 resource/upgrade_identity.go diff --git a/internal/fromproto5/upgraderesourceidentity.go b/internal/fromproto5/upgraderesourceidentity.go new file mode 100644 index 000000000..4b279d057 --- /dev/null +++ b/internal/fromproto5/upgraderesourceidentity.go @@ -0,0 +1,47 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fromproto5 + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-go/tfprotov5" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest +// equivalent of a *tfprotov5.UpgradeResourceIdentityRequest. +func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { + if proto5 == nil { + return nil, nil + } + + var diags diag.Diagnostics + + // Panic prevention here to simplify the calling implementations. + // This should not happen, but just in case. + if identitySchema == nil { + diags.AddError( + "Unable to Create Empty Identity", + "An unexpected error was encountered when creating the empty Identity. "+ + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ + "Please report this to the provider developer:\n\n"+ + "Missing schema.", + ) + + return nil, diags + } + + fw := &fwserver.UpgradeResourceIdentityRequest{ + RawState: (*tfprotov6.RawState)(proto5.RawIdentity), + IdentitySchema: identitySchema, + Resource: resource, + Version: proto5.Version, + } + + return fw, diags +} diff --git a/internal/fromproto5/upgraderesourceidentity_test.go b/internal/fromproto5/upgraderesourceidentity_test.go new file mode 100644 index 000000000..e7257812d --- /dev/null +++ b/internal/fromproto5/upgraderesourceidentity_test.go @@ -0,0 +1,104 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fromproto5_test + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-go/tfprotov5" +) + +func TestUpgradeResourceIdentityRequest(t *testing.T) { + t.Parallel() + + testIdentitySchema := identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_attribute": schema.StringAttribute{ + Required: true, + }, + }, + } + + testCases := map[string]struct { + input *tfprotov5.UpgradeResourceIdentityRequest + identitySchema fwschema.Schema + resource resource.Resource + expected *fwserver.UpgradeResourceIdentityRequest + expectedDiagnostics diag.Diagnostics + }{ + "nil": { + input: nil, + expected: nil, + }, + "rawstate": { + input: &tfprotov5.UpgradeResourceIdentityRequest{ + RawIdentity: testNewTfprotov5RawState(t, map[string]interface{}{ + "test_attribute": "test-value", + }), + }, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewTfprotov6RawState(t, map[string]interface{}{ + "test_attribute": "test-value", + }), + IdentitySchema: testIdentitySchema, + }, + }, + "resourceschema": { + input: &tfprotov5.UpgradeResourceIdentityRequest{}, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + IdentitySchema: testIdentitySchema, + }, + }, + "identityschema-missing": { + input: &tfprotov5.UpgradeResourceIdentityRequest{}, + expected: nil, + expectedDiagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Create Empty Identity", + "An unexpected error was encountered when creating the empty Identity. "+ + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ + "Please report this to the provider developer:\n\n"+ + "Missing schema.", + ), + }, + }, + "version": { + input: &tfprotov5.UpgradeResourceIdentityRequest{ + Version: 123, + }, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + IdentitySchema: testIdentitySchema, + Version: 123, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + + if diff := cmp.Diff(got, testCase.expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { + t.Errorf("unexpected diagnostics difference: %s", diff) + } + }) + } +} diff --git a/internal/fromproto6/upgraderesourceidentity.go b/internal/fromproto6/upgraderesourceidentity.go new file mode 100644 index 000000000..849eb3b28 --- /dev/null +++ b/internal/fromproto6/upgraderesourceidentity.go @@ -0,0 +1,46 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fromproto6 + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest +// equivalent of a *tfprotov6.UpgradeResourceIdentityRequest. +func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { + if proto6 == nil { + return nil, nil + } + + var diags diag.Diagnostics + + // Panic prevention here to simplify the calling implementations. + // This should not happen, but just in case. + if identitySchema == nil { + diags.AddError( + "Unable to Create Empty Identity", + "An unexpected error was encountered when creating the empty Identity. "+ + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ + "Please report this to the provider developer:\n\n"+ + "Missing schema.", + ) + + return nil, diags + } + + fw := &fwserver.UpgradeResourceIdentityRequest{ + RawState: proto6.RawIdentity, + IdentitySchema: identitySchema, + Resource: resource, + Version: proto6.Version, + } + + return fw, diags +} diff --git a/internal/fromproto6/upgraderesourceidentity_test.go b/internal/fromproto6/upgraderesourceidentity_test.go new file mode 100644 index 000000000..6cf766e6d --- /dev/null +++ b/internal/fromproto6/upgraderesourceidentity_test.go @@ -0,0 +1,104 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fromproto6_test + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto6" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +func TestUpgradeResourceIdentityRequest(t *testing.T) { + t.Parallel() + + testIdentitySchema := identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_attribute": schema.StringAttribute{ + Required: true, + }, + }, + } + + testCases := map[string]struct { + input *tfprotov6.UpgradeResourceIdentityRequest + identitySchema fwschema.Schema + resource resource.Resource + expected *fwserver.UpgradeResourceIdentityRequest + expectedDiagnostics diag.Diagnostics + }{ + "nil": { + input: nil, + expected: nil, + }, + "rawIdentity": { + input: &tfprotov6.UpgradeResourceIdentityRequest{ + RawIdentity: testNewRawState(t, map[string]interface{}{ + "test_attribute": "test-value", + }), + }, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "test_attribute": "test-value", + }), + IdentitySchema: testIdentitySchema, + }, + }, + "resourceschema": { + input: &tfprotov6.UpgradeResourceIdentityRequest{}, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + IdentitySchema: testIdentitySchema, + }, + }, + "resourceschema-missing": { + input: &tfprotov6.UpgradeResourceIdentityRequest{}, + expected: nil, + expectedDiagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Create Empty Identity", + "An unexpected error was encountered when creating the empty Identity. "+ + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ + "Please report this to the provider developer:\n\n"+ + "Missing schema.", + ), + }, + }, + "version": { + input: &tfprotov6.UpgradeResourceIdentityRequest{ + Version: 123, + }, + identitySchema: testIdentitySchema, + expected: &fwserver.UpgradeResourceIdentityRequest{ + IdentitySchema: testIdentitySchema, + Version: 123, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + + if diff := cmp.Diff(got, testCase.expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { + t.Errorf("unexpected diagnostics difference: %s", diff) + } + }) + } +} diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go new file mode 100644 index 000000000..6bdd05d5b --- /dev/null +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -0,0 +1,240 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fwserver + +import ( + "context" + "fmt" + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" + "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/resource" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" +) + +// UpgradeResourceIdentityRequest is the framework server request for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityRequest struct { + Resource resource.Resource + IdentitySchema fwschema.Schema // TODO: Figure out if this was supposed to be a specifically identity schema + // TypeName is the type of resource that Terraform needs to upgrade the + // identity state for. + TypeName string + + // Version is the version of the identity state the resource currently has. + Version int64 + + // Using the tfprotov6 type here was a pragmatic effort decision around when + // the framework introduced compatibility promises. This type was chosen as + // it was readily available and trivial to convert between tfprotov5. + // + // Using a terraform-plugin-go type is not ideal for the framework as almost + // all terraform-plugin-go types have framework abstractions, but if there + // is ever a time where it makes sense to re-evaluate this decision, such as + // a major version bump, it could be changed then. + // Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/340 + RawState *tfprotov6.RawState +} + +// UpgradeResourceIdentityResponse is the framework server response for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityResponse struct { + UpgradedIdentity *tfsdk.ResourceIdentity + Diagnostics diag.Diagnostics +} + +// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC. +func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) { + if req == nil { + return + } + + // No UpgradedIdentity to return. This could return an error diagnostic about + // the odd scenario, but seems best to allow Terraform CLI to handle the + // situation itself in case it might be expected behavior. + if req.RawState == nil { + return + } + + // Define options to be used when unmarshalling raw state. + // IgnoreUndefinedAttributes will silently skip over fields in the JSON + // that do not have a matching entry in the schema. + unmarshalOpts := tfprotov6.UnmarshalOpts{ + ValueFromJSONOpts: tftypes.ValueFromJSONOpts{ + IgnoreUndefinedAttributes: true, + }, + } + + // TODO: throw error if the schemas are the same, it is a bug in core + + // Terraform CLI can call UpgradeResourceIdentity even if the stored Identity + // version matches the current schema. Presumably this is to account for + // the previous terraform-plugin-sdk implementation, which handled some + // Identity fixups on behalf of Terraform CLI. When this happens, we do not + // want to return errors for a missing ResourceWithUpgradeIdentity + // implementation or an undefined version within an existing + // ResourceWithUpgradeIdentity implementation as that would be confusing + // detail for provider developers. Instead, the framework will attempt to + // roundtrip the prior RawState to a Identity matching the current Schema. + // + // TODO: To prevent provider developers from accidentally implementing + // ResourceWithUpgradeIdentity with a version matching the current schema + // 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.IdentitySchema.GetVersion() { + logging.FrameworkTrace(ctx, "UpgradeResourceIdentity request version matches current Schema version, using framework defined passthrough implementation") + + identitySchemaType := req.IdentitySchema.Type().TerraformType(ctx) + + rawStateValue, err := req.RawState.UnmarshalWithOpts(identitySchemaType, unmarshalOpts) + + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "There was an error reading the saved resource Identity using the current resource schema.\n\n"+ + "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource schema. "+ + "Otherwise, please report this to the provider developer:\n\n"+err.Error(), + ) + return + } + + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Schema: req.IdentitySchema, + Raw: rawStateValue, + } + + return + }*/ + + if resourceWithConfigure, ok := req.Resource.(resource.ResourceWithConfigure); ok { + logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure") + + configureReq := resource.ConfigureRequest{ + ProviderData: s.ResourceConfigureData, + } + configureResp := resource.ConfigureResponse{} + + logging.FrameworkTrace(ctx, "Calling provider defined Resource Configure") + resourceWithConfigure.Configure(ctx, configureReq, &configureResp) + logging.FrameworkTrace(ctx, "Called provider defined Resource Configure") + + resp.Diagnostics.Append(configureResp.Diagnostics...) + + if resp.Diagnostics.HasError() { + return + } + } + + resourceWithUpgradeIdentity, ok := req.Resource.(resource.ResourceWithUpgradeIdentity) + + if !ok { + resp.Diagnostics.AddError( + "Unable to Upgrade Resource Identity", + "This resource was implemented without an UpgradeIdentity() method, "+ + fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ) + return + } + + logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") + + logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") + resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeResourceIdentity(ctx) + logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") + + // Panic prevention + if resourceIdentityUpgraders == nil { + resourceIdentityUpgraders = make(map[int64]resource.IdentityUpgrader, 0) + } + + resourceIdentityUpgrader, ok := resourceIdentityUpgraders[req.Version] + + if !ok { + resp.Diagnostics.AddError( + "Unable to Upgrade Resource Identity", + "This resource was implemented with an UpgradeIdentity() method, "+ + fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ) + return + } + + upgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ + RawState: req.RawState, + } + + if resourceIdentityUpgrader.PriorSchema != nil { + logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") + + priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) + + _, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts) + + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) + return + } + + upgradeResourceIdentityRequest.RawState = req.RawState + + } + + upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Schema: req.IdentitySchema, + // Raw is intentionally not set. + }, + } + + // To simplify provider logic, this could perform a best effort attempt + // to populate the response Identity by looping through all Attribute/Block + // by calling the equivalent of SetAttribute(GetAttribute()) and skipping + // any errors. + + logging.FrameworkTrace(ctx, "Calling provider defined IdentityUpgrader") + resourceIdentityUpgrader.IdentityUpgrader(ctx, upgradeResourceIdentityRequest, &upgradeResourceIdentityResponse) + logging.FrameworkTrace(ctx, "Called provider defined IdentityUpgrader") + + resp.Diagnostics.Append(upgradeResourceIdentityResponse.Diagnostics...) + + if resp.Diagnostics.HasError() { + return + } + + if upgradeResourceIdentityResponse.UpgradedIdentity.Raw.Type() == nil || upgradeResourceIdentityResponse.UpgradedIdentity.Raw.IsNull() { + resp.Diagnostics.AddError( + "Missing Upgraded Resource Identity", + fmt.Sprintf("After attempting a resource Identity upgrade to version %d, the provider did not return any Identity data. ", req.Version)+ + "Preventing the unexpected loss of resource Identity data. "+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ) + return + } + + if upgradeResourceIdentityResponse.UpgradedIdentity != nil { + logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse Raw State set, overriding State") + + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Schema: req.IdentitySchema, + Raw: upgradeResourceIdentityResponse.UpgradedIdentity.Raw, + } + + return + } + + resp.UpgradedIdentity = upgradeResourceIdentityResponse.UpgradedIdentity +} diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go new file mode 100644 index 000000000..a325ba02f --- /dev/null +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -0,0 +1,672 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package fwserver_test + +import ( + "context" + "encoding/json" + "fmt" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "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/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" +) + +func TestServerUpgradeResourceIdentity(t *testing.T) { + t.Parallel() + + ctx := context.Background() + /* testSchema := schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "optional_attribute": schema.StringAttribute{ + Optional: true, + }, + "required_attribute": schema.StringAttribute{ + Required: true, + }, + }, + Version: 1, // Must be above 0 + }*/ + + testIdentitySchema := identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "id": identityschema.StringAttribute{ + RequiredForImport: true, + }, + }, + Version: 1, + } + + schemaIdentityType := testIdentitySchema.Type().TerraformType(ctx) + + testCases := map[string]struct { + server *fwserver.Server + request *fwserver.UpgradeResourceIdentityRequest + expectedResponse *fwserver.UpgradeResourceIdentityResponse + }{ + "empty-provider": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + }, + "resource-configure-data": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + ResourceConfigureData: "test-provider-configure-value", + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithConfigureAndUpgradeIdentity{ + ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + providerData, ok := req.ProviderData.(string) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected ConfigureRequest.ProviderData", + fmt.Sprintf("Expected string, got: %T", req.ProviderData), + ) + return + } + + if providerData != "test-provider-configure-value" { + resp.Diagnostics.AddError( + "Unexpected ConfigureRequest.ProviderData", + fmt.Sprintf("Expected test-provider-configure-value, got: %q", providerData), + ) + } + }, + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + PriorSchema: &identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + }, + }, + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + // In practice, the Configure method would save the + // provider data to the Resource implementation and + // use it here. The fact that Configure is able to + // read the data proves this can work. + + var priorIdentityData struct { + Id string `tfsdk:"id"` + } + + //resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...) need to find the equivalent + // of this line for state for identity + + //rawStateValue, err := req.RawState.Unmarshal(schemaIdentityType) + // rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + // AttributeTypes: map[string]tftypes.Type{ + // "id": tftypes.String, + // }, + // }) + + // if err != nil { + // resp.Diagnostics.AddError( + // "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + // fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + // "Please report this to the provider developer:\n\n"+err.Error(), + // ) + // } + + // priorIdentityData.Id = rawStateValue + + if resp.Diagnostics.HasError() { + return + } + + upgradedIdentityData := struct { + Id string `tfsdk:"id"` + }{ + Id: priorIdentityData.Id, + } + + resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value + "id": tftypes.NewValue(tftypes.String, ""), + }), + Schema: testIdentitySchema, + }, + }, + }, + "RawState-missing": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + IdentitySchema: testIdentitySchema, + Resource: &testprovider.Resource{}, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + }, + "RawState-Unmarshal-and-ResourceIdentity": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + RawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "id": tftypes.String, + }, + }) + + if err != nil { + resp.Diagnostics.AddError( + "Unable to Unmarshal Prior Identity", + err.Error(), + ) + return + } + + var RawState map[string]tftypes.Value + + if err := RawStateValue.As(&RawState); err != nil { + resp.Diagnostics.AddError( + "Unable to Convert Prior Identity", + err.Error(), + ) + return + } + + ResourceIdentity := &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": RawState["id"], + }), + Schema: testIdentitySchema, + } + + if err != nil { + resp.Diagnostics.AddError( + "Unable to Convert Upgraded Identity", + err.Error(), + ) + return + } + + resp.UpgradedIdentity = ResourceIdentity + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + }, + }, + }, + "RawState-JSON-and-ResourceIdentity": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "required_attribute": true, + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + var RawState struct { + Id string `json:"id"` + } + + if err := json.Unmarshal(req.RawState.JSON, &RawState); err != nil { + resp.Diagnostics.AddError( + "Unable to Unmarshal Prior Identity", + err.Error(), + ) + return + } + + ResourceIdentity := tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, RawState.Id), + }), + Schema: testIdentitySchema, + } + + resp.UpgradedIdentity = &ResourceIdentity + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + }, + }, + }, + "ResourceType-UpgradeIdentity-not-implemented": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "required_attribute": true, + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.Resource{}, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Upgrade Resource Identity", + "This resource was implemented without an UpgradeIdentity() method, "+ + "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ), + }, + }, + }, + "ResourceType-UpgradeIdentity-empty": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "required_attribute": true, + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return nil + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Upgrade Resource Identity", + "This resource was implemented with an UpgradeIdentity() method, "+ + "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ), + }, + }, + }, + "PriorSchema-incorrect": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "optional_for_import_attribute": true, + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + PriorSchema: &identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "optional_for_import_attribute": identityschema.Int64Attribute{ // Purposefully incorrect + OptionalForImport: true, + }, + }, + }, + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + // Expect error before reaching this logic. + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "There was an error reading the saved resource Identity using the prior resource schema defined for version 0 upgrade.\n\n"+ + "Please report this to the provider developer:\n\n"+ + "AttributeName(\"optional_for_import_attribute\"): unsupported type bool sent as tftypes.Number", + ), + }, + }, + }, + "PriorSchema-and-Identity": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + PriorSchema: &testIdentitySchema, + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + var priorIdentityData struct { + Id string `tfsdk:"id"` + } + + // resp.Diagnostics.Append(req.Identity.Get(ctx, &priorIdentityData)...) + + if resp.Diagnostics.HasError() { + return + } + + upgradedIdentityData := struct { + Id string `tfsdk:"id"` + }{ + Id: priorIdentityData.Id, + } + + resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value + "id": tftypes.NewValue(tftypes.String, ""), + }), + Schema: testIdentitySchema, + }, + }, + }, + "PriorSchema-and-Identity-json-mismatch": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "nonexistent_attribute": "value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + PriorSchema: &testIdentitySchema, + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + var priorIdentityData struct { + Id string `tfsdk:"id"` + } + + // resp.Diagnostics.Append(req.RawState.Unmarshal()) + + if resp.Diagnostics.HasError() { + return + } + + upgradedIdentityData := struct { + Id string `tfsdk:"id"` + }{ + Id: priorIdentityData.Id, + } + + resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value + "id": tftypes.NewValue(tftypes.String, ""), + }), + Schema: testIdentitySchema, + }, + }, + }, + "UpgradedIdentity-missing": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity + }, + }, + } + }, + }, + Version: 0, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Missing Upgraded Resource Identity", + "After attempting a resource Identity upgrade to version 0, the provider did not return any Identity data. "+ + "Preventing the unexpected loss of resource Identity data. "+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ), + }, + }, + }, + /*"Version-current-flatmap": { // TODO: add tests back + server: &fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + } + }, + } + }, + }, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: &tfprotov6.RawState{ + Flatmap: map[string]string{ + "flatmap": "is not supported", + }, + }, + IdentitySchema: testIdentitySchema, + Resource: &testprovider.Resource{}, + Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "There was an error reading the saved resource Identity using the current resource schema.\n\n"+ + "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource schema. "+ + "Otherwise, please report this to the provider developer:\n\n"+ + "flatmap Identitys cannot be unmarshaled, only Identitys written by Terraform 0.12 and higher can be unmarshaled", + ), + }, + }, + }, + "Version-current-json-match": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + } + }, + } + }, + }, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.Resource{}, + Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + }, + }, + }, + "Version-current-json-mismatch": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + "nonexistent_attribute": "value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.Resource{}, + Version: 1, // Must match current tfsdk.IdentitySchema version to trigger framework implementation + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ + "id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + }, + }, + },*/ + "Version-not-implemented": { + server: &fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + request: &fwserver.UpgradeResourceIdentityRequest{ + RawState: testNewRawState(t, map[string]interface{}{ + "id": "test-id-value", + }), + IdentitySchema: testIdentitySchema, + Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return nil + }, + }, + Version: 999, + }, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewErrorDiagnostic( + "Unable to Upgrade Resource Identity", + "This resource was implemented with an UpgradeIdentity() method, "+ + "however Terraform was expecting an implementation for version 999 upgrade.\n\n"+ + "This is always an issue with the Terraform Provider and should be reported to the provider developer.", + ), + }, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + response := &fwserver.UpgradeResourceIdentityResponse{} + testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request, response) + + if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + } +} diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index f5b1979f1..6317e3048 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -5,11 +5,51 @@ package proto5server import ( "context" + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/logging" + "github.com/hashicorp/terraform-plugin-framework/internal/toproto5" "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) // UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { - panic("unimplemented") // TODO:ResourceIdentity: implement + // panic("unimplemented") // TODO:ResourceIdentity: implement + ctx = s.registerContext(ctx) + ctx = logging.InitContext(ctx) + + fwResp := &fwserver.UpgradeResourceIdentityResponse{} + + if proto5Req == nil { + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + resource, diags := s.FrameworkServer.Resource(ctx, proto5Req.TypeName) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + fwReq, diags := fromproto5.UpgradeResourceIdentityRequest(ctx, proto5Req, resource, identitySchema) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go new file mode 100644 index 000000000..78d7afa58 --- /dev/null +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -0,0 +1,296 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package proto5server + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tfprotov5" + "github.com/hashicorp/terraform-plugin-go/tftypes" +) + +func TestServerUpgradeResourceIdentity(t *testing.T) { + t.Parallel() + + testSchema := schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "optional_attribute": schema.StringAttribute{ + Optional: true, + }, + "required_attribute": schema.StringAttribute{ + Required: true, + }, + }, + Version: 1, // Must be above 0 + } + + testIdentitySchema := identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.StringAttribute{ + RequiredForImport: true, + }, + }, + } + + ctx := context.Background() + testIdentityType := testIdentitySchema.Type().TerraformType(ctx) + + testCases := map[string]struct { + server *Server + request *tfprotov5.UpgradeResourceIdentityRequest + expectedResponse *tfprotov5.UpgradeResourceIdentityResponse + expectedError error + }{ + "nil": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: nil, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{}, + }, + "request-RawIdentity": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }) + + if diff := cmp.Diff(req.RawState, expectedSourceIdentity); diff != "" { + resp.Diagnostics.AddError("Unexpected req.SourceIdentity difference", diff) + } + + // Prevent Missing Upgraded Resource Identity error + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + } + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov5.UpgradeResourceIdentityRequest{ + RawIdentity: testNewTfprotov5RawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov5.ResourceIdentityData{ + IdentityData: testNewDynamicValue(t, testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + }, + }, + }, + "request-TypeName-missing": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: &tfprotov5.UpgradeResourceIdentityRequest{}, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "Resource Type Not Found", + Detail: "No resource type named \"\" was found in the provider.", + }, + }, + }, + }, + "request-TypeName-unknown": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: &tfprotov5.UpgradeResourceIdentityRequest{ + TypeName: "unknown", + }, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "Resource Type Not Found", + Detail: "No resource type named \"unknown\" was found in the provider.", + }, + }, + }, + }, + "response-Diagnostics": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov5.UpgradeResourceIdentityRequest{ + RawIdentity: testNewTfprotov5RawState(t, map[string]interface{}{ + "test_id": "test-id-value", + "required_attribute": true, + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityWarning, + Summary: "warning summary", + Detail: "warning detail", + }, + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "error summary", + Detail: "error detail", + }, + }, + }, + }, + "response-UpgradedIdentity": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + } + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov5.UpgradeResourceIdentityRequest{ + RawIdentity: testNewTfprotov5RawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov5.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov5.ResourceIdentityData{ + IdentityData: testNewDynamicValue(t, testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + }, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + + if diff := cmp.Diff(testCase.expectedError, err); diff != "" { + t.Errorf("unexpected error difference: %s", diff) + } + + if diff := cmp.Diff(testCase.expectedResponse, got); diff != "" { + t.Errorf("unexpected response difference: %s", diff) + } + }) + } +} diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index 042efce8e..5926ebe6f 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -5,11 +5,51 @@ package proto6server import ( "context" + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto6" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/logging" + "github.com/hashicorp/terraform-plugin-framework/internal/toproto6" "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) // UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { - panic("unimplemented") // TODO:ResourceIdentity: implement + // panic("unimplemented") // TODO:ResourceIdentity: implement + ctx = s.registerContext(ctx) + ctx = logging.InitContext(ctx) + + fwResp := &fwserver.UpgradeResourceIdentityResponse{} + + if proto6Req == nil { + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + resource, diags := s.FrameworkServer.Resource(ctx, proto6Req.TypeName) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + fwReq, diags := fromproto6.UpgradeResourceIdentityRequest(ctx, proto6Req, resource, identitySchema) + + fwResp.Diagnostics.Append(diags...) + + if fwResp.Diagnostics.HasError() { + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + } + + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go new file mode 100644 index 000000000..7299602d7 --- /dev/null +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -0,0 +1,296 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package proto6server + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/hashicorp/terraform-plugin-go/tftypes" +) + +func TestServerUpgradeResourceIdentity(t *testing.T) { + t.Parallel() + + testSchema := schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "optional_attribute": schema.StringAttribute{ + Optional: true, + }, + "required_attribute": schema.StringAttribute{ + Required: true, + }, + }, + Version: 1, // Must be above 0 + } + + testIdentitySchema := identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.StringAttribute{ + RequiredForImport: true, + }, + }, + } + + ctx := context.Background() + testIdentityType := testIdentitySchema.Type().TerraformType(ctx) + + testCases := map[string]struct { + server *Server + request *tfprotov6.UpgradeResourceIdentityRequest + expectedResponse *tfprotov6.UpgradeResourceIdentityResponse + expectedError error + }{ + "nil": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: nil, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{}, + }, + "request-RawIdentity": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }) + + if diff := cmp.Diff(req.RawState, expectedSourceIdentity); diff != "" { + resp.Diagnostics.AddError("Unexpected req.SourceIdentity difference", diff) + } + + // Prevent Missing Upgraded Resource Identity error + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + } + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov6.UpgradeResourceIdentityRequest{ + RawIdentity: testNewRawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov6.ResourceIdentityData{ + IdentityData: testNewDynamicValue(t, testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + }, + }, + }, + "request-TypeName-missing": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: &tfprotov6.UpgradeResourceIdentityRequest{}, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "Resource Type Not Found", + Detail: "No resource type named \"\" was found in the provider.", + }, + }, + }, + }, + "request-TypeName-unknown": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{}, + }, + }, + request: &tfprotov6.UpgradeResourceIdentityRequest{ + TypeName: "unknown", + }, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "Resource Type Not Found", + Detail: "No resource type named \"unknown\" was found in the provider.", + }, + }, + }, + }, + "response-Diagnostics": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov6.UpgradeResourceIdentityRequest{ + RawIdentity: testNewRawState(t, map[string]interface{}{ + "test_id": "test-id-value", + "required_attribute": true, + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityWarning, + Summary: "warning summary", + Detail: "warning detail", + }, + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "error summary", + Detail: "error detail", + }, + }, + }, + }, + "response-UpgradedIdentity": { + server: &Server{ + FrameworkServer: fwserver.Server{ + Provider: &testprovider.Provider{ + ResourcesMethod: func(_ context.Context) []func() resource.Resource { + return []func() resource.Resource{ + func() resource.Resource { + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{ + SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = testSchema + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, + }, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + Schema: testIdentitySchema, + } + }, + }, + } + }, + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + resp.IdentitySchema = testIdentitySchema + }, + } + }, + } + }, + }, + }, + }, + request: &tfprotov6.UpgradeResourceIdentityRequest{ + RawIdentity: testNewRawState(t, map[string]interface{}{ + "test_id": "test-id-value", + }), + TypeName: "test_resource", + Version: 0, + }, + expectedResponse: &tfprotov6.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov6.ResourceIdentityData{ + IdentityData: testNewDynamicValue(t, testIdentityType, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), + }), + }, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + + if diff := cmp.Diff(testCase.expectedError, err); diff != "" { + t.Errorf("unexpected error difference: %s", diff) + } + + if diff := cmp.Diff(testCase.expectedResponse, got); diff != "" { + t.Errorf("unexpected response difference: %s", diff) + } + }) + } +} diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go new file mode 100644 index 000000000..b7bdbd703 --- /dev/null +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -0,0 +1,43 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testprovider + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/resource" +) + +var _ resource.Resource = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithConfigureAndUpgradeIdentity{} + +// Declarative resource.ResourceWithConfigureAndUpgradeIdentity for unit testing. +type ResourceWithConfigureAndUpgradeIdentity struct { + *Resource + + // ResourceWithConfigureAndUpgradeIdentity interface methods + ConfigureMethod func(context.Context, resource.ConfigureRequest, *resource.ConfigureResponse) + + // ResourceWithUpgradeIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader +} + +// Configure satisfies the resource.ResourceWithConfigureAndUpgradeIdentity interface. +func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + if r.ConfigureMethod == nil { + return + } + + r.ConfigureMethod(ctx, req, resp) +} + +// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. +func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if r.UpgradeResourceIdentityMethod == nil { + return nil + } + + return r.UpgradeResourceIdentityMethod(ctx) +} diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go new file mode 100644 index 000000000..c57b8336b --- /dev/null +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -0,0 +1,42 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testprovider + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/resource" +) + +var _ resource.Resource = &ResourceWithUpgradeIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithUpgradeIdentity{} + +// Declarative resource.ResourceWithUpgradeIdentity for unit testing. +type ResourceWithUpgradeIdentity struct { + *Resource + + // ResourceWithUpgradeIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + + // ResourceWithIdentity interface methods + IdentitySchemaMethod func(context.Context, resource.IdentitySchemaRequest, *resource.IdentitySchemaResponse) +} + +// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. +func (p *ResourceWithUpgradeIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if p.UpgradeResourceIdentityMethod == nil { + return nil + } + + return p.UpgradeResourceIdentityMethod(ctx) +} + +// IdentitySchema implements resource.ResourceWithIdentity. +func (p *ResourceWithUpgradeIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { + if p.IdentitySchemaMethod == nil { + return + } + + p.IdentitySchemaMethod(ctx, req, resp) +} diff --git a/internal/toproto5/upgraderesourceidentity.go b/internal/toproto5/upgraderesourceidentity.go new file mode 100644 index 000000000..57ea1d9a8 --- /dev/null +++ b/internal/toproto5/upgraderesourceidentity.go @@ -0,0 +1,30 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package toproto5 + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-go/tfprotov5" +) + +// UpgradeResourceIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeResourceIdentityResponse. +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { + if fw == nil { + return nil + } + + proto5 := &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: Diagnostics(ctx, fw.Diagnostics), + } + + upgradedIdentity, diags := ResourceIdentity(ctx, fw.UpgradedIdentity) + + proto5.Diagnostics = append(proto5.Diagnostics, Diagnostics(ctx, diags)...) + proto5.UpgradedIdentity = upgradedIdentity + + return proto5 +} diff --git a/internal/toproto5/upgraderesourceidentity_test.go b/internal/toproto5/upgraderesourceidentity_test.go new file mode 100644 index 000000000..619303bd0 --- /dev/null +++ b/internal/toproto5/upgraderesourceidentity_test.go @@ -0,0 +1,149 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package toproto5_test + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/toproto5" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tfprotov5" + "github.com/hashicorp/terraform-plugin-go/tftypes" +) + +func TestUpgradeResourceIdentityResponse(t *testing.T) { + t.Parallel() + + testIdentityProto5Type := tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "test_id": tftypes.String, + }, + } + + testIdentityProto5Value := tftypes.NewValue(testIdentityProto5Type, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "id-123"), + }) + + testIdentityProto5DynamicValue, err := tfprotov5.NewDynamicValue(testIdentityProto5Type, testIdentityProto5Value) + + if err != nil { + t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err) + } + + testIdentity := &tfsdk.ResourceIdentity{ + Raw: testIdentityProto5Value, + Schema: identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.StringAttribute{ + RequiredForImport: true, + }, + }, + }, + } + + testIdentityInvalid := &tfsdk.ResourceIdentity{ + Raw: testIdentityProto5Value, + Schema: identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.BoolAttribute{ + RequiredForImport: true, + }, + }, + }, + } + + testCases := map[string]struct { + input *fwserver.UpgradeResourceIdentityResponse + expected *tfprotov5.UpgradeResourceIdentityResponse + }{ + "nil": { + input: nil, + expected: nil, + }, + "empty": { + input: &fwserver.UpgradeResourceIdentityResponse{}, + expected: &tfprotov5.UpgradeResourceIdentityResponse{}, + }, + "diagnostics": { + input: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewWarningDiagnostic("test warning summary", "test warning details"), + diag.NewErrorDiagnostic("test error summary", "test error details"), + }, + }, + expected: &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityWarning, + Summary: "test warning summary", + Detail: "test warning details", + }, + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "test error summary", + Detail: "test error details", + }, + }, + }, + }, + "diagnostics-invalid-upgradedIdentity": { + input: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewWarningDiagnostic("test warning summary", "test warning details"), + diag.NewErrorDiagnostic("test error summary", "test error details"), + }, + UpgradedIdentity: testIdentityInvalid, + }, + expected: &tfprotov5.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityWarning, + Summary: "test warning summary", + Detail: "test warning details", + }, + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "test error summary", + Detail: "test error details", + }, + { + Severity: tfprotov5.DiagnosticSeverityError, + Summary: "Unable to Convert Resource Identity", + Detail: "An unexpected error was encountered when converting the resource identity to the protocol type. " + + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n" + + "Please report this to the provider developer:\n\n" + + "Unable to create DynamicValue: AttributeName(\"test_id\"): unexpected value type string, tftypes.Bool values must be of type bool", + }, + }, + }, + }, + "upgradedIdentity": { + input: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: testIdentity, + }, + expected: &tfprotov5.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov5.ResourceIdentityData{ + IdentityData: &testIdentityProto5DynamicValue, + }, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got := toproto5.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + + if diff := cmp.Diff(got, testCase.expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + } +} diff --git a/internal/toproto6/upgraderesourceidentity.go b/internal/toproto6/upgraderesourceidentity.go new file mode 100644 index 000000000..2748a4172 --- /dev/null +++ b/internal/toproto6/upgraderesourceidentity.go @@ -0,0 +1,30 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package toproto6 + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +// UpgradeResourceIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeResourceIdentityResponse. +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { + if fw == nil { + return nil + } + + proto6 := &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: Diagnostics(ctx, fw.Diagnostics), + } + + upgradedIdentity, diags := ResourceIdentity(ctx, fw.UpgradedIdentity) + + proto6.Diagnostics = append(proto6.Diagnostics, Diagnostics(ctx, diags)...) + proto6.UpgradedIdentity = upgradedIdentity + + return proto6 +} diff --git a/internal/toproto6/upgraderesourceidentity_test.go b/internal/toproto6/upgraderesourceidentity_test.go new file mode 100644 index 000000000..191d51832 --- /dev/null +++ b/internal/toproto6/upgraderesourceidentity_test.go @@ -0,0 +1,149 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package toproto6_test + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" + "github.com/hashicorp/terraform-plugin-framework/internal/toproto6" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tftypes" +) + +func TestUpgradeResourceIdentityResponse(t *testing.T) { + t.Parallel() + + testIdentityProto6Type := tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "test_id": tftypes.String, + }, + } + + testIdentityProto6Value := tftypes.NewValue(testIdentityProto6Type, map[string]tftypes.Value{ + "test_id": tftypes.NewValue(tftypes.String, "id-123"), + }) + + testIdentityProto6DynamicValue, err := tfprotov6.NewDynamicValue(testIdentityProto6Type, testIdentityProto6Value) + + if err != nil { + t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err) + } + + testIdentity := &tfsdk.ResourceIdentity{ + Raw: testIdentityProto6Value, + Schema: identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.StringAttribute{ + RequiredForImport: true, + }, + }, + }, + } + + testIdentityInvalid := &tfsdk.ResourceIdentity{ + Raw: testIdentityProto6Value, + Schema: identityschema.Schema{ + Attributes: map[string]identityschema.Attribute{ + "test_id": identityschema.BoolAttribute{ + RequiredForImport: true, + }, + }, + }, + } + + testCases := map[string]struct { + input *fwserver.UpgradeResourceIdentityResponse + expected *tfprotov6.UpgradeResourceIdentityResponse + }{ + "nil": { + input: nil, + expected: nil, + }, + "empty": { + input: &fwserver.UpgradeResourceIdentityResponse{}, + expected: &tfprotov6.UpgradeResourceIdentityResponse{}, + }, + "diagnostics": { + input: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewWarningDiagnostic("test warning summary", "test warning details"), + diag.NewErrorDiagnostic("test error summary", "test error details"), + }, + }, + expected: &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityWarning, + Summary: "test warning summary", + Detail: "test warning details", + }, + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "test error summary", + Detail: "test error details", + }, + }, + }, + }, + "diagnostics-invalid-upgradedIdentity": { + input: &fwserver.UpgradeResourceIdentityResponse{ + Diagnostics: diag.Diagnostics{ + diag.NewWarningDiagnostic("test warning summary", "test warning details"), + diag.NewErrorDiagnostic("test error summary", "test error details"), + }, + UpgradedIdentity: testIdentityInvalid, + }, + expected: &tfprotov6.UpgradeResourceIdentityResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityWarning, + Summary: "test warning summary", + Detail: "test warning details", + }, + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "test error summary", + Detail: "test error details", + }, + { + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "Unable to Convert Resource Identity", + Detail: "An unexpected error was encountered when converting the resource identity to the protocol type. " + + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n" + + "Please report this to the provider developer:\n\n" + + "Unable to create DynamicValue: AttributeName(\"test_id\"): unexpected value type string, tftypes.Bool values must be of type bool", + }, + }, + }, + }, + "upgradedIdentity": { + input: &fwserver.UpgradeResourceIdentityResponse{ + UpgradedIdentity: testIdentity, + }, + expected: &tfprotov6.UpgradeResourceIdentityResponse{ + UpgradedIdentity: &tfprotov6.ResourceIdentityData{ + IdentityData: &testIdentityProto6DynamicValue, + }, + }, + }, + } + + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + got := toproto6.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + + if diff := cmp.Diff(got, testCase.expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + } +} diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go new file mode 100644 index 000000000..f75f178e8 --- /dev/null +++ b/resource/identity_upgrader.go @@ -0,0 +1,49 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package resource + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" + "github.com/hashicorp/terraform-plugin-go/tftypes" +) + +// Implementation handler for a UpgradeState operation. +// +// This is used to encapsulate all upgrade logic from a prior state to the +// current schema version when a Resource implements the +// ResourceWithUpgradeState interface. +type IdentityUpgrader struct { + // Schema information for the prior state version. While not required, + // setting this will populate the UpgradeStateRequest type State + // field similar to other Resource data types. This allows for easier data + // handling such as calling Get() or GetAttribute(). + // + // If not set, prior state data is available in the + // UpgradeResourceStateRequest type RawState field. + PriorSchema *identityschema.Schema + + // Version is the version schema that this Upgrader will handle, converting + // it to Version+1. + Version int64 + + // Type describes the schema that this function can upgrade. Type is + // required to decode the schema if the state was stored in a legacy + // flatmap format. + Type tftypes.Type + + // Provider defined logic for upgrading a resource state from the prior + // state version to the current schema version. + // + // The context.Context parameter contains framework-defined loggers and + // supports request cancellation. + // + // The UpgradeStateRequest parameter contains the prior state data. + // If PriorSchema was set, the State field will be available. Otherwise, + // the RawState must be used. + // + // The UpgradeStateResponse parameter should contain the upgraded + // state data and can be used to signal any logic warnings or errors. + IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) +} diff --git a/resource/resource.go b/resource/resource.go index 2cf334a77..f5dcd54e8 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -223,3 +223,18 @@ type ResourceWithIdentity interface { // IdentitySchema should return the identity schema for this resource. IdentitySchema(context.Context, IdentitySchemaRequest, *IdentitySchemaResponse) } + +type ResourceWithUpgradeIdentity interface { + Resource + + // A mapping of prior state version to current schema version state upgrade + // implementations. Only the specified state upgrader for the prior state + // version is called, rather than each version in between, so it must + // encapsulate all logic to convert the prior state to the current schema + // version. + // + // Version keys begin at 0, which is the default schema version when + // undefined. The framework will return an error diagnostic should the + // requested state version not be implemented. + UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader +} diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go new file mode 100644 index 000000000..99b414d92 --- /dev/null +++ b/resource/upgrade_identity.go @@ -0,0 +1,37 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package resource + +import ( + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +// Request information for the provider logic to update a resource identity +// from a prior resource identity version to the current identity version. +type UpgradeResourceIdentityRequest struct { + // TypeName is the type of resource that Terraform needs to upgrade the + // identity state for. + TypeName string + + // Version is the version of the identity state the resource currently has. + Version int64 + + // RawIdentity is the identity state as Terraform sees it right now in JSON. See the + // documentation for `RawIdentity` for information on how to work with the + // data it contains. + RawState *tfprotov6.RawState +} + +// Response information for the provider logic to update a resource identity +// from a prior resource identity version to the current identity version. +type UpgradeResourceIdentityResponse struct { + UpgradedIdentity *tfsdk.ResourceIdentity + + // Diagnostics report errors or warnings related to retrieving the resource + // identity schema. An empty slice indicates success, with no warnings + // or errors generated. + Diagnostics diag.Diagnostics +} From d55f85bbc864dbaedb3faf0bcc26d83bb0fb0a13 Mon Sep 17 00:00:00 2001 From: Rain Date: Fri, 2 May 2025 13:25:19 -0400 Subject: [PATCH 02/11] Updated tests in server_upgraderesourceidentity_test.go --- .../server_upgraderesourceidentity.go | 4 +- .../server_upgraderesourceidentity_test.go | 196 ++++++++---------- 2 files changed, 84 insertions(+), 116 deletions(-) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index 6bdd05d5b..013d1fc21 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -20,7 +20,7 @@ import ( // UpgradeResourceIdentity RPC. type UpgradeResourceIdentityRequest struct { Resource resource.Resource - IdentitySchema fwschema.Schema // TODO: Figure out if this was supposed to be a specifically identity schema + IdentitySchema fwschema.Schema // TypeName is the type of resource that Terraform needs to upgrade the // identity state for. TypeName string @@ -69,7 +69,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour }, } - // TODO: throw error if the schemas are the same, it is a bug in core + // TODO: Maybe throw error if the schemas are the same, it is a bug in core // Terraform CLI can call UpgradeResourceIdentity even if the stored Identity // version matches the current schema. Presumably this is to account for diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index a325ba02f..d8020e720 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -25,20 +25,6 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Parallel() ctx := context.Background() - /* testSchema := schema.Schema{ - Attributes: map[string]schema.Attribute{ - "id": schema.StringAttribute{ - Computed: true, - }, - "optional_attribute": schema.StringAttribute{ - Optional: true, - }, - "required_attribute": schema.StringAttribute{ - Required: true, - }, - }, - Version: 1, // Must be above 0 - }*/ testIdentitySchema := identityschema.Schema{ Attributes: map[string]identityschema.Attribute{ @@ -108,38 +94,35 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { // use it here. The fact that Configure is able to // read the data proves this can work. - var priorIdentityData struct { - Id string `tfsdk:"id"` - } - - //resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...) need to find the equivalent - // of this line for state for identity - - //rawStateValue, err := req.RawState.Unmarshal(schemaIdentityType) - // rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ - // AttributeTypes: map[string]tftypes.Type{ - // "id": tftypes.String, - // }, - // }) - - // if err != nil { - // resp.Diagnostics.AddError( - // "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - // fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ - // "Please report this to the provider developer:\n\n"+err.Error(), - // ) - // } - - // priorIdentityData.Id = rawStateValue + rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "id": tftypes.String, + }, + }) - if resp.Diagnostics.HasError() { + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) + return + } + rawValues := make(map[string]tftypes.Value) + err = rawStateValue.As(&rawValues) + if err != nil { + resp.Diagnostics.AddError( + "Unable to convert raw state value into prior identity struct", + fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) return } upgradedIdentityData := struct { Id string `tfsdk:"id"` }{ - Id: priorIdentityData.Id, + Id: id, } resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) @@ -153,8 +136,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ - //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value - "id": tftypes.NewValue(tftypes.String, ""), + "id": tftypes.NewValue(tftypes.String, "test-id-value"), }), Schema: testIdentitySchema, }, @@ -408,71 +390,49 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { 0: { PriorSchema: &testIdentitySchema, IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - var priorIdentityData struct { - Id string `tfsdk:"id"` - } - - // resp.Diagnostics.Append(req.Identity.Get(ctx, &priorIdentityData)...) + rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + AttributeTypes: map[string]tftypes.Type{ + "id": tftypes.String, + }, + }) - if resp.Diagnostics.HasError() { + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) return } - - upgradedIdentityData := struct { - Id string `tfsdk:"id"` - }{ - Id: priorIdentityData.Id, - } - - resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) - }, - }, - } - }, - }, - Version: 0, - }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ - UpgradedIdentity: &tfsdk.ResourceIdentity{ - Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ - //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value - "id": tftypes.NewValue(tftypes.String, ""), - }), - Schema: testIdentitySchema, - }, - }, - }, - "PriorSchema-and-Identity-json-mismatch": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{}, - }, - request: &fwserver.UpgradeResourceIdentityRequest{ - RawState: testNewRawState(t, map[string]interface{}{ - "id": "test-id-value", - "nonexistent_attribute": "value", - }), - IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ - Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { - return map[int64]resource.IdentityUpgrader{ - 0: { - PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - var priorIdentityData struct { - Id string `tfsdk:"id"` + rawValues := make(map[string]tftypes.Value) + err = rawStateValue.As(&rawValues) + if err != nil { + resp.Diagnostics.AddError( + "Unable to convert raw state value into prior identity struct", + fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) + return } - // resp.Diagnostics.Append(req.RawState.Unmarshal()) - - if resp.Diagnostics.HasError() { - return + priorIdentityId := rawValues["id"] + var id string + if priorIdentityId.Type().Is(tftypes.String) { + err := priorIdentityId.As(&id) + if err != nil { + resp.Diagnostics.AddError( + "Unable to convert raw state id value into string", + fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) + return + } } upgradedIdentityData := struct { Id string `tfsdk:"id"` }{ - Id: priorIdentityData.Id, + Id: id, } resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) @@ -486,8 +446,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ - //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value - "id": tftypes.NewValue(tftypes.String, ""), + "id": tftypes.NewValue(tftypes.String, "test-id-value"), }), Schema: testIdentitySchema, }, @@ -527,18 +486,23 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - /*"Version-current-flatmap": { // TODO: add tests back + /*"Version-current-flatmap": { // TODO: See if we need to add these tests back server: &fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.Resource{ - SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { - resp.Schema = testSchema - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeIdentity{ + + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity + }, + }, + } }, } }, @@ -553,7 +517,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, IdentitySchema: testIdentitySchema, - Resource: &testprovider.Resource{}, + Resource: &testprovider.ResourceWithUpgradeIdentity{}, Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation }, expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ @@ -575,12 +539,16 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.Resource{ - SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { - resp.Schema = testSchema - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.Resource{}, + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + return map[int64]resource.IdentityUpgrader{ + 0: { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity + }, + }, + } }, } }, @@ -593,7 +561,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.Resource{}, + Resource: &testprovider.ResourceWithUpgradeIdentity{}, Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation }, expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ @@ -615,7 +583,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { "nonexistent_attribute": "value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.Resource{}, + Resource: &testprovider.ResourceWithUpgradeIdentity{}, Version: 1, // Must match current tfsdk.IdentitySchema version to trigger framework implementation }, expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ From 7dbd19ed619af4803193c8e3dcce46d519d4894f Mon Sep 17 00:00:00 2001 From: Rain Date: Fri, 2 May 2025 13:25:19 -0400 Subject: [PATCH 03/11] Updated tests in server_upgraderesourceidentity_test.go --- .../fwserver/server_upgraderesourceidentity_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index d8020e720..0c96e3d58 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -118,6 +118,19 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ) return } + priorIdentityId := rawValues["id"] + var id string + if priorIdentityId.Type().Is(tftypes.String) { + err := priorIdentityId.As(&id) + if err != nil { + resp.Diagnostics.AddError( + "Unable to convert raw state id value into string", + fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Version)+ + "Please report this to the provider developer:\n\n"+err.Error(), + ) + return + } + } upgradedIdentityData := struct { Id string `tfsdk:"id"` From 9517607f66a6f2877e3eca9b33e975bf81902a3d Mon Sep 17 00:00:00 2001 From: Rain Date: Fri, 2 May 2025 13:56:32 -0400 Subject: [PATCH 04/11] Updated tests in server_upgraderesourceidentity_test.go --- internal/fwserver/server_upgraderesourceidentity_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index 0c96e3d58..5deaf6237 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -118,6 +118,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ) return } + priorIdentityId := rawValues["id"] var id string if priorIdentityId.Type().Is(tftypes.String) { From 5db6046755deac3d9eac3f724cc0b14dd2deba30 Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 5 May 2025 13:26:48 -0400 Subject: [PATCH 05/11] Addressed PR commenta --- .../server_upgraderesourceidentity.go | 72 +++-------- .../server_upgraderesourceidentity_test.go | 117 +----------------- .../server_upgraderesourceidentity.go | 1 - .../server_upgraderesourceidentity_test.go | 5 +- .../server_upgraderesourceidentity.go | 1 - .../server_upgraderesourceidentity_test.go | 5 +- resource/identity_upgrader.go | 34 +++-- resource/resource.go | 8 +- resource/upgrade_identity.go | 7 +- 9 files changed, 56 insertions(+), 194 deletions(-) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index 013d1fc21..73dac6d69 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -69,51 +69,16 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour }, } - // TODO: Maybe throw error if the schemas are the same, it is a bug in core - - // Terraform CLI can call UpgradeResourceIdentity even if the stored Identity - // version matches the current schema. Presumably this is to account for - // the previous terraform-plugin-sdk implementation, which handled some - // Identity fixups on behalf of Terraform CLI. When this happens, we do not - // want to return errors for a missing ResourceWithUpgradeIdentity - // implementation or an undefined version within an existing - // ResourceWithUpgradeIdentity implementation as that would be confusing - // detail for provider developers. Instead, the framework will attempt to - // roundtrip the prior RawState to a Identity matching the current Schema. - // - // TODO: To prevent provider developers from accidentally implementing - // ResourceWithUpgradeIdentity with a version matching the current schema - // 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.IdentitySchema.GetVersion() { - logging.FrameworkTrace(ctx, "UpgradeResourceIdentity request version matches current Schema version, using framework defined passthrough implementation") - - identitySchemaType := req.IdentitySchema.Type().TerraformType(ctx) - - rawStateValue, err := req.RawState.UnmarshalWithOpts(identitySchemaType, unmarshalOpts) - - if err != nil { - resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - "There was an error reading the saved resource Identity using the current resource schema.\n\n"+ - "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource schema. "+ - "Otherwise, please report this to the provider developer:\n\n"+err.Error(), - ) - return - } - - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ - Schema: req.IdentitySchema, - Raw: rawStateValue, - } - + if req.Version == req.IdentitySchema.GetVersion() { + resp.Diagnostics.AddError( + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "There was an error reading the saved resource identity using the current resource identity schema.\n\n"+ + "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource identity schema. "+ + "Otherwise, please report this to the provider developer:\n\n", + ) return - }*/ + } if resourceWithConfigure, ok := req.Resource.(resource.ResourceWithConfigure); ok { logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure") @@ -178,7 +143,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) - _, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts) + rawIdentityValue, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts) if err != nil { resp.Diagnostics.AddError( @@ -189,12 +154,15 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - upgradeResourceIdentityRequest.RawState = req.RawState + upgradeResourceIdentityRequest.Identity = &tfsdk.ResourceIdentity{ + Raw: rawIdentityValue, // from the output of req.RawState.UnmarshalWithOpts + Schema: *resourceIdentityUpgrader.PriorSchema, + } } upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ - UpgradedIdentity: &tfsdk.ResourceIdentity{ + Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. }, @@ -215,7 +183,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - if upgradeResourceIdentityResponse.UpgradedIdentity.Raw.Type() == nil || upgradeResourceIdentityResponse.UpgradedIdentity.Raw.IsNull() { + if upgradeResourceIdentityResponse.Identity.Raw.Type() == nil || upgradeResourceIdentityResponse.Identity.Raw.IsNull() { resp.Diagnostics.AddError( "Missing Upgraded Resource Identity", fmt.Sprintf("After attempting a resource Identity upgrade to version %d, the provider did not return any Identity data. ", req.Version)+ @@ -225,16 +193,16 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - if upgradeResourceIdentityResponse.UpgradedIdentity != nil { - logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse Raw State set, overriding State") + if upgradeResourceIdentityResponse.Identity.Raw.Type() != nil { + logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse RawState set, overriding State") resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, - Raw: upgradeResourceIdentityResponse.UpgradedIdentity.Raw, + Raw: upgradeResourceIdentityResponse.Identity.Raw, } return } - resp.UpgradedIdentity = upgradeResourceIdentityResponse.UpgradedIdentity + resp.UpgradedIdentity = upgradeResourceIdentityResponse.Identity } diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index 5deaf6237..6ff2f30b3 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -139,7 +139,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { Id: id, } - resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) + resp.Diagnostics.Append(resp.Identity.Set(ctx, upgradedIdentityData)...) }, }, } @@ -221,7 +221,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { return } - resp.UpgradedIdentity = ResourceIdentity + resp.Identity = ResourceIdentity }, }, } @@ -273,7 +273,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { Schema: testIdentitySchema, } - resp.UpgradedIdentity = &ResourceIdentity + resp.Identity = &ResourceIdentity }, }, } @@ -449,7 +449,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { Id: id, } - resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...) + resp.Diagnostics.Append(resp.Identity.Set(ctx, upgradedIdentityData)...) }, }, } @@ -500,115 +500,6 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - /*"Version-current-flatmap": { // TODO: See if we need to add these tests back - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ - - Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { - return map[int64]resource.IdentityUpgrader{ - 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity - }, - }, - } - }, - } - }, - } - }, - }, - }, - request: &fwserver.UpgradeResourceIdentityRequest{ - RawState: &tfprotov6.RawState{ - Flatmap: map[string]string{ - "flatmap": "is not supported", - }, - }, - IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{}, - Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation - }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ - Diagnostics: diag.Diagnostics{ - diag.NewErrorDiagnostic( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - "There was an error reading the saved resource Identity using the current resource schema.\n\n"+ - "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource schema. "+ - "Otherwise, please report this to the provider developer:\n\n"+ - "flatmap Identitys cannot be unmarshaled, only Identitys written by Terraform 0.12 and higher can be unmarshaled", - ), - }, - }, - }, - "Version-current-json-match": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ - Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { - return map[int64]resource.IdentityUpgrader{ - 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity - }, - }, - } - }, - } - }, - } - }, - }, - }, - request: &fwserver.UpgradeResourceIdentityRequest{ - RawState: testNewRawState(t, map[string]interface{}{ - "id": "test-id-value", - }), - IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{}, - Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation - }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ - UpgradedIdentity: &tfsdk.ResourceIdentity{ - Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ - "id": tftypes.NewValue(tftypes.String, "test-id-value"), - }), - Schema: testIdentitySchema, - }, - }, - }, - "Version-current-json-mismatch": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{}, - }, - request: &fwserver.UpgradeResourceIdentityRequest{ - RawState: testNewRawState(t, map[string]interface{}{ - "id": "test-id-value", - "nonexistent_attribute": "value", - }), - IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{}, - Version: 1, // Must match current tfsdk.IdentitySchema version to trigger framework implementation - }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ - UpgradedIdentity: &tfsdk.ResourceIdentity{ - Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ - "id": tftypes.NewValue(tftypes.String, "test-id-value"), - }), - Schema: testIdentitySchema, - }, - }, - },*/ "Version-not-implemented": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index 6317e3048..a0dcfb463 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -15,7 +15,6 @@ import ( // UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { - // panic("unimplemented") // TODO:ResourceIdentity: implement ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 78d7afa58..48f211a8b 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -42,6 +42,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { RequiredForImport: true, }, }, + Version: 1, // Must be above 0 } ctx := context.Background() @@ -91,7 +92,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { } // Prevent Missing Upgraded Resource Identity error - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), }), @@ -241,7 +242,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { return map[int64]resource.IdentityUpgrader{ 0: { IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), }), diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index 5926ebe6f..7484eb49f 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -15,7 +15,6 @@ import ( // UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { - // panic("unimplemented") // TODO:ResourceIdentity: implement ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index 7299602d7..87e1aaf8a 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -42,6 +42,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { RequiredForImport: true, }, }, + Version: 1, // Must be above 0 } ctx := context.Background() @@ -91,7 +92,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { } // Prevent Missing Upgraded Resource Identity error - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), }), @@ -241,7 +242,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { return map[int64]resource.IdentityUpgrader{ 0: { IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ + resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), }), diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index f75f178e8..5d6641be9 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -9,41 +9,39 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -// Implementation handler for a UpgradeState operation. +// Implementation handler for an UpgradeIdentity operation. // -// This is used to encapsulate all upgrade logic from a prior state to the -// current schema version when a Resource implements the -// ResourceWithUpgradeState interface. +// This is used to encapsulate all upgrade logic from a prior identity to the +// current version when a Resource implements the +// ResourceWithUpgradeIdentity interface. type IdentityUpgrader struct { - // Schema information for the prior state version. While not required, - // setting this will populate the UpgradeStateRequest type State + // Schema information for the prior identity version. While not required, + // setting this will populate the UpgradeResourceIdentityRequest type Identity // field similar to other Resource data types. This allows for easier data // handling such as calling Get() or GetAttribute(). // - // If not set, prior state data is available in the - // UpgradeResourceStateRequest type RawState field. + // If not set, prior identity data is available in the + // UpgradeResourceIdentityRequest type RawIdentity field. PriorSchema *identityschema.Schema // Version is the version schema that this Upgrader will handle, converting // it to Version+1. Version int64 - // Type describes the schema that this function can upgrade. Type is - // required to decode the schema if the state was stored in a legacy - // flatmap format. + // Type describes the schema that this function can upgrade. Type tftypes.Type - // Provider defined logic for upgrading a resource state from the prior - // state version to the current schema version. + // Provider defined logic for upgrading a resource identity from the prior + // identity version to the current schema version. // // The context.Context parameter contains framework-defined loggers and // supports request cancellation. // - // The UpgradeStateRequest parameter contains the prior state data. - // If PriorSchema was set, the State field will be available. Otherwise, - // the RawState must be used. + // The UpgradeResourceIdentityRequest parameter contains the prior identity data. + // If PriorSchema was set, the Identity field will be available. Otherwise, + // the RawIdentity must be used. // - // The UpgradeStateResponse parameter should contain the upgraded - // state data and can be used to signal any logic warnings or errors. + // The UpgradeResourceIdentityResponse parameter should contain the upgraded + // identity data and can be used to signal any logic warnings or errors. IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) } diff --git a/resource/resource.go b/resource/resource.go index f5dcd54e8..ac4699f20 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -227,14 +227,14 @@ type ResourceWithIdentity interface { type ResourceWithUpgradeIdentity interface { Resource - // A mapping of prior state version to current schema version state upgrade - // implementations. Only the specified state upgrader for the prior state + // A mapping of the prior identity version to current identity upgrade + // implementation. Only the specified identity upgrader for the prior identity // version is called, rather than each version in between, so it must - // encapsulate all logic to convert the prior state to the current schema + // encapsulate all logic to convert the prior identity to the current identity schema // version. // // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the - // requested state version not be implemented. + // requested identity version not be implemented. UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index 99b414d92..0f877f5e2 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -23,12 +23,17 @@ type UpgradeResourceIdentityRequest struct { // documentation for `RawIdentity` for information on how to work with the // data it contains. RawState *tfprotov6.RawState + + // Previous identity of the resource if the wrapping IdentityUpgrader + // type PriorSchema field was present. When available, this allows for + // easier data handling such as calling Get() or GetAttribute(). + Identity *tfsdk.ResourceIdentity } // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. type UpgradeResourceIdentityResponse struct { - UpgradedIdentity *tfsdk.ResourceIdentity + Identity *tfsdk.ResourceIdentity // Diagnostics report errors or warnings related to retrieving the resource // identity schema. An empty slice indicates success, with no warnings From 17967cb2fbfea31f378bad49748d19760de18429 Mon Sep 17 00:00:00 2001 From: Rain Date: Thu, 8 May 2025 18:14:20 -0400 Subject: [PATCH 06/11] Addressed PR comments round 2 --- .../fromproto5/upgraderesourceidentity.go | 4 +- .../upgraderesourceidentity_test.go | 10 +- .../fromproto6/upgraderesourceidentity.go | 8 +- .../upgraderesourceidentity_test.go | 12 +- .../server_upgraderesourceidentity.go | 52 +++------ .../server_upgraderesourceidentity_test.go | 106 +++++++++--------- .../server_upgraderesourceidentity.go | 18 +-- .../server_upgraderesourceidentity_test.go | 16 +-- .../server_upgraderesourceidentity.go | 18 +-- .../server_upgraderesourceidentity_test.go | 16 +-- ...resourcewithconfigureandupgradeidentity.go | 10 +- .../resourcewithupgradeidentity.go | 8 +- internal/toproto5/upgraderesourceidentity.go | 2 +- .../toproto5/upgraderesourceidentity_test.go | 12 +- internal/toproto6/upgraderesourceidentity.go | 2 +- .../toproto6/upgraderesourceidentity_test.go | 12 +- resource/identity_upgrader.go | 10 +- resource/resource.go | 2 +- resource/upgrade_identity.go | 37 +++--- 19 files changed, 169 insertions(+), 186 deletions(-) diff --git a/internal/fromproto5/upgraderesourceidentity.go b/internal/fromproto5/upgraderesourceidentity.go index 4b279d057..753065c7f 100644 --- a/internal/fromproto5/upgraderesourceidentity.go +++ b/internal/fromproto5/upgraderesourceidentity.go @@ -15,7 +15,7 @@ import ( // UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest // equivalent of a *tfprotov5.UpgradeResourceIdentityRequest. -func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { +func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { if proto5 == nil { return nil, nil } @@ -36,7 +36,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.Upgra return nil, diags } - fw := &fwserver.UpgradeResourceIdentityRequest{ + fw := &fwserver.UpgradeIdentityRequest{ RawState: (*tfprotov6.RawState)(proto5.RawIdentity), IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto5/upgraderesourceidentity_test.go b/internal/fromproto5/upgraderesourceidentity_test.go index e7257812d..a64c89508 100644 --- a/internal/fromproto5/upgraderesourceidentity_test.go +++ b/internal/fromproto5/upgraderesourceidentity_test.go @@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { input *tfprotov5.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeResourceIdentityRequest + expected *fwserver.UpgradeIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ RawState: testNewTfprotov6RawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov5.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto5.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fromproto6/upgraderesourceidentity.go b/internal/fromproto6/upgraderesourceidentity.go index 849eb3b28..fa03e2b14 100644 --- a/internal/fromproto6/upgraderesourceidentity.go +++ b/internal/fromproto6/upgraderesourceidentity.go @@ -12,9 +12,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest -// equivalent of a *tfprotov6.UpgradeResourceIdentityRequest. -func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { +// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest +// equivalent of a *tfprotov6.UpgradeIdentityRequest. +func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { if proto6 == nil { return nil, nil } @@ -35,7 +35,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.Upgra return nil, diags } - fw := &fwserver.UpgradeResourceIdentityRequest{ + fw := &fwserver.UpgradeIdentityRequest{ RawState: proto6.RawIdentity, IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto6/upgraderesourceidentity_test.go b/internal/fromproto6/upgraderesourceidentity_test.go index 6cf766e6d..952f45869 100644 --- a/internal/fromproto6/upgraderesourceidentity_test.go +++ b/internal/fromproto6/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -func TestUpgradeResourceIdentityRequest(t *testing.T) { +func TestUpgradeIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { input *tfprotov6.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeResourceIdentityRequest + expected *fwserver.UpgradeIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov6.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto6.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index 73dac6d69..a1df7536b 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -16,9 +16,9 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -// UpgradeResourceIdentityRequest is the framework server request for the -// UpgradeResourceIdentity RPC. -type UpgradeResourceIdentityRequest struct { +// UpgradeIdentityRequest is the framework server request for the +// UpgradeIdentity RPC. +type UpgradeIdentityRequest struct { Resource resource.Resource IdentitySchema fwschema.Schema // TypeName is the type of resource that Terraform needs to upgrade the @@ -28,10 +28,6 @@ type UpgradeResourceIdentityRequest struct { // Version is the version of the identity state the resource currently has. Version int64 - // Using the tfprotov6 type here was a pragmatic effort decision around when - // the framework introduced compatibility promises. This type was chosen as - // it was readily available and trivial to convert between tfprotov5. - // // Using a terraform-plugin-go type is not ideal for the framework as almost // all terraform-plugin-go types have framework abstractions, but if there // is ever a time where it makes sense to re-evaluate this decision, such as @@ -40,15 +36,15 @@ type UpgradeResourceIdentityRequest struct { RawState *tfprotov6.RawState } -// UpgradeResourceIdentityResponse is the framework server response for the -// UpgradeResourceIdentity RPC. -type UpgradeResourceIdentityResponse struct { +// UpgradeIdentityResponse is the framework server response for the +// UpgradeIdentity RPC. +type UpgradeIdentityResponse struct { UpgradedIdentity *tfsdk.ResourceIdentity Diagnostics diag.Diagnostics } -// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) { +// UpgradeIdentity implements the framework server UpgradeIdentity RPC. +func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityRequest, resp *UpgradeIdentityResponse) { if req == nil { return } @@ -71,11 +67,10 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour if req.Version == req.IdentitySchema.GetVersion() { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - "There was an error reading the saved resource identity using the current resource identity schema.\n\n"+ - "If this resource Identity 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 Identity, you will need to manually modify it to match the current resource identity schema. "+ - "Otherwise, please report this to the provider developer:\n\n", + "Unexpected Identity Upgrade Request", + "Terraform Core invoked UpgradeResourceIdentity even though the stored identity schema version matches the current version. "+ + "This likely indicates a bug in the Terraform provider framework or Terraform Core. "+ + "Please report this issue to the provider developer.", ) return } @@ -114,7 +109,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") - resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeResourceIdentity(ctx) + resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeIdentity(ctx) logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") // Panic prevention @@ -134,12 +129,12 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - upgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ - RawState: req.RawState, + upgradeResourceIdentityRequest := resource.UpgradeIdentityRequest{ + RawIdentity: req.RawState, } if resourceIdentityUpgrader.PriorSchema != nil { - logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") + logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState") priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) @@ -147,7 +142,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -161,7 +156,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour } - upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ + upgradeResourceIdentityResponse := resource.UpgradeIdentityResponse{ Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. @@ -193,16 +188,5 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - if upgradeResourceIdentityResponse.Identity.Raw.Type() != nil { - logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse RawState set, overriding State") - - resp.UpgradedIdentity = &tfsdk.ResourceIdentity{ - Schema: req.IdentitySchema, - Raw: upgradeResourceIdentityResponse.Identity.Raw, - } - - return - } - resp.UpgradedIdentity = upgradeResourceIdentityResponse.Identity } diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index 6ff2f30b3..e0e5a19ac 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -21,7 +21,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -func TestServerUpgradeResourceIdentity(t *testing.T) { +func TestServerUpgradeIdentity(t *testing.T) { t.Parallel() ctx := context.Background() @@ -39,21 +39,21 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { testCases := map[string]struct { server *fwserver.Server - request *fwserver.UpgradeResourceIdentityRequest - expectedResponse *fwserver.UpgradeResourceIdentityResponse + request *fwserver.UpgradeIdentityRequest + expectedResponse *fwserver.UpgradeIdentityResponse }{ "empty-provider": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + expectedResponse: &fwserver.UpgradeIdentityResponse{}, }, "resource-configure-data": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, ResourceConfigureData: "test-provider-configure-value", }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), @@ -78,7 +78,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { } }, Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -88,13 +88,13 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // In practice, the Configure method would save the // provider data to the Resource implementation and // use it here. The fact that Configure is able to // read the data proves this can work. - rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, }, @@ -102,8 +102,8 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + "Unable to Read Previously Saved Identity for UpgradeIdentity", + fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -113,7 +113,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( "Unable to convert raw state value into prior identity struct", - fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Version)+ + fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -126,7 +126,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( "Unable to convert raw state id value into string", - fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Version)+ + fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -147,7 +147,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -160,29 +160,29 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + expectedResponse: &fwserver.UpgradeIdentityResponse{}, }, "RawState-Unmarshal-and-ResourceIdentity": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - RawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + RawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, }, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -242,7 +242,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -250,15 +250,15 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { var RawState struct { Id string `json:"id"` } - if err := json.Unmarshal(req.RawState.JSON, &RawState); err != nil { + if err := json.Unmarshal(req.RawIdentity.JSON, &RawState); err != nil { resp.Diagnostics.AddError( "Unable to Unmarshal Prior Identity", err.Error(), @@ -281,7 +281,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -294,7 +294,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -303,7 +303,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", @@ -318,7 +318,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -326,13 +326,13 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", @@ -347,7 +347,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "optional_for_import_attribute": true, @@ -355,7 +355,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -368,7 +368,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Expect error before reaching this logic. }, }, @@ -377,10 +377,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", "There was an error reading the saved resource Identity using the prior resource schema defined for version 0 upgrade.\n\n"+ "Please report this to the provider developer:\n\n"+ "AttributeName(\"optional_for_import_attribute\"): unsupported type bool sent as tftypes.Number", @@ -392,19 +392,19 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { - rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{ + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, }, @@ -412,8 +412,8 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", - fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ + "Unable to Read Previously Saved Identity for UpgradeIdentity", + fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -423,7 +423,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( "Unable to convert raw state value into prior identity struct", - fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Version)+ + fmt.Sprintf("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -436,7 +436,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( "Unable to convert raw state id value into string", - fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Version)+ + fmt.Sprintf("There was an error converting the raw state id value into string for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) return @@ -457,7 +457,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -470,17 +470,17 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity }, }, @@ -489,7 +489,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Missing Upgraded Resource Identity", @@ -504,20 +504,20 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 999, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", @@ -534,8 +534,8 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - response := &fwserver.UpgradeResourceIdentityResponse{} - testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request, response) + response := &fwserver.UpgradeIdentityResponse{} + testCase.server.UpgradeIdentity(context.Background(), testCase.request, response) if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index a0dcfb463..75a96aeed 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -14,14 +14,14 @@ import ( ) // UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { +func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeResourceIdentityResponse{} + fwResp := &fwserver.UpgradeIdentityResponse{} if proto5Req == nil { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto5Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto5.UpgradeResourceIdentityRequest(ctx, proto5Req, resource, identitySchema) + fwReq, diags := fromproto5.UpgradeIdentityRequest(ctx, proto5Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 48f211a8b..6c3fd0315 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -79,15 +79,15 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ "test_id": "test-id-value", }) - if diff := cmp.Diff(req.RawState, expectedSourceIdentity); diff != "" { + if diff := cmp.Diff(req.RawIdentity, expectedSourceIdentity); diff != "" { resp.Diagnostics.AddError("Unexpected req.SourceIdentity difference", diff) } @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index 7484eb49f..f33a2ab7f 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -14,14 +14,14 @@ import ( ) // UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { +func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeResourceIdentityResponse{} + fwResp := &fwserver.UpgradeIdentityResponse{} if proto6Req == nil { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto6Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto6.UpgradeResourceIdentityRequest(ctx, proto6Req, resource, identitySchema) + fwReq, diags := fromproto6.UpgradeIdentityRequest(ctx, proto6Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index 87e1aaf8a..f9228e156 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -79,15 +79,15 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ "test_id": "test-id-value", }) - if diff := cmp.Diff(req.RawState, expectedSourceIdentity); diff != "" { + if diff := cmp.Diff(req.RawIdentity, expectedSourceIdentity); diff != "" { resp.Diagnostics.AddError("Unexpected req.SourceIdentity difference", diff) } @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go index b7bdbd703..851da6fec 100644 --- a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -21,7 +21,7 @@ type ResourceWithConfigureAndUpgradeIdentity struct { ConfigureMethod func(context.Context, resource.ConfigureRequest, *resource.ConfigureResponse) // ResourceWithUpgradeIdentity interface methods - UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader } // Configure satisfies the resource.ResourceWithConfigureAndUpgradeIdentity interface. @@ -33,11 +33,11 @@ func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, r.ConfigureMethod(ctx, req, resp) } -// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if r.UpgradeResourceIdentityMethod == nil { +// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. +func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if r.UpgradeIdentityMethod == nil { return nil } - return r.UpgradeResourceIdentityMethod(ctx) + return r.UpgradeIdentityMethod(ctx) } diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go index c57b8336b..6ecb96cbe 100644 --- a/internal/testing/testprovider/resourcewithupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -17,19 +17,19 @@ type ResourceWithUpgradeIdentity struct { *Resource // ResourceWithUpgradeIdentity interface methods - UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader // ResourceWithIdentity interface methods IdentitySchemaMethod func(context.Context, resource.IdentitySchemaRequest, *resource.IdentitySchemaResponse) } // UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (p *ResourceWithUpgradeIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if p.UpgradeResourceIdentityMethod == nil { +func (p *ResourceWithUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if p.UpgradeIdentityMethod == nil { return nil } - return p.UpgradeResourceIdentityMethod(ctx) + return p.UpgradeIdentityMethod(ctx) } // IdentitySchema implements resource.ResourceWithIdentity. diff --git a/internal/toproto5/upgraderesourceidentity.go b/internal/toproto5/upgraderesourceidentity.go index 57ea1d9a8..a02bdc557 100644 --- a/internal/toproto5/upgraderesourceidentity.go +++ b/internal/toproto5/upgraderesourceidentity.go @@ -12,7 +12,7 @@ import ( // UpgradeResourceIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse // equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { +func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto5/upgraderesourceidentity_test.go b/internal/toproto5/upgraderesourceidentity_test.go index 619303bd0..0ffcdef91 100644 --- a/internal/toproto5/upgraderesourceidentity_test.go +++ b/internal/toproto5/upgraderesourceidentity_test.go @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeResourceIdentityResponse + input *fwserver.UpgradeIdentityResponse expected *tfprotov5.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeResourceIdentityResponse{}, + input: &fwserver.UpgradeIdentityResponse{}, expected: &tfprotov5.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov5.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto5.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + got := toproto5.UpgradeIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/toproto6/upgraderesourceidentity.go b/internal/toproto6/upgraderesourceidentity.go index 2748a4172..344b154bb 100644 --- a/internal/toproto6/upgraderesourceidentity.go +++ b/internal/toproto6/upgraderesourceidentity.go @@ -12,7 +12,7 @@ import ( // UpgradeResourceIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse // equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { +func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto6/upgraderesourceidentity_test.go b/internal/toproto6/upgraderesourceidentity_test.go index 191d51832..4884238de 100644 --- a/internal/toproto6/upgraderesourceidentity_test.go +++ b/internal/toproto6/upgraderesourceidentity_test.go @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeResourceIdentityResponse + input *fwserver.UpgradeIdentityResponse expected *tfprotov6.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeResourceIdentityResponse{}, + input: &fwserver.UpgradeIdentityResponse{}, expected: &tfprotov6.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov6.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto6.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + got := toproto6.UpgradeIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index 5d6641be9..068bb3bbe 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -6,7 +6,6 @@ package resource import ( "context" "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" - "github.com/hashicorp/terraform-plugin-go/tftypes" ) // Implementation handler for an UpgradeIdentity operation. @@ -24,13 +23,6 @@ type IdentityUpgrader struct { // UpgradeResourceIdentityRequest type RawIdentity field. PriorSchema *identityschema.Schema - // Version is the version schema that this Upgrader will handle, converting - // it to Version+1. - Version int64 - - // Type describes the schema that this function can upgrade. - Type tftypes.Type - // Provider defined logic for upgrading a resource identity from the prior // identity version to the current schema version. // @@ -43,5 +35,5 @@ type IdentityUpgrader struct { // // The UpgradeResourceIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. - IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) + IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) } diff --git a/resource/resource.go b/resource/resource.go index ac4699f20..b15bbb80b 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -236,5 +236,5 @@ type ResourceWithUpgradeIdentity interface { // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the // requested identity version not be implemented. - UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader + UpgradeIdentity(context.Context) map[int64]IdentityUpgrader } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index 0f877f5e2..ade327cca 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -11,18 +11,17 @@ import ( // Request information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityRequest struct { - // TypeName is the type of resource that Terraform needs to upgrade the - // identity state for. - TypeName string - - // Version is the version of the identity state the resource currently has. - Version int64 - - // RawIdentity is the identity state as Terraform sees it right now in JSON. See the - // documentation for `RawIdentity` for information on how to work with the - // data it contains. - RawState *tfprotov6.RawState +type UpgradeIdentityRequest struct { + // Previous state of the resource identity in JSON format + // (Terraform CLI 0.12 and later) This data is always available, + // regardless of whether the wrapping IdentityUpgrader type + // PriorSchema field was present. + // + // This is advanced functionality for providers wanting to skip the full + // redeclaration of older identity schemas and instead use lower level handlers + // to transform data. A typical implementation for working with this data will + // call the Unmarshal() method. + RawIdentity *tfprotov6.RawState // Previous identity of the resource if the wrapping IdentityUpgrader // type PriorSchema field was present. When available, this allows for @@ -32,11 +31,19 @@ type UpgradeResourceIdentityRequest struct { // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityResponse struct { +type UpgradeIdentityResponse struct { + // Upgraded identity of the resource, which should match the current identity + //schema version. + // + // This field allows for easier data handling such as calling Set() or + // SetAttribute(). + // + // All data must be populated to prevent data loss during the upgrade + // operation. No prior identity data is copied automatically. Identity *tfsdk.ResourceIdentity - // Diagnostics report errors or warnings related to retrieving the resource - // identity schema. An empty slice indicates success, with no warnings + // Diagnostics report errors or warnings related to upgrading the resource + // identity state. An empty slice indicates success, with no warnings // or errors generated. Diagnostics diag.Diagnostics } From ed423e854f3d56c40a4035d37b9f98c1d63f732d Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 12 May 2025 10:06:22 -0400 Subject: [PATCH 07/11] Reverting the renaming for now --- .../fromproto5/upgraderesourceidentity.go | 4 +- .../upgraderesourceidentity_test.go | 10 +- .../fromproto6/upgraderesourceidentity.go | 8 +- .../upgraderesourceidentity_test.go | 12 +- .../server_upgraderesourceidentity.go | 38 +++--- .../server_upgraderesourceidentity_test.go | 112 +++++++++--------- .../server_upgraderesourceidentity.go | 18 +-- .../server_upgraderesourceidentity_test.go | 20 ++-- .../server_upgraderesourceidentity.go | 18 +-- .../server_upgraderesourceidentity_test.go | 20 ++-- ...resourcewithconfigureandupgradeidentity.go | 28 ++--- .../resourcewithupgradeidentity.go | 22 ++-- internal/toproto5/upgraderesourceidentity.go | 2 +- .../toproto5/upgraderesourceidentity_test.go | 12 +- internal/toproto6/upgraderesourceidentity.go | 2 +- .../toproto6/upgraderesourceidentity_test.go | 12 +- resource/identity_upgrader.go | 6 +- resource/identityschema/schema.go | 2 +- resource/resource.go | 6 +- resource/upgrade_identity.go | 4 +- 20 files changed, 178 insertions(+), 178 deletions(-) diff --git a/internal/fromproto5/upgraderesourceidentity.go b/internal/fromproto5/upgraderesourceidentity.go index 753065c7f..4b279d057 100644 --- a/internal/fromproto5/upgraderesourceidentity.go +++ b/internal/fromproto5/upgraderesourceidentity.go @@ -15,7 +15,7 @@ import ( // UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest // equivalent of a *tfprotov5.UpgradeResourceIdentityRequest. -func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { +func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { if proto5 == nil { return nil, nil } @@ -36,7 +36,7 @@ func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResour return nil, diags } - fw := &fwserver.UpgradeIdentityRequest{ + fw := &fwserver.UpgradeResourceIdentityRequest{ RawState: (*tfprotov6.RawState)(proto5.RawIdentity), IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto5/upgraderesourceidentity_test.go b/internal/fromproto5/upgraderesourceidentity_test.go index a64c89508..e7257812d 100644 --- a/internal/fromproto5/upgraderesourceidentity_test.go +++ b/internal/fromproto5/upgraderesourceidentity_test.go @@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { input *tfprotov5.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeIdentityRequest + expected *fwserver.UpgradeResourceIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewTfprotov6RawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov5.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto5.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fromproto6/upgraderesourceidentity.go b/internal/fromproto6/upgraderesourceidentity.go index fa03e2b14..849eb3b28 100644 --- a/internal/fromproto6/upgraderesourceidentity.go +++ b/internal/fromproto6/upgraderesourceidentity.go @@ -12,9 +12,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest -// equivalent of a *tfprotov6.UpgradeIdentityRequest. -func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { +// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest +// equivalent of a *tfprotov6.UpgradeResourceIdentityRequest. +func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { if proto6 == nil { return nil, nil } @@ -35,7 +35,7 @@ func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResour return nil, diags } - fw := &fwserver.UpgradeIdentityRequest{ + fw := &fwserver.UpgradeResourceIdentityRequest{ RawState: proto6.RawIdentity, IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto6/upgraderesourceidentity_test.go b/internal/fromproto6/upgraderesourceidentity_test.go index 952f45869..6cf766e6d 100644 --- a/internal/fromproto6/upgraderesourceidentity_test.go +++ b/internal/fromproto6/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -func TestUpgradeIdentityRequest(t *testing.T) { +func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { input *tfprotov6.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeIdentityRequest + expected *fwserver.UpgradeResourceIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov6.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto6.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index a1df7536b..2d7ca18c1 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -16,9 +16,9 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -// UpgradeIdentityRequest is the framework server request for the -// UpgradeIdentity RPC. -type UpgradeIdentityRequest struct { +// UpgradeResourceIdentityRequest is the framework server request for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityRequest struct { Resource resource.Resource IdentitySchema fwschema.Schema // TypeName is the type of resource that Terraform needs to upgrade the @@ -36,15 +36,15 @@ type UpgradeIdentityRequest struct { RawState *tfprotov6.RawState } -// UpgradeIdentityResponse is the framework server response for the -// UpgradeIdentity RPC. -type UpgradeIdentityResponse struct { +// UpgradeResourceIdentityResponse is the framework server response for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityResponse struct { UpgradedIdentity *tfsdk.ResourceIdentity Diagnostics diag.Diagnostics } -// UpgradeIdentity implements the framework server UpgradeIdentity RPC. -func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityRequest, resp *UpgradeIdentityResponse) { +// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC. +func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) { if req == nil { return } @@ -94,23 +94,23 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques } } - resourceWithUpgradeIdentity, ok := req.Resource.(resource.ResourceWithUpgradeIdentity) + resourceWithUpgradeResourceIdentity, ok := req.Resource.(resource.ResourceWithUpgradeResourceIdentity) if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeIdentity() method, "+ + "This resource was implemented without an UpgradeResourceIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") + logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeResourceIdentity") - logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") - resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeIdentity(ctx) - logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") + logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeResourceIdentity") + resourceIdentityUpgraders := resourceWithUpgradeResourceIdentity.UpgradeResourceIdentity(ctx) + logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeResourceIdentity") // Panic prevention if resourceIdentityUpgraders == nil { @@ -122,19 +122,19 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - upgradeResourceIdentityRequest := resource.UpgradeIdentityRequest{ + upgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ RawIdentity: req.RawState, } if resourceIdentityUpgrader.PriorSchema != nil { - logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState") + logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) @@ -142,7 +142,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -156,7 +156,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques } - upgradeResourceIdentityResponse := resource.UpgradeIdentityResponse{ + upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index e0e5a19ac..6716f2134 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -21,7 +21,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -func TestServerUpgradeIdentity(t *testing.T) { +func TestServerUpgradeResourceIdentity(t *testing.T) { t.Parallel() ctx := context.Background() @@ -39,26 +39,26 @@ func TestServerUpgradeIdentity(t *testing.T) { testCases := map[string]struct { server *fwserver.Server - request *fwserver.UpgradeIdentityRequest - expectedResponse *fwserver.UpgradeIdentityResponse + request *fwserver.UpgradeResourceIdentityRequest + expectedResponse *fwserver.UpgradeResourceIdentityResponse }{ "empty-provider": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{}, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, }, "resource-configure-data": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, ResourceConfigureData: "test-provider-configure-value", }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithConfigureAndUpgradeIdentity{ + Resource: &testprovider.ResourceWithConfigureAndUpgradeResourceIdentity{ ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { providerData, ok := req.ProviderData.(string) @@ -78,7 +78,7 @@ func TestServerUpgradeIdentity(t *testing.T) { } }, Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -88,7 +88,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // In practice, the Configure method would save the // provider data to the Resource implementation and // use it here. The fact that Configure is able to @@ -102,7 +102,7 @@ func TestServerUpgradeIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -147,7 +147,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -160,28 +160,28 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{}, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, }, "RawState-Unmarshal-and-ResourceIdentity": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { RawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -229,7 +229,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -242,18 +242,18 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { var RawState struct { Id string `json:"id"` } @@ -281,7 +281,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -290,11 +290,11 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - "ResourceType-UpgradeIdentity-not-implemented": { + "ResourceType-UpgradeResourceIdentity-not-implemented": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -303,40 +303,40 @@ func TestServerUpgradeIdentity(t *testing.T) { Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeIdentity() method, "+ + "This resource was implemented without an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), }, }, }, - "ResourceType-UpgradeIdentity-empty": { + "ResourceType-UpgradeResourceIdentity-empty": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -347,15 +347,15 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "optional_for_import_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -368,7 +368,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // Expect error before reaching this logic. }, }, @@ -377,10 +377,10 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", "There was an error reading the saved resource Identity using the prior resource schema defined for version 0 upgrade.\n\n"+ "Please report this to the provider developer:\n\n"+ "AttributeName(\"optional_for_import_attribute\"): unsupported type bool sent as tftypes.Number", @@ -392,18 +392,18 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -412,7 +412,7 @@ func TestServerUpgradeIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -457,7 +457,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -470,17 +470,17 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity }, }, @@ -489,7 +489,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Missing Upgraded Resource Identity", @@ -504,24 +504,24 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 999, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 999 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -534,8 +534,8 @@ func TestServerUpgradeIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - response := &fwserver.UpgradeIdentityResponse{} - testCase.server.UpgradeIdentity(context.Background(), testCase.request, response) + response := &fwserver.UpgradeResourceIdentityResponse{} + testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request, response) if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index 75a96aeed..a0dcfb463 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -14,14 +14,14 @@ import ( ) // UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. -func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { +func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeIdentityResponse{} + fwResp := &fwserver.UpgradeResourceIdentityResponse{} if proto5Req == nil { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto5Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto5.UpgradeIdentityRequest(ctx, proto5Req, resource, identitySchema) + fwReq, diags := fromproto5.UpgradeResourceIdentityRequest(ctx, proto5Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 6c3fd0315..0a1998981 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -70,7 +70,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index f33a2ab7f..7484eb49f 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -14,14 +14,14 @@ import ( ) // UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. -func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { +func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeIdentityResponse{} + fwResp := &fwserver.UpgradeResourceIdentityResponse{} if proto6Req == nil { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto6Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto6.UpgradeIdentityRequest(ctx, proto6Req, resource, identitySchema) + fwReq, diags := fromproto6.UpgradeResourceIdentityRequest(ctx, proto6Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index f9228e156..d11c2adb5 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -70,7 +70,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go index 851da6fec..da74ae5f1 100644 --- a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -9,23 +9,23 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithConfigureAndUpgradeIdentity{} -var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeIdentity{} -var _ resource.ResourceWithUpgradeIdentity = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.Resource = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithConfigureAndUpgradeResourceIdentity{} -// Declarative resource.ResourceWithConfigureAndUpgradeIdentity for unit testing. -type ResourceWithConfigureAndUpgradeIdentity struct { +// Declarative resource.ResourceWithConfigureAndUpgradeResourceIdentity for unit testing. +type ResourceWithConfigureAndUpgradeResourceIdentity struct { *Resource - // ResourceWithConfigureAndUpgradeIdentity interface methods + // ResourceWithConfigureAndUpgradeResourceIdentity interface methods ConfigureMethod func(context.Context, resource.ConfigureRequest, *resource.ConfigureResponse) - // ResourceWithUpgradeIdentity interface methods - UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeResourceIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader } -// Configure satisfies the resource.ResourceWithConfigureAndUpgradeIdentity interface. -func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { +// Configure satisfies the resource.ResourceWithConfigureAndUpgradeResourceIdentity interface. +func (r *ResourceWithConfigureAndUpgradeResourceIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { if r.ConfigureMethod == nil { return } @@ -33,11 +33,11 @@ func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, r.ConfigureMethod(ctx, req, resp) } -// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if r.UpgradeIdentityMethod == nil { +// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. +func (r *ResourceWithConfigureAndUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if r.UpgradeResourceIdentityMethod == nil { return nil } - return r.UpgradeIdentityMethod(ctx) + return r.UpgradeResourceIdentityMethod(ctx) } diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go index 6ecb96cbe..bc7ae54ab 100644 --- a/internal/testing/testprovider/resourcewithupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -9,31 +9,31 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithUpgradeIdentity{} -var _ resource.ResourceWithUpgradeIdentity = &ResourceWithUpgradeIdentity{} +var _ resource.Resource = &ResourceWithUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithUpgradeResourceIdentity{} -// Declarative resource.ResourceWithUpgradeIdentity for unit testing. -type ResourceWithUpgradeIdentity struct { +// Declarative resource.ResourceWithUpgradeResourceIdentity for unit testing. +type ResourceWithUpgradeResourceIdentity struct { *Resource - // ResourceWithUpgradeIdentity interface methods - UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeResourceIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader // ResourceWithIdentity interface methods IdentitySchemaMethod func(context.Context, resource.IdentitySchemaRequest, *resource.IdentitySchemaResponse) } -// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (p *ResourceWithUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if p.UpgradeIdentityMethod == nil { +// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. +func (p *ResourceWithUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if p.UpgradeResourceIdentityMethod == nil { return nil } - return p.UpgradeIdentityMethod(ctx) + return p.UpgradeResourceIdentityMethod(ctx) } // IdentitySchema implements resource.ResourceWithIdentity. -func (p *ResourceWithUpgradeIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { +func (p *ResourceWithUpgradeResourceIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { if p.IdentitySchemaMethod == nil { return } diff --git a/internal/toproto5/upgraderesourceidentity.go b/internal/toproto5/upgraderesourceidentity.go index a02bdc557..57ea1d9a8 100644 --- a/internal/toproto5/upgraderesourceidentity.go +++ b/internal/toproto5/upgraderesourceidentity.go @@ -12,7 +12,7 @@ import ( // UpgradeResourceIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse // equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto5/upgraderesourceidentity_test.go b/internal/toproto5/upgraderesourceidentity_test.go index 0ffcdef91..619303bd0 100644 --- a/internal/toproto5/upgraderesourceidentity_test.go +++ b/internal/toproto5/upgraderesourceidentity_test.go @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeIdentityResponse + input *fwserver.UpgradeResourceIdentityResponse expected *tfprotov5.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeIdentityResponse{}, + input: &fwserver.UpgradeResourceIdentityResponse{}, expected: &tfprotov5.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov5.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto5.UpgradeIdentityResponse(context.Background(), testCase.input) + got := toproto5.UpgradeResourceIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/toproto6/upgraderesourceidentity.go b/internal/toproto6/upgraderesourceidentity.go index 344b154bb..2748a4172 100644 --- a/internal/toproto6/upgraderesourceidentity.go +++ b/internal/toproto6/upgraderesourceidentity.go @@ -12,7 +12,7 @@ import ( // UpgradeResourceIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse // equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto6/upgraderesourceidentity_test.go b/internal/toproto6/upgraderesourceidentity_test.go index 4884238de..191d51832 100644 --- a/internal/toproto6/upgraderesourceidentity_test.go +++ b/internal/toproto6/upgraderesourceidentity_test.go @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeIdentityResponse + input *fwserver.UpgradeResourceIdentityResponse expected *tfprotov6.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeIdentityResponse{}, + input: &fwserver.UpgradeResourceIdentityResponse{}, expected: &tfprotov6.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov6.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto6.UpgradeIdentityResponse(context.Background(), testCase.input) + got := toproto6.UpgradeResourceIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index 068bb3bbe..372f67e19 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -8,11 +8,11 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" ) -// Implementation handler for an UpgradeIdentity operation. +// Implementation handler for an UpgradeResourceIdentity operation. // // This is used to encapsulate all upgrade logic from a prior identity to the // current version when a Resource implements the -// ResourceWithUpgradeIdentity interface. +// ResourceWithUpgradeResourceIdentity interface. type IdentityUpgrader struct { // Schema information for the prior identity version. While not required, // setting this will populate the UpgradeResourceIdentityRequest type Identity @@ -35,5 +35,5 @@ type IdentityUpgrader struct { // // The UpgradeResourceIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. - IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) + IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) } diff --git a/resource/identityschema/schema.go b/resource/identityschema/schema.go index 6d68aff9a..3eb498b93 100644 --- a/resource/identityschema/schema.go +++ b/resource/identityschema/schema.go @@ -29,7 +29,7 @@ type Schema struct { // Version indicates the current version of the resource identity schema. Resource // identity schema versioning enables identity state upgrades in conjunction with the - // [resource.ResourceWithUpgradeIdentity] interface. Versioning is only + // [resource.ResourceWithUpgradeResourceIdentity] interface. Versioning is only // required if there is a breaking change involving existing identity state data, // such as changing an attribute type in a manner that is incompatible with the Terraform type. // diff --git a/resource/resource.go b/resource/resource.go index b15bbb80b..95f8300a1 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -216,7 +216,7 @@ type ResourceWithValidateConfig interface { // by the identity object via the ResourceWithImportState interface. // - The resource identity should not change during the lifecycle of the remote object. That is, from the // creation of the remote object in the remote system until its destruction. An exception to this rule -// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeIdentity interface. +// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeResourceIdentity interface. type ResourceWithIdentity interface { Resource @@ -224,7 +224,7 @@ type ResourceWithIdentity interface { IdentitySchema(context.Context, IdentitySchemaRequest, *IdentitySchemaResponse) } -type ResourceWithUpgradeIdentity interface { +type ResourceWithUpgradeResourceIdentity interface { Resource // A mapping of the prior identity version to current identity upgrade @@ -236,5 +236,5 @@ type ResourceWithUpgradeIdentity interface { // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the // requested identity version not be implemented. - UpgradeIdentity(context.Context) map[int64]IdentityUpgrader + UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index ade327cca..0855a72ec 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -11,7 +11,7 @@ import ( // Request information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeIdentityRequest struct { +type UpgradeResourceIdentityRequest struct { // Previous state of the resource identity in JSON format // (Terraform CLI 0.12 and later) This data is always available, // regardless of whether the wrapping IdentityUpgrader type @@ -31,7 +31,7 @@ type UpgradeIdentityRequest struct { // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeIdentityResponse struct { +type UpgradeResourceIdentityResponse struct { // Upgraded identity of the resource, which should match the current identity //schema version. // From b0913a00fc6727940eda1b15dd97d995ebb3b97c Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 12 May 2025 10:12:51 -0400 Subject: [PATCH 08/11] Testing renaming again --- .../fromproto5/upgraderesourceidentity.go | 6 +- .../upgraderesourceidentity_test.go | 12 +- .../fromproto6/upgraderesourceidentity.go | 6 +- .../upgraderesourceidentity_test.go | 12 +- .../server_upgraderesourceidentity.go | 50 ++++---- .../server_upgraderesourceidentity_test.go | 112 +++++++++--------- .../server_upgraderesourceidentity.go | 20 ++-- .../server_upgraderesourceidentity_test.go | 22 ++-- .../server_upgraderesourceidentity.go | 20 ++-- .../server_upgraderesourceidentity_test.go | 22 ++-- ...resourcewithconfigureandupgradeidentity.go | 28 ++--- .../resourcewithupgradeidentity.go | 22 ++-- internal/toproto5/upgraderesourceidentity.go | 6 +- .../toproto5/upgraderesourceidentity_test.go | 14 +-- internal/toproto6/upgraderesourceidentity.go | 6 +- .../toproto6/upgraderesourceidentity_test.go | 14 +-- resource/identity_upgrader.go | 14 +-- resource/identityschema/schema.go | 2 +- resource/resource.go | 6 +- resource/upgrade_identity.go | 4 +- 20 files changed, 199 insertions(+), 199 deletions(-) diff --git a/internal/fromproto5/upgraderesourceidentity.go b/internal/fromproto5/upgraderesourceidentity.go index 4b279d057..6f8a18ea7 100644 --- a/internal/fromproto5/upgraderesourceidentity.go +++ b/internal/fromproto5/upgraderesourceidentity.go @@ -13,9 +13,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest +// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest // equivalent of a *tfprotov5.UpgradeResourceIdentityRequest. -func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { +func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { if proto5 == nil { return nil, nil } @@ -36,7 +36,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.Upgra return nil, diags } - fw := &fwserver.UpgradeResourceIdentityRequest{ + fw := &fwserver.UpgradeIdentityRequest{ RawState: (*tfprotov6.RawState)(proto5.RawIdentity), IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto5/upgraderesourceidentity_test.go b/internal/fromproto5/upgraderesourceidentity_test.go index e7257812d..6967c57a1 100644 --- a/internal/fromproto5/upgraderesourceidentity_test.go +++ b/internal/fromproto5/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -func TestUpgradeResourceIdentityRequest(t *testing.T) { +func TestUpgradeIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { input *tfprotov5.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeResourceIdentityRequest + expected *fwserver.UpgradeIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ RawState: testNewTfprotov6RawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov5.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto5.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fromproto6/upgraderesourceidentity.go b/internal/fromproto6/upgraderesourceidentity.go index 849eb3b28..7bc20429e 100644 --- a/internal/fromproto6/upgraderesourceidentity.go +++ b/internal/fromproto6/upgraderesourceidentity.go @@ -12,9 +12,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest +// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest // equivalent of a *tfprotov6.UpgradeResourceIdentityRequest. -func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { +func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { if proto6 == nil { return nil, nil } @@ -35,7 +35,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.Upgra return nil, diags } - fw := &fwserver.UpgradeResourceIdentityRequest{ + fw := &fwserver.UpgradeIdentityRequest{ RawState: proto6.RawIdentity, IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto6/upgraderesourceidentity_test.go b/internal/fromproto6/upgraderesourceidentity_test.go index 6cf766e6d..952f45869 100644 --- a/internal/fromproto6/upgraderesourceidentity_test.go +++ b/internal/fromproto6/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -func TestUpgradeResourceIdentityRequest(t *testing.T) { +func TestUpgradeIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { input *tfprotov6.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeResourceIdentityRequest + expected *fwserver.UpgradeIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov6.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeResourceIdentityRequest{ + expected: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto6.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index 2d7ca18c1..d514b1949 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -16,9 +16,9 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -// UpgradeResourceIdentityRequest is the framework server request for the -// UpgradeResourceIdentity RPC. -type UpgradeResourceIdentityRequest struct { +// UpgradeIdentityRequest is the framework server request for the +// UpgradeIdentity RPC. +type UpgradeIdentityRequest struct { Resource resource.Resource IdentitySchema fwschema.Schema // TypeName is the type of resource that Terraform needs to upgrade the @@ -36,15 +36,15 @@ type UpgradeResourceIdentityRequest struct { RawState *tfprotov6.RawState } -// UpgradeResourceIdentityResponse is the framework server response for the -// UpgradeResourceIdentity RPC. -type UpgradeResourceIdentityResponse struct { +// UpgradeIdentityResponse is the framework server response for the +// UpgradeIdentity RPC. +type UpgradeIdentityResponse struct { UpgradedIdentity *tfsdk.ResourceIdentity Diagnostics diag.Diagnostics } -// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) { +// UpgradeIdentity implements the framework server UpgradeIdentity RPC. +func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityRequest, resp *UpgradeIdentityResponse) { if req == nil { return } @@ -68,7 +68,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour if req.Version == req.IdentitySchema.GetVersion() { resp.Diagnostics.AddError( "Unexpected Identity Upgrade Request", - "Terraform Core invoked UpgradeResourceIdentity even though the stored identity schema version matches the current version. "+ + "Terraform Core invoked UpgradeIdentity even though the stored identity schema version matches the current version. "+ "This likely indicates a bug in the Terraform provider framework or Terraform Core. "+ "Please report this issue to the provider developer.", ) @@ -94,23 +94,23 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour } } - resourceWithUpgradeResourceIdentity, ok := req.Resource.(resource.ResourceWithUpgradeResourceIdentity) + resourceWithUpgradeIdentity, ok := req.Resource.(resource.ResourceWithUpgradeIdentity) if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeResourceIdentity() method, "+ + "This resource was implemented without an UpgradeIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeResourceIdentity") + logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") - logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeResourceIdentity") - resourceIdentityUpgraders := resourceWithUpgradeResourceIdentity.UpgradeResourceIdentity(ctx) - logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeResourceIdentity") + logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") + resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeIdentity(ctx) + logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") // Panic prevention if resourceIdentityUpgraders == nil { @@ -122,19 +122,19 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeResourceIdentity() method, "+ + "This resource was implemented with an UpgradeIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - upgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ + UpgradeIdentityRequest := resource.UpgradeIdentityRequest{ RawIdentity: req.RawState, } if resourceIdentityUpgrader.PriorSchema != nil { - logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") + logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState") priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) @@ -142,21 +142,21 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ "Please report this to the provider developer:\n\n"+err.Error(), ) return } - upgradeResourceIdentityRequest.Identity = &tfsdk.ResourceIdentity{ + UpgradeIdentityRequest.Identity = &tfsdk.ResourceIdentity{ Raw: rawIdentityValue, // from the output of req.RawState.UnmarshalWithOpts Schema: *resourceIdentityUpgrader.PriorSchema, } } - upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ + UpgradeIdentityResponse := resource.UpgradeIdentityResponse{ Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. @@ -169,16 +169,16 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour // any errors. logging.FrameworkTrace(ctx, "Calling provider defined IdentityUpgrader") - resourceIdentityUpgrader.IdentityUpgrader(ctx, upgradeResourceIdentityRequest, &upgradeResourceIdentityResponse) + resourceIdentityUpgrader.IdentityUpgrader(ctx, UpgradeIdentityRequest, &UpgradeIdentityResponse) logging.FrameworkTrace(ctx, "Called provider defined IdentityUpgrader") - resp.Diagnostics.Append(upgradeResourceIdentityResponse.Diagnostics...) + resp.Diagnostics.Append(UpgradeIdentityResponse.Diagnostics...) if resp.Diagnostics.HasError() { return } - if upgradeResourceIdentityResponse.Identity.Raw.Type() == nil || upgradeResourceIdentityResponse.Identity.Raw.IsNull() { + if UpgradeIdentityResponse.Identity.Raw.Type() == nil || UpgradeIdentityResponse.Identity.Raw.IsNull() { resp.Diagnostics.AddError( "Missing Upgraded Resource Identity", fmt.Sprintf("After attempting a resource Identity upgrade to version %d, the provider did not return any Identity data. ", req.Version)+ @@ -188,5 +188,5 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - resp.UpgradedIdentity = upgradeResourceIdentityResponse.Identity + resp.UpgradedIdentity = UpgradeIdentityResponse.Identity } diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index 6716f2134..e0e5a19ac 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -21,7 +21,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -func TestServerUpgradeResourceIdentity(t *testing.T) { +func TestServerUpgradeIdentity(t *testing.T) { t.Parallel() ctx := context.Background() @@ -39,26 +39,26 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { testCases := map[string]struct { server *fwserver.Server - request *fwserver.UpgradeResourceIdentityRequest - expectedResponse *fwserver.UpgradeResourceIdentityResponse + request *fwserver.UpgradeIdentityRequest + expectedResponse *fwserver.UpgradeIdentityResponse }{ "empty-provider": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + expectedResponse: &fwserver.UpgradeIdentityResponse{}, }, "resource-configure-data": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, ResourceConfigureData: "test-provider-configure-value", }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithConfigureAndUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithConfigureAndUpgradeIdentity{ ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { providerData, ok := req.ProviderData.(string) @@ -78,7 +78,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { } }, Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -88,7 +88,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // In practice, the Configure method would save the // provider data to the Resource implementation and // use it here. The fact that Configure is able to @@ -102,7 +102,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -147,7 +147,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -160,28 +160,28 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ IdentitySchema: testIdentitySchema, Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, + expectedResponse: &fwserver.UpgradeIdentityResponse{}, }, "RawState-Unmarshal-and-ResourceIdentity": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { RawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -242,18 +242,18 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { var RawState struct { Id string `json:"id"` } @@ -281,7 +281,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -290,11 +290,11 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - "ResourceType-UpgradeResourceIdentity-not-implemented": { + "ResourceType-UpgradeIdentity-not-implemented": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -303,40 +303,40 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeResourceIdentity() method, "+ + "This resource was implemented without an UpgradeIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), }, }, }, - "ResourceType-UpgradeResourceIdentity-empty": { + "ResourceType-UpgradeIdentity-empty": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeResourceIdentity() method, "+ + "This resource was implemented with an UpgradeIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -347,15 +347,15 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "optional_for_import_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -368,7 +368,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Expect error before reaching this logic. }, }, @@ -377,10 +377,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", "There was an error reading the saved resource Identity using the prior resource schema defined for version 0 upgrade.\n\n"+ "Please report this to the provider developer:\n\n"+ "AttributeName(\"optional_for_import_attribute\"): unsupported type bool sent as tftypes.Number", @@ -392,18 +392,18 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -412,7 +412,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", + "Unable to Read Previously Saved Identity for UpgradeIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -457,7 +457,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -470,17 +470,17 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity }, }, @@ -489,7 +489,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Missing Upgraded Resource Identity", @@ -504,24 +504,24 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeResourceIdentityRequest{ + request: &fwserver.UpgradeIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ + Resource: &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{}, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 999, }, - expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ + expectedResponse: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeResourceIdentity() method, "+ + "This resource was implemented with an UpgradeIdentity() method, "+ "however Terraform was expecting an implementation for version 999 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -534,8 +534,8 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - response := &fwserver.UpgradeResourceIdentityResponse{} - testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request, response) + response := &fwserver.UpgradeIdentityResponse{} + testCase.server.UpgradeIdentity(context.Background(), testCase.request, response) if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index a0dcfb463..4574dd979 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -13,15 +13,15 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -// UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { +// UpgradeIdentity satisfies the tfprotov5.ProviderServer interface. +func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeResourceIdentityResponse{} + fwResp := &fwserver.UpgradeIdentityResponse{} if proto5Req == nil { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto5Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto5.UpgradeResourceIdentityRequest(ctx, proto5Req, resource, identitySchema) + fwReq, diags := fromproto5.UpgradeIdentityRequest(ctx, proto5Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) - return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 0a1998981..6c0bcdd22 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestServerUpgradeResourceIdentity(t *testing.T) { +func TestServerUpgradeIdentity(t *testing.T) { t.Parallel() testSchema := schema.Schema{ @@ -70,7 +70,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index 7484eb49f..ecd2fbf0b 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -13,15 +13,15 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. -func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { +// UpgradeIdentity satisfies the tfprotov6.ProviderServer interface. +func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeResourceIdentityResponse{} + fwResp := &fwserver.UpgradeIdentityResponse{} if proto6Req == nil { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto6Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfproto fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto6.UpgradeResourceIdentityRequest(ctx, proto6Req, resource, identitySchema) + fwReq, diags := fromproto6.UpgradeIdentityRequest(ctx, proto6Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) - return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index d11c2adb5..8f999e2e9 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestServerUpgradeResourceIdentity(t *testing.T) { +func TestServerUpgradeIdentity(t *testing.T) { t.Parallel() testSchema := schema.Schema{ @@ -70,7 +70,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeResourceIdentity{ + return &testprovider.ResourceWithUpgradeIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go index da74ae5f1..851da6fec 100644 --- a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -9,23 +9,23 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithConfigureAndUpgradeResourceIdentity{} -var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeResourceIdentity{} -var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.Resource = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithConfigureAndUpgradeIdentity{} -// Declarative resource.ResourceWithConfigureAndUpgradeResourceIdentity for unit testing. -type ResourceWithConfigureAndUpgradeResourceIdentity struct { +// Declarative resource.ResourceWithConfigureAndUpgradeIdentity for unit testing. +type ResourceWithConfigureAndUpgradeIdentity struct { *Resource - // ResourceWithConfigureAndUpgradeResourceIdentity interface methods + // ResourceWithConfigureAndUpgradeIdentity interface methods ConfigureMethod func(context.Context, resource.ConfigureRequest, *resource.ConfigureResponse) - // ResourceWithUpgradeResourceIdentity interface methods - UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeIdentity interface methods + UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader } -// Configure satisfies the resource.ResourceWithConfigureAndUpgradeResourceIdentity interface. -func (r *ResourceWithConfigureAndUpgradeResourceIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { +// Configure satisfies the resource.ResourceWithConfigureAndUpgradeIdentity interface. +func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { if r.ConfigureMethod == nil { return } @@ -33,11 +33,11 @@ func (r *ResourceWithConfigureAndUpgradeResourceIdentity) Configure(ctx context. r.ConfigureMethod(ctx, req, resp) } -// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. -func (r *ResourceWithConfigureAndUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if r.UpgradeResourceIdentityMethod == nil { +// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. +func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if r.UpgradeIdentityMethod == nil { return nil } - return r.UpgradeResourceIdentityMethod(ctx) + return r.UpgradeIdentityMethod(ctx) } diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go index bc7ae54ab..6ecb96cbe 100644 --- a/internal/testing/testprovider/resourcewithupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -9,31 +9,31 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithUpgradeResourceIdentity{} -var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithUpgradeResourceIdentity{} +var _ resource.Resource = &ResourceWithUpgradeIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithUpgradeIdentity{} -// Declarative resource.ResourceWithUpgradeResourceIdentity for unit testing. -type ResourceWithUpgradeResourceIdentity struct { +// Declarative resource.ResourceWithUpgradeIdentity for unit testing. +type ResourceWithUpgradeIdentity struct { *Resource - // ResourceWithUpgradeResourceIdentity interface methods - UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeIdentity interface methods + UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader // ResourceWithIdentity interface methods IdentitySchemaMethod func(context.Context, resource.IdentitySchemaRequest, *resource.IdentitySchemaResponse) } -// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. -func (p *ResourceWithUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if p.UpgradeResourceIdentityMethod == nil { +// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. +func (p *ResourceWithUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if p.UpgradeIdentityMethod == nil { return nil } - return p.UpgradeResourceIdentityMethod(ctx) + return p.UpgradeIdentityMethod(ctx) } // IdentitySchema implements resource.ResourceWithIdentity. -func (p *ResourceWithUpgradeResourceIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { +func (p *ResourceWithUpgradeIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { if p.IdentitySchemaMethod == nil { return } diff --git a/internal/toproto5/upgraderesourceidentity.go b/internal/toproto5/upgraderesourceidentity.go index 57ea1d9a8..a63aaa346 100644 --- a/internal/toproto5/upgraderesourceidentity.go +++ b/internal/toproto5/upgraderesourceidentity.go @@ -10,9 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -// UpgradeResourceIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse -// equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { +// UpgradeIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeIdentityResponse. +func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto5/upgraderesourceidentity_test.go b/internal/toproto5/upgraderesourceidentity_test.go index 619303bd0..48badb3e4 100644 --- a/internal/toproto5/upgraderesourceidentity_test.go +++ b/internal/toproto5/upgraderesourceidentity_test.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestUpgradeResourceIdentityResponse(t *testing.T) { +func TestUpgradeIdentityResponse(t *testing.T) { t.Parallel() testIdentityProto5Type := tftypes.Object{ @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeResourceIdentityResponse + input *fwserver.UpgradeIdentityResponse expected *tfprotov5.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeResourceIdentityResponse{}, + input: &fwserver.UpgradeIdentityResponse{}, expected: &tfprotov5.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov5.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto5.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + got := toproto5.UpgradeIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/toproto6/upgraderesourceidentity.go b/internal/toproto6/upgraderesourceidentity.go index 2748a4172..afe5bcf03 100644 --- a/internal/toproto6/upgraderesourceidentity.go +++ b/internal/toproto6/upgraderesourceidentity.go @@ -10,9 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeResourceIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse -// equivalent of a *fwserver.UpgradeResourceIdentityResponse. -func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { +// UpgradeIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeIdentityResponse. +func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto6/upgraderesourceidentity_test.go b/internal/toproto6/upgraderesourceidentity_test.go index 191d51832..cc567940b 100644 --- a/internal/toproto6/upgraderesourceidentity_test.go +++ b/internal/toproto6/upgraderesourceidentity_test.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestUpgradeResourceIdentityResponse(t *testing.T) { +func TestUpgradeIdentityResponse(t *testing.T) { t.Parallel() testIdentityProto6Type := tftypes.Object{ @@ -59,7 +59,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeResourceIdentityResponse + input *fwserver.UpgradeIdentityResponse expected *tfprotov6.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeResourceIdentityResponse{}, + input: &fwserver.UpgradeIdentityResponse{}, expected: &tfprotov6.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeResourceIdentityResponse{ + input: &fwserver.UpgradeIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov6.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto6.UpgradeResourceIdentityResponse(context.Background(), testCase.input) + got := toproto6.UpgradeIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index 372f67e19..cee02d283 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -8,19 +8,19 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" ) -// Implementation handler for an UpgradeResourceIdentity operation. +// Implementation handler for an UpgradeIdentity operation. // // This is used to encapsulate all upgrade logic from a prior identity to the // current version when a Resource implements the -// ResourceWithUpgradeResourceIdentity interface. +// ResourceWithUpgradeIdentity interface. type IdentityUpgrader struct { // Schema information for the prior identity version. While not required, - // setting this will populate the UpgradeResourceIdentityRequest type Identity + // setting this will populate the UpgradeIdentityRequest type Identity // field similar to other Resource data types. This allows for easier data // handling such as calling Get() or GetAttribute(). // // If not set, prior identity data is available in the - // UpgradeResourceIdentityRequest type RawIdentity field. + // UpgradeIdentityRequest type RawIdentity field. PriorSchema *identityschema.Schema // Provider defined logic for upgrading a resource identity from the prior @@ -29,11 +29,11 @@ type IdentityUpgrader struct { // The context.Context parameter contains framework-defined loggers and // supports request cancellation. // - // The UpgradeResourceIdentityRequest parameter contains the prior identity data. + // The UpgradeIdentityRequest parameter contains the prior identity data. // If PriorSchema was set, the Identity field will be available. Otherwise, // the RawIdentity must be used. // - // The UpgradeResourceIdentityResponse parameter should contain the upgraded + // The UpgradeIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. - IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) + IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) } diff --git a/resource/identityschema/schema.go b/resource/identityschema/schema.go index 3eb498b93..6d68aff9a 100644 --- a/resource/identityschema/schema.go +++ b/resource/identityschema/schema.go @@ -29,7 +29,7 @@ type Schema struct { // Version indicates the current version of the resource identity schema. Resource // identity schema versioning enables identity state upgrades in conjunction with the - // [resource.ResourceWithUpgradeResourceIdentity] interface. Versioning is only + // [resource.ResourceWithUpgradeIdentity] interface. Versioning is only // required if there is a breaking change involving existing identity state data, // such as changing an attribute type in a manner that is incompatible with the Terraform type. // diff --git a/resource/resource.go b/resource/resource.go index 95f8300a1..b15bbb80b 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -216,7 +216,7 @@ type ResourceWithValidateConfig interface { // by the identity object via the ResourceWithImportState interface. // - The resource identity should not change during the lifecycle of the remote object. That is, from the // creation of the remote object in the remote system until its destruction. An exception to this rule -// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeResourceIdentity interface. +// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeIdentity interface. type ResourceWithIdentity interface { Resource @@ -224,7 +224,7 @@ type ResourceWithIdentity interface { IdentitySchema(context.Context, IdentitySchemaRequest, *IdentitySchemaResponse) } -type ResourceWithUpgradeResourceIdentity interface { +type ResourceWithUpgradeIdentity interface { Resource // A mapping of the prior identity version to current identity upgrade @@ -236,5 +236,5 @@ type ResourceWithUpgradeResourceIdentity interface { // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the // requested identity version not be implemented. - UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader + UpgradeIdentity(context.Context) map[int64]IdentityUpgrader } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index 0855a72ec..ade327cca 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -11,7 +11,7 @@ import ( // Request information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityRequest struct { +type UpgradeIdentityRequest struct { // Previous state of the resource identity in JSON format // (Terraform CLI 0.12 and later) This data is always available, // regardless of whether the wrapping IdentityUpgrader type @@ -31,7 +31,7 @@ type UpgradeResourceIdentityRequest struct { // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityResponse struct { +type UpgradeIdentityResponse struct { // Upgraded identity of the resource, which should match the current identity //schema version. // From 7894a361bcbb97530327c8efe4bde5bdb29d6737 Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 12 May 2025 10:17:17 -0400 Subject: [PATCH 09/11] Renaming to UpgradeIdentity confirmed to fail corner tests --- .../fromproto5/upgraderesourceidentity.go | 6 +- .../upgraderesourceidentity_test.go | 12 +- .../fromproto6/upgraderesourceidentity.go | 6 +- .../upgraderesourceidentity_test.go | 12 +- .../server_upgraderesourceidentity.go | 50 ++++---- .../server_upgraderesourceidentity_test.go | 112 +++++++++--------- .../server_upgraderesourceidentity.go | 20 ++-- .../server_upgraderesourceidentity_test.go | 22 ++-- .../server_upgraderesourceidentity.go | 20 ++-- .../server_upgraderesourceidentity_test.go | 22 ++-- ...resourcewithconfigureandupgradeidentity.go | 28 ++--- .../resourcewithupgradeidentity.go | 22 ++-- internal/toproto5/upgraderesourceidentity.go | 6 +- .../toproto5/upgraderesourceidentity_test.go | 14 +-- internal/toproto6/upgraderesourceidentity.go | 6 +- .../toproto6/upgraderesourceidentity_test.go | 14 +-- resource/identity_upgrader.go | 14 +-- resource/identityschema/schema.go | 2 +- resource/resource.go | 6 +- resource/upgrade_identity.go | 4 +- 20 files changed, 199 insertions(+), 199 deletions(-) diff --git a/internal/fromproto5/upgraderesourceidentity.go b/internal/fromproto5/upgraderesourceidentity.go index 6f8a18ea7..4b279d057 100644 --- a/internal/fromproto5/upgraderesourceidentity.go +++ b/internal/fromproto5/upgraderesourceidentity.go @@ -13,9 +13,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest +// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest // equivalent of a *tfprotov5.UpgradeResourceIdentityRequest. -func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { +func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { if proto5 == nil { return nil, nil } @@ -36,7 +36,7 @@ func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResour return nil, diags } - fw := &fwserver.UpgradeIdentityRequest{ + fw := &fwserver.UpgradeResourceIdentityRequest{ RawState: (*tfprotov6.RawState)(proto5.RawIdentity), IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto5/upgraderesourceidentity_test.go b/internal/fromproto5/upgraderesourceidentity_test.go index 6967c57a1..e7257812d 100644 --- a/internal/fromproto5/upgraderesourceidentity_test.go +++ b/internal/fromproto5/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -func TestUpgradeIdentityRequest(t *testing.T) { +func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { input *tfprotov5.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeIdentityRequest + expected *fwserver.UpgradeResourceIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewTfprotov6RawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov5.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto5.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fromproto6/upgraderesourceidentity.go b/internal/fromproto6/upgraderesourceidentity.go index 7bc20429e..849eb3b28 100644 --- a/internal/fromproto6/upgraderesourceidentity.go +++ b/internal/fromproto6/upgraderesourceidentity.go @@ -12,9 +12,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest +// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest // equivalent of a *tfprotov6.UpgradeResourceIdentityRequest. -func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) { +func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) { if proto6 == nil { return nil, nil } @@ -35,7 +35,7 @@ func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResour return nil, diags } - fw := &fwserver.UpgradeIdentityRequest{ + fw := &fwserver.UpgradeResourceIdentityRequest{ RawState: proto6.RawIdentity, IdentitySchema: identitySchema, Resource: resource, diff --git a/internal/fromproto6/upgraderesourceidentity_test.go b/internal/fromproto6/upgraderesourceidentity_test.go index 952f45869..6cf766e6d 100644 --- a/internal/fromproto6/upgraderesourceidentity_test.go +++ b/internal/fromproto6/upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -func TestUpgradeIdentityRequest(t *testing.T) { +func TestUpgradeResourceIdentityRequest(t *testing.T) { t.Parallel() testIdentitySchema := identityschema.Schema{ @@ -33,7 +33,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { input *tfprotov6.UpgradeResourceIdentityRequest identitySchema fwschema.Schema resource resource.Resource - expected *fwserver.UpgradeIdentityRequest + expected *fwserver.UpgradeResourceIdentityRequest expectedDiagnostics diag.Diagnostics }{ "nil": { @@ -47,7 +47,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { }), }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "test_attribute": "test-value", }), @@ -57,7 +57,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { "resourceschema": { input: &tfprotov6.UpgradeResourceIdentityRequest{}, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, }, }, @@ -79,7 +79,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { Version: 123, }, identitySchema: testIdentitySchema, - expected: &fwserver.UpgradeIdentityRequest{ + expected: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Version: 123, }, @@ -90,7 +90,7 @@ func TestUpgradeIdentityRequest(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, diags := fromproto6.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) + got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index d514b1949..f26973069 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -16,9 +16,9 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -// UpgradeIdentityRequest is the framework server request for the -// UpgradeIdentity RPC. -type UpgradeIdentityRequest struct { +// UpgradeResourceIdentityRequest is the framework server request for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityRequest struct { Resource resource.Resource IdentitySchema fwschema.Schema // TypeName is the type of resource that Terraform needs to upgrade the @@ -36,15 +36,15 @@ type UpgradeIdentityRequest struct { RawState *tfprotov6.RawState } -// UpgradeIdentityResponse is the framework server response for the -// UpgradeIdentity RPC. -type UpgradeIdentityResponse struct { +// UpgradeResourceIdentityResponse is the framework server response for the +// UpgradeResourceIdentity RPC. +type UpgradeResourceIdentityResponse struct { UpgradedIdentity *tfsdk.ResourceIdentity Diagnostics diag.Diagnostics } -// UpgradeIdentity implements the framework server UpgradeIdentity RPC. -func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityRequest, resp *UpgradeIdentityResponse) { +// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC. +func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) { if req == nil { return } @@ -68,7 +68,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques if req.Version == req.IdentitySchema.GetVersion() { resp.Diagnostics.AddError( "Unexpected Identity Upgrade Request", - "Terraform Core invoked UpgradeIdentity even though the stored identity schema version matches the current version. "+ + "Terraform Core invoked UpgradeResourceIdentity even though the stored identity schema version matches the current version. "+ "This likely indicates a bug in the Terraform provider framework or Terraform Core. "+ "Please report this issue to the provider developer.", ) @@ -94,23 +94,23 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques } } - resourceWithUpgradeIdentity, ok := req.Resource.(resource.ResourceWithUpgradeIdentity) + resourceWithUpgradeResourceIdentity, ok := req.Resource.(resource.ResourceWithUpgradeResourceIdentity) if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeIdentity() method, "+ + "This resource was implemented without an UpgradeResourceIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") + logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeResourceIdentity") - logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") - resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeIdentity(ctx) - logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") + logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeResourceIdentity") + resourceIdentityUpgraders := resourceWithUpgradeResourceIdentity.UpgradeResourceIdentity(ctx) + logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeResourceIdentity") // Panic prevention if resourceIdentityUpgraders == nil { @@ -122,19 +122,19 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques if !ok { resp.Diagnostics.AddError( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ fmt.Sprintf("however Terraform was expecting an implementation for version %d upgrade.\n\n", req.Version)+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ) return } - UpgradeIdentityRequest := resource.UpgradeIdentityRequest{ + UpgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ RawIdentity: req.RawState, } if resourceIdentityUpgrader.PriorSchema != nil { - logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState") + logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) @@ -142,21 +142,21 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+ "Please report this to the provider developer:\n\n"+err.Error(), ) return } - UpgradeIdentityRequest.Identity = &tfsdk.ResourceIdentity{ + UpgradeResourceIdentityRequest.Identity = &tfsdk.ResourceIdentity{ Raw: rawIdentityValue, // from the output of req.RawState.UnmarshalWithOpts Schema: *resourceIdentityUpgrader.PriorSchema, } } - UpgradeIdentityResponse := resource.UpgradeIdentityResponse{ + UpgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. @@ -169,16 +169,16 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques // any errors. logging.FrameworkTrace(ctx, "Calling provider defined IdentityUpgrader") - resourceIdentityUpgrader.IdentityUpgrader(ctx, UpgradeIdentityRequest, &UpgradeIdentityResponse) + resourceIdentityUpgrader.IdentityUpgrader(ctx, UpgradeResourceIdentityRequest, &UpgradeResourceIdentityResponse) logging.FrameworkTrace(ctx, "Called provider defined IdentityUpgrader") - resp.Diagnostics.Append(UpgradeIdentityResponse.Diagnostics...) + resp.Diagnostics.Append(UpgradeResourceIdentityResponse.Diagnostics...) if resp.Diagnostics.HasError() { return } - if UpgradeIdentityResponse.Identity.Raw.Type() == nil || UpgradeIdentityResponse.Identity.Raw.IsNull() { + if UpgradeResourceIdentityResponse.Identity.Raw.Type() == nil || UpgradeResourceIdentityResponse.Identity.Raw.IsNull() { resp.Diagnostics.AddError( "Missing Upgraded Resource Identity", fmt.Sprintf("After attempting a resource Identity upgrade to version %d, the provider did not return any Identity data. ", req.Version)+ @@ -188,5 +188,5 @@ func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityReques return } - resp.UpgradedIdentity = UpgradeIdentityResponse.Identity + resp.UpgradedIdentity = UpgradeResourceIdentityResponse.Identity } diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index e0e5a19ac..6716f2134 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -21,7 +21,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) -func TestServerUpgradeIdentity(t *testing.T) { +func TestServerUpgradeResourceIdentity(t *testing.T) { t.Parallel() ctx := context.Background() @@ -39,26 +39,26 @@ func TestServerUpgradeIdentity(t *testing.T) { testCases := map[string]struct { server *fwserver.Server - request *fwserver.UpgradeIdentityRequest - expectedResponse *fwserver.UpgradeIdentityResponse + request *fwserver.UpgradeResourceIdentityRequest + expectedResponse *fwserver.UpgradeResourceIdentityResponse }{ "empty-provider": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{}, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, }, "resource-configure-data": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, ResourceConfigureData: "test-provider-configure-value", }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithConfigureAndUpgradeIdentity{ + Resource: &testprovider.ResourceWithConfigureAndUpgradeResourceIdentity{ ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { providerData, ok := req.ProviderData.(string) @@ -78,7 +78,7 @@ func TestServerUpgradeIdentity(t *testing.T) { } }, Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -88,7 +88,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // In practice, the Configure method would save the // provider data to the Resource implementation and // use it here. The fact that Configure is able to @@ -102,7 +102,7 @@ func TestServerUpgradeIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -147,7 +147,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -160,28 +160,28 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ IdentitySchema: testIdentitySchema, Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{}, + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{}, }, "RawState-Unmarshal-and-ResourceIdentity": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { RawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -229,7 +229,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -242,18 +242,18 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { var RawState struct { Id string `json:"id"` } @@ -281,7 +281,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -290,11 +290,11 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - "ResourceType-UpgradeIdentity-not-implemented": { + "ResourceType-UpgradeResourceIdentity-not-implemented": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, @@ -303,40 +303,40 @@ func TestServerUpgradeIdentity(t *testing.T) { Resource: &testprovider.Resource{}, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented without an UpgradeIdentity() method, "+ + "This resource was implemented without an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), }, }, }, - "ResourceType-UpgradeIdentity-empty": { + "ResourceType-UpgradeResourceIdentity-empty": { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "required_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 0 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -347,15 +347,15 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", "optional_for_import_attribute": true, }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &identityschema.Schema{ @@ -368,7 +368,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // Expect error before reaching this logic. }, }, @@ -377,10 +377,10 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", "There was an error reading the saved resource Identity using the prior resource schema defined for version 0 upgrade.\n\n"+ "Please report this to the provider developer:\n\n"+ "AttributeName(\"optional_for_import_attribute\"): unsupported type bool sent as tftypes.Number", @@ -392,18 +392,18 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -412,7 +412,7 @@ func TestServerUpgradeIdentity(t *testing.T) { if err != nil { resp.Diagnostics.AddError( - "Unable to Read Previously Saved Identity for UpgradeIdentity", + "Unable to Read Previously Saved Identity for UpgradeResourceIdentity", fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Identity.Schema.GetVersion())+ "Please report this to the provider developer:\n\n"+err.Error(), ) @@ -457,7 +457,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{ "id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -470,17 +470,17 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity }, }, @@ -489,7 +489,7 @@ func TestServerUpgradeIdentity(t *testing.T) { }, Version: 0, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Missing Upgraded Resource Identity", @@ -504,24 +504,24 @@ func TestServerUpgradeIdentity(t *testing.T) { server: &fwserver.Server{ Provider: &testprovider.Provider{}, }, - request: &fwserver.UpgradeIdentityRequest{ + request: &fwserver.UpgradeResourceIdentityRequest{ RawState: testNewRawState(t, map[string]interface{}{ "id": "test-id-value", }), IdentitySchema: testIdentitySchema, - Resource: &testprovider.ResourceWithUpgradeIdentity{ + Resource: &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{}, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return nil }, }, Version: 999, }, - expectedResponse: &fwserver.UpgradeIdentityResponse{ + expectedResponse: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Unable to Upgrade Resource Identity", - "This resource was implemented with an UpgradeIdentity() method, "+ + "This resource was implemented with an UpgradeResourceIdentity() method, "+ "however Terraform was expecting an implementation for version 999 upgrade.\n\n"+ "This is always an issue with the Terraform Provider and should be reported to the provider developer.", ), @@ -534,8 +534,8 @@ func TestServerUpgradeIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - response := &fwserver.UpgradeIdentityResponse{} - testCase.server.UpgradeIdentity(context.Background(), testCase.request, response) + response := &fwserver.UpgradeResourceIdentityResponse{} + testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request, response) if diff := cmp.Diff(response, testCase.expectedResponse); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/proto5server/server_upgraderesourceidentity.go b/internal/proto5server/server_upgraderesourceidentity.go index 4574dd979..a0dcfb463 100644 --- a/internal/proto5server/server_upgraderesourceidentity.go +++ b/internal/proto5server/server_upgraderesourceidentity.go @@ -13,15 +13,15 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -// UpgradeIdentity satisfies the tfprotov5.ProviderServer interface. -func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { +// UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface. +func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeIdentityResponse{} + fwResp := &fwserver.UpgradeResourceIdentityResponse{} if proto5Req == nil { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto5Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto5Req *tfprotov5.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto5.UpgradeIdentityRequest(ctx, proto5Req, resource, identitySchema) + fwReq, diags := fromproto5.UpgradeResourceIdentityRequest(ctx, proto5Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) - return toproto5.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto5.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 6c0bcdd22..0a1998981 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestServerUpgradeIdentity(t *testing.T) { +func TestServerUpgradeResourceIdentity(t *testing.T) { t.Parallel() testSchema := schema.Schema{ @@ -70,7 +70,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/proto6server/server_upgraderesourceidentity.go b/internal/proto6server/server_upgraderesourceidentity.go index ecd2fbf0b..7484eb49f 100644 --- a/internal/proto6server/server_upgraderesourceidentity.go +++ b/internal/proto6server/server_upgraderesourceidentity.go @@ -13,15 +13,15 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeIdentity satisfies the tfprotov6.ProviderServer interface. -func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { +// UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface. +func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) { ctx = s.registerContext(ctx) ctx = logging.InitContext(ctx) - fwResp := &fwserver.UpgradeIdentityResponse{} + fwResp := &fwserver.UpgradeResourceIdentityResponse{} if proto6Req == nil { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } resource, diags := s.FrameworkServer.Resource(ctx, proto6Req.TypeName) @@ -29,7 +29,7 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName) @@ -37,18 +37,18 @@ func (s *Server) UpgradeIdentity(ctx context.Context, proto6Req *tfprotov6.Upgra fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - fwReq, diags := fromproto6.UpgradeIdentityRequest(ctx, proto6Req, resource, identitySchema) + fwReq, diags := fromproto6.UpgradeResourceIdentityRequest(ctx, proto6Req, resource, identitySchema) fwResp.Diagnostics.Append(diags...) if fwResp.Diagnostics.HasError() { - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } - s.FrameworkServer.UpgradeIdentity(ctx, fwReq, fwResp) + s.FrameworkServer.UpgradeResourceIdentity(ctx, fwReq, fwResp) - return toproto6.UpgradeIdentityResponse(ctx, fwResp), nil + return toproto6.UpgradeResourceIdentityResponse(ctx, fwResp), nil } diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index 8f999e2e9..d11c2adb5 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestServerUpgradeIdentity(t *testing.T) { +func TestServerUpgradeResourceIdentity(t *testing.T) { t.Parallel() testSchema := schema.Schema{ @@ -70,7 +70,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -79,10 +79,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -170,7 +170,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -179,10 +179,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -229,7 +229,7 @@ func TestServerUpgradeIdentity(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithUpgradeIdentity{ + return &testprovider.ResourceWithUpgradeResourceIdentity{ Resource: &testprovider.Resource{ SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = testSchema @@ -238,10 +238,10 @@ func TestServerUpgradeIdentity(t *testing.T) { resp.TypeName = "test_resource" }, }, - UpgradeIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { + UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), @@ -283,7 +283,7 @@ func TestServerUpgradeIdentity(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got, err := testCase.server.UpgradeIdentity(context.Background(), testCase.request) + got, err := testCase.server.UpgradeResourceIdentity(context.Background(), testCase.request) if diff := cmp.Diff(testCase.expectedError, err); diff != "" { t.Errorf("unexpected error difference: %s", diff) diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go index 851da6fec..da74ae5f1 100644 --- a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -9,23 +9,23 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithConfigureAndUpgradeIdentity{} -var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeIdentity{} -var _ resource.ResourceWithUpgradeIdentity = &ResourceWithConfigureAndUpgradeIdentity{} +var _ resource.Resource = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithConfigureAndUpgradeResourceIdentity{} -// Declarative resource.ResourceWithConfigureAndUpgradeIdentity for unit testing. -type ResourceWithConfigureAndUpgradeIdentity struct { +// Declarative resource.ResourceWithConfigureAndUpgradeResourceIdentity for unit testing. +type ResourceWithConfigureAndUpgradeResourceIdentity struct { *Resource - // ResourceWithConfigureAndUpgradeIdentity interface methods + // ResourceWithConfigureAndUpgradeResourceIdentity interface methods ConfigureMethod func(context.Context, resource.ConfigureRequest, *resource.ConfigureResponse) - // ResourceWithUpgradeIdentity interface methods - UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeResourceIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader } -// Configure satisfies the resource.ResourceWithConfigureAndUpgradeIdentity interface. -func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { +// Configure satisfies the resource.ResourceWithConfigureAndUpgradeResourceIdentity interface. +func (r *ResourceWithConfigureAndUpgradeResourceIdentity) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { if r.ConfigureMethod == nil { return } @@ -33,11 +33,11 @@ func (r *ResourceWithConfigureAndUpgradeIdentity) Configure(ctx context.Context, r.ConfigureMethod(ctx, req, resp) } -// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (r *ResourceWithConfigureAndUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if r.UpgradeIdentityMethod == nil { +// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. +func (r *ResourceWithConfigureAndUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if r.UpgradeResourceIdentityMethod == nil { return nil } - return r.UpgradeIdentityMethod(ctx) + return r.UpgradeResourceIdentityMethod(ctx) } diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go index 6ecb96cbe..bc7ae54ab 100644 --- a/internal/testing/testprovider/resourcewithupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -9,31 +9,31 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" ) -var _ resource.Resource = &ResourceWithUpgradeIdentity{} -var _ resource.ResourceWithUpgradeIdentity = &ResourceWithUpgradeIdentity{} +var _ resource.Resource = &ResourceWithUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithUpgradeResourceIdentity{} -// Declarative resource.ResourceWithUpgradeIdentity for unit testing. -type ResourceWithUpgradeIdentity struct { +// Declarative resource.ResourceWithUpgradeResourceIdentity for unit testing. +type ResourceWithUpgradeResourceIdentity struct { *Resource - // ResourceWithUpgradeIdentity interface methods - UpgradeIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader + // ResourceWithUpgradeResourceIdentity interface methods + UpgradeResourceIdentityMethod func(context.Context) map[int64]resource.IdentityUpgrader // ResourceWithIdentity interface methods IdentitySchemaMethod func(context.Context, resource.IdentitySchemaRequest, *resource.IdentitySchemaResponse) } -// UpgradeIdentity satisfies the resource.ResourceWithUpgradeIdentity interface. -func (p *ResourceWithUpgradeIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { - if p.UpgradeIdentityMethod == nil { +// UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. +func (p *ResourceWithUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { + if p.UpgradeResourceIdentityMethod == nil { return nil } - return p.UpgradeIdentityMethod(ctx) + return p.UpgradeResourceIdentityMethod(ctx) } // IdentitySchema implements resource.ResourceWithIdentity. -func (p *ResourceWithUpgradeIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { +func (p *ResourceWithUpgradeResourceIdentity) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { if p.IdentitySchemaMethod == nil { return } diff --git a/internal/toproto5/upgraderesourceidentity.go b/internal/toproto5/upgraderesourceidentity.go index a63aaa346..57ea1d9a8 100644 --- a/internal/toproto5/upgraderesourceidentity.go +++ b/internal/toproto5/upgraderesourceidentity.go @@ -10,9 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov5" ) -// UpgradeIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse -// equivalent of a *fwserver.UpgradeIdentityResponse. -func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { +// UpgradeResourceIdentityResponse returns the *tfprotov5.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeResourceIdentityResponse. +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov5.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto5/upgraderesourceidentity_test.go b/internal/toproto5/upgraderesourceidentity_test.go index 48badb3e4..619303bd0 100644 --- a/internal/toproto5/upgraderesourceidentity_test.go +++ b/internal/toproto5/upgraderesourceidentity_test.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestUpgradeIdentityResponse(t *testing.T) { +func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Parallel() testIdentityProto5Type := tftypes.Object{ @@ -59,7 +59,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeIdentityResponse + input *fwserver.UpgradeResourceIdentityResponse expected *tfprotov5.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeIdentityResponse{}, + input: &fwserver.UpgradeResourceIdentityResponse{}, expected: &tfprotov5.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov5.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto5.UpgradeIdentityResponse(context.Background(), testCase.input) + got := toproto5.UpgradeResourceIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/internal/toproto6/upgraderesourceidentity.go b/internal/toproto6/upgraderesourceidentity.go index afe5bcf03..2748a4172 100644 --- a/internal/toproto6/upgraderesourceidentity.go +++ b/internal/toproto6/upgraderesourceidentity.go @@ -10,9 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) -// UpgradeIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse -// equivalent of a *fwserver.UpgradeIdentityResponse. -func UpgradeIdentityResponse(ctx context.Context, fw *fwserver.UpgradeIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { +// UpgradeResourceIdentityResponse returns the *tfprotov6.UpgradeResourceIdentityResponse +// equivalent of a *fwserver.UpgradeResourceIdentityResponse. +func UpgradeResourceIdentityResponse(ctx context.Context, fw *fwserver.UpgradeResourceIdentityResponse) *tfprotov6.UpgradeResourceIdentityResponse { if fw == nil { return nil } diff --git a/internal/toproto6/upgraderesourceidentity_test.go b/internal/toproto6/upgraderesourceidentity_test.go index cc567940b..191d51832 100644 --- a/internal/toproto6/upgraderesourceidentity_test.go +++ b/internal/toproto6/upgraderesourceidentity_test.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" ) -func TestUpgradeIdentityResponse(t *testing.T) { +func TestUpgradeResourceIdentityResponse(t *testing.T) { t.Parallel() testIdentityProto6Type := tftypes.Object{ @@ -59,7 +59,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { } testCases := map[string]struct { - input *fwserver.UpgradeIdentityResponse + input *fwserver.UpgradeResourceIdentityResponse expected *tfprotov6.UpgradeResourceIdentityResponse }{ "nil": { @@ -67,11 +67,11 @@ func TestUpgradeIdentityResponse(t *testing.T) { expected: nil, }, "empty": { - input: &fwserver.UpgradeIdentityResponse{}, + input: &fwserver.UpgradeResourceIdentityResponse{}, expected: &tfprotov6.UpgradeResourceIdentityResponse{}, }, "diagnostics": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -93,7 +93,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { }, }, "diagnostics-invalid-upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ Diagnostics: diag.Diagnostics{ diag.NewWarningDiagnostic("test warning summary", "test warning details"), diag.NewErrorDiagnostic("test error summary", "test error details"), @@ -124,7 +124,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { }, }, "upgradedIdentity": { - input: &fwserver.UpgradeIdentityResponse{ + input: &fwserver.UpgradeResourceIdentityResponse{ UpgradedIdentity: testIdentity, }, expected: &tfprotov6.UpgradeResourceIdentityResponse{ @@ -139,7 +139,7 @@ func TestUpgradeIdentityResponse(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - got := toproto6.UpgradeIdentityResponse(context.Background(), testCase.input) + got := toproto6.UpgradeResourceIdentityResponse(context.Background(), testCase.input) if diff := cmp.Diff(got, testCase.expected); diff != "" { t.Errorf("unexpected difference: %s", diff) diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index cee02d283..372f67e19 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -8,19 +8,19 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" ) -// Implementation handler for an UpgradeIdentity operation. +// Implementation handler for an UpgradeResourceIdentity operation. // // This is used to encapsulate all upgrade logic from a prior identity to the // current version when a Resource implements the -// ResourceWithUpgradeIdentity interface. +// ResourceWithUpgradeResourceIdentity interface. type IdentityUpgrader struct { // Schema information for the prior identity version. While not required, - // setting this will populate the UpgradeIdentityRequest type Identity + // setting this will populate the UpgradeResourceIdentityRequest type Identity // field similar to other Resource data types. This allows for easier data // handling such as calling Get() or GetAttribute(). // // If not set, prior identity data is available in the - // UpgradeIdentityRequest type RawIdentity field. + // UpgradeResourceIdentityRequest type RawIdentity field. PriorSchema *identityschema.Schema // Provider defined logic for upgrading a resource identity from the prior @@ -29,11 +29,11 @@ type IdentityUpgrader struct { // The context.Context parameter contains framework-defined loggers and // supports request cancellation. // - // The UpgradeIdentityRequest parameter contains the prior identity data. + // The UpgradeResourceIdentityRequest parameter contains the prior identity data. // If PriorSchema was set, the Identity field will be available. Otherwise, // the RawIdentity must be used. // - // The UpgradeIdentityResponse parameter should contain the upgraded + // The UpgradeResourceIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. - IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) + IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) } diff --git a/resource/identityschema/schema.go b/resource/identityschema/schema.go index 6d68aff9a..3eb498b93 100644 --- a/resource/identityschema/schema.go +++ b/resource/identityschema/schema.go @@ -29,7 +29,7 @@ type Schema struct { // Version indicates the current version of the resource identity schema. Resource // identity schema versioning enables identity state upgrades in conjunction with the - // [resource.ResourceWithUpgradeIdentity] interface. Versioning is only + // [resource.ResourceWithUpgradeResourceIdentity] interface. Versioning is only // required if there is a breaking change involving existing identity state data, // such as changing an attribute type in a manner that is incompatible with the Terraform type. // diff --git a/resource/resource.go b/resource/resource.go index b15bbb80b..95f8300a1 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -216,7 +216,7 @@ type ResourceWithValidateConfig interface { // by the identity object via the ResourceWithImportState interface. // - The resource identity should not change during the lifecycle of the remote object. That is, from the // creation of the remote object in the remote system until its destruction. An exception to this rule -// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeIdentity interface. +// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeResourceIdentity interface. type ResourceWithIdentity interface { Resource @@ -224,7 +224,7 @@ type ResourceWithIdentity interface { IdentitySchema(context.Context, IdentitySchemaRequest, *IdentitySchemaResponse) } -type ResourceWithUpgradeIdentity interface { +type ResourceWithUpgradeResourceIdentity interface { Resource // A mapping of the prior identity version to current identity upgrade @@ -236,5 +236,5 @@ type ResourceWithUpgradeIdentity interface { // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the // requested identity version not be implemented. - UpgradeIdentity(context.Context) map[int64]IdentityUpgrader + UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index ade327cca..0855a72ec 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -11,7 +11,7 @@ import ( // Request information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeIdentityRequest struct { +type UpgradeResourceIdentityRequest struct { // Previous state of the resource identity in JSON format // (Terraform CLI 0.12 and later) This data is always available, // regardless of whether the wrapping IdentityUpgrader type @@ -31,7 +31,7 @@ type UpgradeIdentityRequest struct { // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeIdentityResponse struct { +type UpgradeResourceIdentityResponse struct { // Upgraded identity of the resource, which should match the current identity //schema version. // From f2bb5ef195ed4ee1a5c5e7af572285d0f9d462a0 Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 12 May 2025 14:01:03 -0400 Subject: [PATCH 10/11] Renaming just for the resource --- internal/fwserver/server_upgraderesourceidentity.go | 4 ++-- .../fwserver/server_upgraderesourceidentity_test.go | 12 ++++++------ .../server_upgraderesourceidentity_test.go | 6 +++--- .../server_upgraderesourceidentity_test.go | 6 +++--- resource/identity_upgrader.go | 2 +- resource/upgrade_identity.go | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index f26973069..5b74b2e79 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -129,7 +129,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - UpgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{ + UpgradeResourceIdentityRequest := resource.UpgradeIdentityRequest{ RawIdentity: req.RawState, } @@ -156,7 +156,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour } - UpgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{ + UpgradeResourceIdentityResponse := resource.UpgradeIdentityResponse{ Identity: &tfsdk.ResourceIdentity{ Schema: req.IdentitySchema, // Raw is intentionally not set. diff --git a/internal/fwserver/server_upgraderesourceidentity_test.go b/internal/fwserver/server_upgraderesourceidentity_test.go index 6716f2134..48c15c85d 100644 --- a/internal/fwserver/server_upgraderesourceidentity_test.go +++ b/internal/fwserver/server_upgraderesourceidentity_test.go @@ -88,7 +88,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // In practice, the Configure method would save the // provider data to the Resource implementation and // use it here. The fact that Configure is able to @@ -181,7 +181,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { RawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -253,7 +253,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { var RawState struct { Id string `json:"id"` } @@ -368,7 +368,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { }, }, }, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Expect error before reaching this logic. }, }, @@ -403,7 +403,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { return map[int64]resource.IdentityUpgrader{ 0: { PriorSchema: &testIdentitySchema, - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { rawStateValue, err := req.RawIdentity.Unmarshal(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "id": tftypes.String, @@ -480,7 +480,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(ctx context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity }, }, diff --git a/internal/proto5server/server_upgraderesourceidentity_test.go b/internal/proto5server/server_upgraderesourceidentity_test.go index 0a1998981..70849ccc2 100644 --- a/internal/proto5server/server_upgraderesourceidentity_test.go +++ b/internal/proto5server/server_upgraderesourceidentity_test.go @@ -82,7 +82,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewTfprotov6RawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -182,7 +182,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -241,7 +241,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), diff --git a/internal/proto6server/server_upgraderesourceidentity_test.go b/internal/proto6server/server_upgraderesourceidentity_test.go index d11c2adb5..092f8b2da 100644 --- a/internal/proto6server/server_upgraderesourceidentity_test.go +++ b/internal/proto6server/server_upgraderesourceidentity_test.go @@ -82,7 +82,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, req resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { expectedSourceIdentity := testNewRawState(t, map[string]interface{}{ "test_id": "test-id-value", }) @@ -182,7 +182,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") }, @@ -241,7 +241,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) { UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader { return map[int64]resource.IdentityUpgrader{ 0: { - IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) { + IdentityUpgrader: func(_ context.Context, _ resource.UpgradeIdentityRequest, resp *resource.UpgradeIdentityResponse) { resp.Identity = &tfsdk.ResourceIdentity{ Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ "test_id": tftypes.NewValue(tftypes.String, "test-id-value"), diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index 372f67e19..e9ed6f2a3 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -35,5 +35,5 @@ type IdentityUpgrader struct { // // The UpgradeResourceIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. - IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse) + IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) } diff --git a/resource/upgrade_identity.go b/resource/upgrade_identity.go index 0855a72ec..ade327cca 100644 --- a/resource/upgrade_identity.go +++ b/resource/upgrade_identity.go @@ -11,7 +11,7 @@ import ( // Request information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityRequest struct { +type UpgradeIdentityRequest struct { // Previous state of the resource identity in JSON format // (Terraform CLI 0.12 and later) This data is always available, // regardless of whether the wrapping IdentityUpgrader type @@ -31,7 +31,7 @@ type UpgradeResourceIdentityRequest struct { // Response information for the provider logic to update a resource identity // from a prior resource identity version to the current identity version. -type UpgradeResourceIdentityResponse struct { +type UpgradeIdentityResponse struct { // Upgraded identity of the resource, which should match the current identity //schema version. // From 63bcd6665ce21f2d7a578d402e732af6b4a9a61d Mon Sep 17 00:00:00 2001 From: Rain Date: Mon, 12 May 2025 16:02:08 -0400 Subject: [PATCH 11/11] Renaming the method and interface just for the resource --- internal/fwserver/server_upgraderesourceidentity.go | 12 ++++++------ .../resourcewithconfigureandupgradeidentity.go | 4 ++-- .../testprovider/resourcewithupgradeidentity.go | 4 ++-- resource/identity_upgrader.go | 12 ++++++------ resource/identityschema/schema.go | 2 +- resource/resource.go | 6 +++--- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/fwserver/server_upgraderesourceidentity.go b/internal/fwserver/server_upgraderesourceidentity.go index 5b74b2e79..a67f55793 100644 --- a/internal/fwserver/server_upgraderesourceidentity.go +++ b/internal/fwserver/server_upgraderesourceidentity.go @@ -94,7 +94,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour } } - resourceWithUpgradeResourceIdentity, ok := req.Resource.(resource.ResourceWithUpgradeResourceIdentity) + resourceWithUpgradeResourceIdentity, ok := req.Resource.(resource.ResourceWithUpgradeIdentity) if !ok { resp.Diagnostics.AddError( @@ -106,11 +106,11 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour return } - logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeResourceIdentity") + logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity") - logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeResourceIdentity") - resourceIdentityUpgraders := resourceWithUpgradeResourceIdentity.UpgradeResourceIdentity(ctx) - logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeResourceIdentity") + logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity") + resourceIdentityUpgraders := resourceWithUpgradeResourceIdentity.UpgradeIdentity(ctx) + logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity") // Panic prevention if resourceIdentityUpgraders == nil { @@ -134,7 +134,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour } if resourceIdentityUpgrader.PriorSchema != nil { - logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState") + logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState") priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx) diff --git a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go index da74ae5f1..7234dda47 100644 --- a/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithconfigureandupgradeidentity.go @@ -11,7 +11,7 @@ import ( var _ resource.Resource = &ResourceWithConfigureAndUpgradeResourceIdentity{} var _ resource.ResourceWithConfigure = &ResourceWithConfigureAndUpgradeResourceIdentity{} -var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithConfigureAndUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithConfigureAndUpgradeResourceIdentity{} // Declarative resource.ResourceWithConfigureAndUpgradeResourceIdentity for unit testing. type ResourceWithConfigureAndUpgradeResourceIdentity struct { @@ -34,7 +34,7 @@ func (r *ResourceWithConfigureAndUpgradeResourceIdentity) Configure(ctx context. } // UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. -func (r *ResourceWithConfigureAndUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { +func (r *ResourceWithConfigureAndUpgradeResourceIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { if r.UpgradeResourceIdentityMethod == nil { return nil } diff --git a/internal/testing/testprovider/resourcewithupgradeidentity.go b/internal/testing/testprovider/resourcewithupgradeidentity.go index bc7ae54ab..48af0f92b 100644 --- a/internal/testing/testprovider/resourcewithupgradeidentity.go +++ b/internal/testing/testprovider/resourcewithupgradeidentity.go @@ -10,7 +10,7 @@ import ( ) var _ resource.Resource = &ResourceWithUpgradeResourceIdentity{} -var _ resource.ResourceWithUpgradeResourceIdentity = &ResourceWithUpgradeResourceIdentity{} +var _ resource.ResourceWithUpgradeIdentity = &ResourceWithUpgradeResourceIdentity{} // Declarative resource.ResourceWithUpgradeResourceIdentity for unit testing. type ResourceWithUpgradeResourceIdentity struct { @@ -24,7 +24,7 @@ type ResourceWithUpgradeResourceIdentity struct { } // UpgradeResourceIdentity satisfies the resource.ResourceWithUpgradeResourceIdentity interface. -func (p *ResourceWithUpgradeResourceIdentity) UpgradeResourceIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { +func (p *ResourceWithUpgradeResourceIdentity) UpgradeIdentity(ctx context.Context) map[int64]resource.IdentityUpgrader { if p.UpgradeResourceIdentityMethod == nil { return nil } diff --git a/resource/identity_upgrader.go b/resource/identity_upgrader.go index e9ed6f2a3..cee02d283 100644 --- a/resource/identity_upgrader.go +++ b/resource/identity_upgrader.go @@ -8,19 +8,19 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" ) -// Implementation handler for an UpgradeResourceIdentity operation. +// Implementation handler for an UpgradeIdentity operation. // // This is used to encapsulate all upgrade logic from a prior identity to the // current version when a Resource implements the -// ResourceWithUpgradeResourceIdentity interface. +// ResourceWithUpgradeIdentity interface. type IdentityUpgrader struct { // Schema information for the prior identity version. While not required, - // setting this will populate the UpgradeResourceIdentityRequest type Identity + // setting this will populate the UpgradeIdentityRequest type Identity // field similar to other Resource data types. This allows for easier data // handling such as calling Get() or GetAttribute(). // // If not set, prior identity data is available in the - // UpgradeResourceIdentityRequest type RawIdentity field. + // UpgradeIdentityRequest type RawIdentity field. PriorSchema *identityschema.Schema // Provider defined logic for upgrading a resource identity from the prior @@ -29,11 +29,11 @@ type IdentityUpgrader struct { // The context.Context parameter contains framework-defined loggers and // supports request cancellation. // - // The UpgradeResourceIdentityRequest parameter contains the prior identity data. + // The UpgradeIdentityRequest parameter contains the prior identity data. // If PriorSchema was set, the Identity field will be available. Otherwise, // the RawIdentity must be used. // - // The UpgradeResourceIdentityResponse parameter should contain the upgraded + // The UpgradeIdentityResponse parameter should contain the upgraded // identity data and can be used to signal any logic warnings or errors. IdentityUpgrader func(context.Context, UpgradeIdentityRequest, *UpgradeIdentityResponse) } diff --git a/resource/identityschema/schema.go b/resource/identityschema/schema.go index 3eb498b93..6d68aff9a 100644 --- a/resource/identityschema/schema.go +++ b/resource/identityschema/schema.go @@ -29,7 +29,7 @@ type Schema struct { // Version indicates the current version of the resource identity schema. Resource // identity schema versioning enables identity state upgrades in conjunction with the - // [resource.ResourceWithUpgradeResourceIdentity] interface. Versioning is only + // [resource.ResourceWithUpgradeIdentity] interface. Versioning is only // required if there is a breaking change involving existing identity state data, // such as changing an attribute type in a manner that is incompatible with the Terraform type. // diff --git a/resource/resource.go b/resource/resource.go index 95f8300a1..b15bbb80b 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -216,7 +216,7 @@ type ResourceWithValidateConfig interface { // by the identity object via the ResourceWithImportState interface. // - The resource identity should not change during the lifecycle of the remote object. That is, from the // creation of the remote object in the remote system until its destruction. An exception to this rule -// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeResourceIdentity interface. +// is an upgrade of the identity data after a schema change, via the ResourceWithUpgradeIdentity interface. type ResourceWithIdentity interface { Resource @@ -224,7 +224,7 @@ type ResourceWithIdentity interface { IdentitySchema(context.Context, IdentitySchemaRequest, *IdentitySchemaResponse) } -type ResourceWithUpgradeResourceIdentity interface { +type ResourceWithUpgradeIdentity interface { Resource // A mapping of the prior identity version to current identity upgrade @@ -236,5 +236,5 @@ type ResourceWithUpgradeResourceIdentity interface { // Version keys begin at 0, which is the default schema version when // undefined. The framework will return an error diagnostic should the // requested identity version not be implemented. - UpgradeResourceIdentity(context.Context) map[int64]IdentityUpgrader + UpgradeIdentity(context.Context) map[int64]IdentityUpgrader }