Skip to content

Commit

Permalink
Setting up private data for use in ReadResource RPC (#399)
Browse files Browse the repository at this point in the history
Reference: #399
  • Loading branch information
bendbennett committed Aug 2, 2022
1 parent 827ee38 commit 48d25c6
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 9 deletions.
28 changes: 28 additions & 0 deletions internal/fromproto5/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fromproto5

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
)

// PrivateData returns the privatestate.Data for []byte.
func PrivateData(ctx context.Context, input []byte) (privatestate.Data, diag.Diagnostics) {
var diags diag.Diagnostics

output, err := privatestate.NewData(ctx, input)
if err != nil {
diags.AddError(
"Unable to Create Private Data",
"An unexpected error was encountered when creating the private data. "+
"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"+
"Private data create error.",
)

return privatestate.Data{}, diags
}

return output, nil
}
9 changes: 8 additions & 1 deletion internal/fromproto5/readresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package fromproto5
import (
"context"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
)

// ReadResourceRequest returns the *fwserver.ReadResourceRequest
Expand Down Expand Up @@ -36,5 +37,11 @@ func ReadResourceRequest(ctx context.Context, proto5 *tfprotov5.ReadResourceRequ

fw.ProviderMeta = providerMeta

privateData, privateDataDiags := PrivateData(ctx, proto5.Private)

diags.Append(privateDataDiags...)

fw.PrivateData = privateData

return fw, diags
}
28 changes: 28 additions & 0 deletions internal/fromproto6/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fromproto6

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
)

// PrivateData returns the privatestate.Data for []byte.
func PrivateData(ctx context.Context, input []byte) (privatestate.Data, diag.Diagnostics) {
var diags diag.Diagnostics

output, err := privatestate.NewData(ctx, input)
if err != nil {
diags.AddError(
"Unable to Create Private Data",
"An unexpected error was encountered when creating the private data. "+
"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"+
"Private data create error.",
)

return privatestate.Data{}, diags
}

return output, nil
}
9 changes: 8 additions & 1 deletion internal/fromproto6/readresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package fromproto6
import (
"context"

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

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

// ReadResourceRequest returns the *fwserver.ReadResourceRequest
Expand Down Expand Up @@ -36,5 +37,11 @@ func ReadResourceRequest(ctx context.Context, proto6 *tfprotov6.ReadResourceRequ

fw.ProviderMeta = providerMeta

privateData, privateDataDiags := PrivateData(ctx, proto6.Private)

diags.Append(privateDataDiags...)

fw.PrivateData = privateData

return fw, diags
}
7 changes: 7 additions & 0 deletions internal/fwserver/server_readresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand All @@ -16,6 +17,7 @@ type ReadResourceRequest struct {
CurrentState *tfsdk.State
ResourceType provider.ResourceType
Private []byte
PrivateData privatestate.Data
ProviderMeta *tfsdk.Config
}

Expand All @@ -25,6 +27,7 @@ type ReadResourceResponse struct {
Diagnostics diag.Diagnostics
NewState *tfsdk.State
Private []byte
PrivateData privatestate.Data
}

// ReadResource implements the framework server ReadResource RPC.
Expand Down Expand Up @@ -59,12 +62,14 @@ func (s *Server) ReadResource(ctx context.Context, req *ReadResourceRequest, res
Schema: req.CurrentState.Schema,
Raw: req.CurrentState.Raw.Copy(),
},
Private: req.PrivateData.Provider,
}
readResp := resource.ReadResponse{
State: tfsdk.State{
Schema: req.CurrentState.Schema,
Raw: req.CurrentState.Raw.Copy(),
},
Private: req.PrivateData.Provider,
}

if req.ProviderMeta != nil {
Expand All @@ -77,4 +82,6 @@ func (s *Server) ReadResource(ctx context.Context, req *ReadResourceRequest, res

resp.Diagnostics = readResp.Diagnostics
resp.NewState = &readResp.State
resp.PrivateData.Framework = req.PrivateData.Framework
resp.PrivateData.Provider = readResp.Private
}
28 changes: 28 additions & 0 deletions internal/toproto5/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package toproto5

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
)

