Skip to content

Commit

Permalink
Support parameterization at the gRPC level
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 25, 2024
1 parent 9378ea6 commit a28a0b5
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"
"os"

"github.com/blang/semver"
"github.com/hashicorp/go-multierror"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
pprovider "github.com/pulumi/pulumi/pkg/v3/resource/provider"
Expand Down Expand Up @@ -315,6 +316,9 @@ type Provider struct {

// GetSchema fetches the schema for this resource provider.
GetSchema func(context.Context, GetSchemaRequest) (GetSchemaResponse, error)

Parameterize func(context.Context, ParameterizeRequest) (ParameterizeResponse, error)

// Cancel signals the provider to gracefully shut down and abort any ongoing resource operations.
// Operations aborted in this way will return an error (e.g., `Update` and `Create` will either return a
// creation error or an initialization error). Since Cancel is advisory and non-blocking, it is up
Expand Down Expand Up @@ -380,6 +384,13 @@ func (d Provider) WithDefaults() Provider {
return nyi("Cancel")
}
}

if d.Parameterize == nil {
d.Parameterize = func(context.Context, ParameterizeRequest) (ParameterizeResponse, error) {
return ParameterizeResponse{}, nyi("Parameterize")
}
}

if d.CheckConfig == nil {
d.CheckConfig = func(context.Context, CheckRequest) (CheckResponse, error) {
return CheckResponse{}, nyi("CheckConfig")
Expand Down Expand Up @@ -1091,6 +1102,61 @@ func (p *provider) Cancel(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty

}

type ParameterizeRequest struct {
Args *ParameterizeRequestArgs
Value *ParameterizeRequestValue
}

type ParameterizeRequestArgs struct {
Args []string
}

type ParameterizeRequestValue struct {
Name string
Version semver.Version
Value []byte
}

type ParameterizeResponse struct {
Name string
Version *semver.Version
}

func (p *provider) Parameterize(ctx context.Context, req *rpc.ParameterizeRequest) (*rpc.ParameterizeResponse, error) {
var parsedRequest ParameterizeRequest

switch params := req.Parameters.(type) {
case *rpc.ParameterizeRequest_Args:
parsedRequest.Args = &ParameterizeRequestArgs{
Args: params.Args.GetArgs(),
}
case *rpc.ParameterizeRequest_Value:
version, err := semver.Parse(params.Value.Version)
if err != nil {
return nil, rpcerror.Wrap(codes.InvalidArgument, err, "invalid version")
}
parsedRequest.Value = &ParameterizeRequestValue{
Name: params.Value.Name,
Version: version,
Value: params.Value.Value,
}
}

resp, err := p.client.Parameterize(p.ctx(ctx, ""), parsedRequest)
if err != nil {
return nil, err
}
var version string
if resp.Version != nil {
version = resp.Version.String()
}

return &rpc.ParameterizeResponse{
Name: resp.Name,
Version: version,
}, nil
}

func (p *provider) GetPluginInfo(context.Context, *emptypb.Empty) (*rpc.PluginInfo, error) {
return &rpc.PluginInfo{
Version: p.version,
Expand Down

0 comments on commit a28a0b5

Please sign in to comment.