github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/base/client.go (about)

     1  package base
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/nomad/helper/pluginutils/grpcutils"
     8  	"github.com/hashicorp/nomad/plugins/base/proto"
     9  	"github.com/hashicorp/nomad/plugins/shared/hclspec"
    10  )
    11  
    12  // BasePluginClient implements the client side of a remote base plugin, using
    13  // gRPC to communicate to the remote plugin.
    14  type BasePluginClient struct {
    15  	Client proto.BasePluginClient
    16  
    17  	// DoneCtx is closed when the plugin exits
    18  	DoneCtx context.Context
    19  }
    20  
    21  func (b *BasePluginClient) PluginInfo() (*PluginInfoResponse, error) {
    22  	presp, err := b.Client.PluginInfo(b.DoneCtx, &proto.PluginInfoRequest{})
    23  	if err != nil {
    24  		return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
    25  	}
    26  
    27  	var ptype string
    28  	switch presp.GetType() {
    29  	case proto.PluginType_DRIVER:
    30  		ptype = PluginTypeDriver
    31  	case proto.PluginType_DEVICE:
    32  		ptype = PluginTypeDevice
    33  	default:
    34  		return nil, fmt.Errorf("plugin is of unknown type: %q", presp.GetType().String())
    35  	}
    36  
    37  	resp := &PluginInfoResponse{
    38  		Type:              ptype,
    39  		PluginApiVersions: presp.GetPluginApiVersions(),
    40  		PluginVersion:     presp.GetPluginVersion(),
    41  		Name:              presp.GetName(),
    42  	}
    43  
    44  	return resp, nil
    45  }
    46  
    47  func (b *BasePluginClient) ConfigSchema() (*hclspec.Spec, error) {
    48  	presp, err := b.Client.ConfigSchema(b.DoneCtx, &proto.ConfigSchemaRequest{})
    49  	if err != nil {
    50  		return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
    51  	}
    52  
    53  	return presp.GetSpec(), nil
    54  }
    55  
    56  func (b *BasePluginClient) SetConfig(c *Config) error {
    57  	// Send the config
    58  	_, err := b.Client.SetConfig(b.DoneCtx, &proto.SetConfigRequest{
    59  		MsgpackConfig:    c.PluginConfig,
    60  		NomadConfig:      c.AgentConfig.toProto(),
    61  		PluginApiVersion: c.ApiVersion,
    62  	})
    63  
    64  	return grpcutils.HandleGrpcErr(err, b.DoneCtx)
    65  }