// PrivateData returns []byte from privateState.Data.
func PrivateData(ctx context.Context, input privatestate.Data) ([]byte, diag.Diagnostics) {
var diags diag.Diagnostics

output, err := input.Bytes(ctx)
if err != nil {
diags.AddError(
"Unable to Convert Private Data",
"An unexpected error was encountered when converting the private data. "+
"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"+
"Private data convert error.",
)

return nil, diags
}

return output, nil
}
9 changes: 7 additions & 2 deletions internal/toproto5/readresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package toproto5
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
)

// ReadResourceResponse returns the *tfprotov5.ReadResourceResponse
Expand All @@ -16,13 +17,17 @@ func ReadResourceResponse(ctx context.Context, fw *fwserver.ReadResourceResponse

proto5 := &tfprotov5.ReadResourceResponse{
Diagnostics: Diagnostics(ctx, fw.Diagnostics),
Private: fw.Private,
}

newState, diags := State(ctx, fw.NewState)

proto5.Diagnostics = append(proto5.Diagnostics, Diagnostics(ctx, diags)...)
proto5.NewState = newState

newPrivate, diags := PrivateData(ctx, fw.PrivateData)

proto5.Diagnostics = append(proto5.Diagnostics, Diagnostics(ctx, diags)...)
proto5.Private = newPrivate

return proto5
}
28 changes: 28 additions & 0 deletions internal/toproto6/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package toproto6

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
)

// PrivateData returns []byte from privateState.Data.
func PrivateData(ctx context.Context, input privatestate.Data) ([]byte, diag.Diagnostics) {
var diags diag.Diagnostics

output, err := input.Bytes(ctx)
if err != nil {
diags.AddError(
"Unable to Convert Private Data",
"An unexpected error was encountered when converting the private data. "+
"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"+
"Private data convert error.",
)

return nil, diags
}

return output, nil
}
8 changes: 7 additions & 1 deletion internal/toproto6/readresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package toproto6
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
)

// ReadResourceResponse returns the *tfprotov6.ReadResourceResponse
Expand All @@ -24,5 +25,10 @@ func ReadResourceResponse(ctx context.Context, fw *fwserver.ReadResourceResponse
proto6.Diagnostics = append(proto6.Diagnostics, Diagnostics(ctx, diags)...)
proto6.NewState = newState

newPrivate, diags := PrivateData(ctx, fw.PrivateData)

proto6.Diagnostics = append(proto6.Diagnostics, Diagnostics(ctx, diags)...)
proto6.Private = newPrivate

return proto6
}
11 changes: 7 additions & 4 deletions internal/toproto6/readresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"testing"

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

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

func TestReadResourceResponse(t *testing.T) {
Expand Down Expand Up @@ -66,8 +67,10 @@ func TestReadResourceResponse(t *testing.T) {
expected: nil,
},
"empty": {
input: &fwserver.ReadResourceResponse{},
expected: &tfprotov6.ReadResourceResponse{},
input: &fwserver.ReadResourceResponse{},
expected: &tfprotov6.ReadResourceResponse{
Private: []byte(`{}`),
},
},
"diagnostics": {
input: &fwserver.ReadResourceResponse{
Expand Down
9 changes: 9 additions & 0 deletions resource/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resource

import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand All @@ -14,6 +15,9 @@ type ReadRequest struct {
// operation.
State tfsdk.State

// Private is resource private state data.
Private privatestate.ProviderData

// ProviderMeta is metadata from the provider_meta block of the module.
ProviderMeta tfsdk.Config
}
Expand All @@ -28,6 +32,11 @@ type ReadResponse struct {
// should be set during the resource's Read operation.
State tfsdk.State

// Private is the private state resource data following the Read operation.
// This field is pre-populated from ReadResourceRequest.Private and
// can be modified during the resource's Read operation.
Private privatestate.ProviderData

// Diagnostics report errors or warnings related to reading the
// resource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Expand Down

0 comments on commit 48d25c6

Please sign in to comment.