github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/plugins/base/server.go (about)

     1  package base
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	plugin "github.com/hashicorp/go-plugin"
     9  	"github.com/hashicorp/nomad/plugins/base/proto"
    10  )
    11  
    12  // basePluginServer wraps a base plugin and exposes it via gRPC.
    13  type basePluginServer struct {
    14  	broker *plugin.GRPCBroker
    15  	impl   BasePlugin
    16  }
    17  
    18  func (b *basePluginServer) PluginInfo(context.Context, *proto.PluginInfoRequest) (*proto.PluginInfoResponse, error) {
    19  	resp, err := b.impl.PluginInfo()
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	var ptype proto.PluginType
    25  	switch resp.Type {
    26  	case PluginTypeDriver:
    27  		ptype = proto.PluginType_DRIVER
    28  	case PluginTypeDevice:
    29  		ptype = proto.PluginType_DEVICE
    30  	default:
    31  		return nil, fmt.Errorf("plugin is of unknown type: %q", resp.Type)
    32  	}
    33  
    34  	presp := &proto.PluginInfoResponse{
    35  		Type:             ptype,
    36  		PluginApiVersion: resp.PluginApiVersion,
    37  		PluginVersion:    resp.PluginVersion,
    38  		Name:             resp.Name,
    39  	}
    40  
    41  	return presp, nil
    42  }
    43  
    44  func (b *basePluginServer) ConfigSchema(context.Context, *proto.ConfigSchemaRequest) (*proto.ConfigSchemaResponse, error) {
    45  	spec, err := b.impl.ConfigSchema()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	presp := &proto.ConfigSchemaResponse{
    51  		Spec: spec,
    52  	}
    53  
    54  	return presp, nil
    55  }
    56  
    57  func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRequest) (*proto.SetConfigResponse, error) {
    58  	// Set the config
    59  	if err := b.impl.SetConfig(req.GetMsgpackConfig()); err != nil {
    60  		return nil, fmt.Errorf("SetConfig failed: %v", err)
    61  	}
    62  
    63  	return &proto.SetConfigResponse{}, nil
    64  }