Skip to content

Commit

Permalink
address review
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Nov 25, 2024
1 parent 10b8e75 commit 12bb0f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
38 changes: 29 additions & 9 deletions pkg/pf/internal/plugin/provider_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
pl "github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -76,7 +76,27 @@ func (p *providerServer) checkNYI(method string, err error) error {
return err
}

func (p *providerServer) marshalDiff(diff plugin.DiffResult) (*pulumirpc.DiffResponse, error) {
func pluginDiffKindToRPC(kind pl.DiffKind) pulumirpc.PropertyDiff_Kind {
switch kind {
case pl.DiffAdd:
return pulumirpc.PropertyDiff_ADD
case pl.DiffAddReplace:
return pulumirpc.PropertyDiff_ADD_REPLACE
case pl.DiffDelete:
return pulumirpc.PropertyDiff_DELETE
case pl.DiffDeleteReplace:
return pulumirpc.PropertyDiff_DELETE_REPLACE
case pl.DiffUpdate:
return pulumirpc.PropertyDiff_UPDATE
case pl.DiffUpdateReplace:
return pulumirpc.PropertyDiff_UPDATE_REPLACE
default:
contract.Assertf(false, "unknown diff kind: %v", kind)
return pulumirpc.PropertyDiff_ADD
}
}

func (p *providerServer) marshalDiff(diff pl.DiffResult) (*pulumirpc.DiffResponse, error) {
var changes pulumirpc.DiffResponse_DiffChanges
switch diff.Changes {
case pl.DiffNone:
Expand All @@ -94,7 +114,7 @@ func (p *providerServer) marshalDiff(diff plugin.DiffResult) (*pulumirpc.DiffRes
detailedDiff = make(map[string]*pulumirpc.PropertyDiff)
for path, diff := range diff.DetailedDiff {
detailedDiff[path] = &pulumirpc.PropertyDiff{
Kind: pulumirpc.PropertyDiff_Kind(diff.Kind), //nolint:gosec
Kind: pluginDiffKindToRPC(diff.Kind),
InputDiff: diff.InputDiff,
}
}
Expand All @@ -120,21 +140,21 @@ func (p *providerServer) marshalDiff(diff plugin.DiffResult) (*pulumirpc.DiffRes
}

type forwardServer struct {
plugin.UnimplementedProvider
pl.UnimplementedProvider

parameterize func(context.Context, plugin.ParameterizeRequest) (plugin.ParameterizeResponse, error)
parameterize func(context.Context, pl.ParameterizeRequest) (pl.ParameterizeResponse, error)
}

func (p forwardServer) Parameterize(
ctx context.Context, req plugin.ParameterizeRequest,
) (plugin.ParameterizeResponse, error) {
ctx context.Context, req pl.ParameterizeRequest,
) (pl.ParameterizeResponse, error) {
return p.parameterize(ctx, req)
}

func (p *providerServer) Parameterize(
ctx context.Context, req *pulumirpc.ParameterizeRequest,
) (*pulumirpc.ParameterizeResponse, error) {
return plugin.NewProviderServer(&forwardServer{
return pl.NewProviderServer(&forwardServer{
parameterize: p.provider.ParameterizeWithContext,
}).Parameterize(ctx, req)
}
Expand All @@ -150,7 +170,7 @@ func (p *providerServer) GetSchema(ctx context.Context,
}
subpackageVersion = &ver
}
schema, err := p.provider.GetSchemaWithContext(ctx, plugin.GetSchemaRequest{
schema, err := p.provider.GetSchemaWithContext(ctx, pl.GetSchemaRequest{
Version: req.GetVersion(),
SubpackageName: req.SubpackageName,
SubpackageVersion: subpackageVersion,
Expand Down
37 changes: 8 additions & 29 deletions pkg/pf/internal/plugin/provider_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,12 @@ package plugin
import (
"testing"

"github.com/hexops/autogold/v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"github.com/stretchr/testify/require"
)

// autogold gets confused about the enums, so we just re-define them here.
//
//nolint:revive
const (
ADD pulumirpc.PropertyDiff_Kind = 0
ADD_REPLACE pulumirpc.PropertyDiff_Kind = 1
DELETE pulumirpc.PropertyDiff_Kind = 2
DELETE_REPLACE pulumirpc.PropertyDiff_Kind = 3
UPDATE pulumirpc.PropertyDiff_Kind = 4
UPDATE_REPLACE pulumirpc.PropertyDiff_Kind = 5
)

//nolint:revive
const (
DIFF_UNKNOWN pulumirpc.DiffResponse_DiffChanges = 0
DIFF_NONE pulumirpc.DiffResponse_DiffChanges = 1
DIFF_SOME pulumirpc.DiffResponse_DiffChanges = 2
)

//nolint:unconvert
func TestMarshalDiff(t *testing.T) {
t.Parallel()

Expand All @@ -48,11 +27,11 @@ func TestMarshalDiff(t *testing.T) {
DetailedDiff: map[string]plugin.PropertyDiff{},
}

autogold.Expect(&pulumirpc.DiffResponse{
require.Equal(t, &pulumirpc.DiffResponse{
Replaces: []string{},
Changes: pulumirpc.DiffResponse_DIFF_NONE,
Diffs: []string{},
}).Equal(t, runTest(t, diff))
}, runTest(t, diff))
})

t.Run("diff without detailed diff", func(t *testing.T) {
Expand All @@ -62,11 +41,11 @@ func TestMarshalDiff(t *testing.T) {
ChangedKeys: []resource.PropertyKey{"change"},
}

autogold.Expect(&pulumirpc.DiffResponse{
require.Equal(t, &pulumirpc.DiffResponse{
Replaces: []string{"replace"},
Changes: pulumirpc.DiffResponse_DIFF_SOME,
Diffs: []string{"change"},
}).Equal(t, runTest(t, diff))
}, runTest(t, diff))
})

t.Run("diff with detailed diff", func(t *testing.T) {
Expand All @@ -84,20 +63,20 @@ func TestMarshalDiff(t *testing.T) {
},
}

autogold.Expect(&pulumirpc.DiffResponse{
require.Equal(t, &pulumirpc.DiffResponse{
Replaces: []string{
"replace",
},
Changes: pulumirpc.DiffResponse_DiffChanges(DIFF_SOME),
Changes: pulumirpc.DiffResponse_DIFF_SOME,
Diffs: []string{
"change",
"replace",
},
DetailedDiff: map[string]*pulumirpc.PropertyDiff{
"change": {},
"replace": {Kind: pulumirpc.PropertyDiff_Kind(DELETE_REPLACE)},
"replace": {Kind: pulumirpc.PropertyDiff_DELETE_REPLACE},
},
HasDetailedDiff: true,
}).Equal(t, runTest(t, diff))
}, runTest(t, diff))
})
}

0 comments on commit 12bb0f7

Please sign in to comment.