open-nomad/plugins/base/client.go

66 lines
1.7 KiB
Go
Raw Normal View History

2018-08-13 17:52:59 +00:00
package base
2018-08-10 17:50:09 +00:00
import (
2018-08-13 17:52:59 +00:00
"context"
2018-08-10 17:50:09 +00:00
"fmt"
2019-01-23 14:27:14 +00:00
"github.com/hashicorp/nomad/helper/pluginutils/grpcutils"
2018-08-10 17:50:09 +00:00
"github.com/hashicorp/nomad/plugins/base/proto"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
)
2018-08-13 17:29:29 +00:00
// BasePluginClient implements the client side of a remote base plugin, using
2018-08-10 17:50:09 +00:00
// gRPC to communicate to the remote plugin.
2018-08-13 17:29:29 +00:00
type BasePluginClient struct {
Client proto.BasePluginClient
// DoneCtx is closed when the plugin exits
DoneCtx context.Context
2018-08-10 17:50:09 +00:00
}
2018-08-13 17:29:29 +00:00
func (b *BasePluginClient) PluginInfo() (*PluginInfoResponse, error) {
presp, err := b.Client.PluginInfo(b.DoneCtx, &proto.PluginInfoRequest{})
2018-08-10 17:50:09 +00:00
if err != nil {
2018-12-21 19:23:21 +00:00
return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
2018-08-10 17:50:09 +00:00
}
var ptype string
switch presp.GetType() {
case proto.PluginType_DRIVER:
ptype = PluginTypeDriver
case proto.PluginType_DEVICE:
ptype = PluginTypeDevice
default:
return nil, fmt.Errorf("plugin is of unknown type: %q", presp.GetType().String())
}
resp := &PluginInfoResponse{
2018-12-14 23:03:31 +00:00
Type: ptype,
PluginApiVersions: presp.GetPluginApiVersions(),
PluginVersion: presp.GetPluginVersion(),
Name: presp.GetName(),
2018-08-10 17:50:09 +00:00
}
return resp, nil
}
2018-08-13 17:29:29 +00:00
func (b *BasePluginClient) ConfigSchema() (*hclspec.Spec, error) {
presp, err := b.Client.ConfigSchema(b.DoneCtx, &proto.ConfigSchemaRequest{})
2018-08-10 17:50:09 +00:00
if err != nil {
2018-12-21 19:23:21 +00:00
return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
2018-08-10 17:50:09 +00:00
}
return presp.GetSpec(), nil
}
2018-12-14 23:03:31 +00:00
func (b *BasePluginClient) SetConfig(c *Config) error {
2018-08-10 17:50:09 +00:00
// Send the config
_, err := b.Client.SetConfig(b.DoneCtx, &proto.SetConfigRequest{
2018-12-14 23:03:31 +00:00
MsgpackConfig: c.PluginConfig,
NomadConfig: c.AgentConfig.toProto(),
PluginApiVersion: c.ApiVersion,
2018-08-10 17:50:09 +00:00
})
2018-12-21 19:23:21 +00:00
return grpcutils.HandleGrpcErr(err, b.DoneCtx)
2018-08-10 17:50:09 +00:00
}