Skip to content
Merged
47 changes: 47 additions & 0 deletions internal/fromproto5/upgraderesourceidentity.go
Original file line number Diff line number Diff line change
@@ -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
}
104 changes: 104 additions & 0 deletions internal/fromproto5/upgraderesourceidentity_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
46 changes: 46 additions & 0 deletions internal/fromproto6/upgraderesourceidentity.go
Original file line number Diff line number Diff line change
@@ -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
}
104 changes: 104 additions & 0 deletions internal/fromproto6/upgraderesourceidentity_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading
Loading