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

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