-
Notifications
You must be signed in to change notification settings - Fork 2k
/
client.go
65 lines (53 loc) · 1.68 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package base
import (
"context"
"fmt"
"github.com/hashicorp/nomad/helper/pluginutils/grpcutils"
"github.com/hashicorp/nomad/plugins/base/proto"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
)
// BasePluginClient implements the client side of a remote base plugin, using
// gRPC to communicate to the remote plugin.
type BasePluginClient struct {
Client proto.BasePluginClient
// DoneCtx is closed when the plugin exits
DoneCtx context.Context
}
func (b *BasePluginClient) PluginInfo() (*PluginInfoResponse, error) {
presp, err := b.Client.PluginInfo(b.DoneCtx, &proto.PluginInfoRequest{})
if err != nil {
return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
}
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{
Type: ptype,
PluginApiVersions: presp.GetPluginApiVersions(),
PluginVersion: presp.GetPluginVersion(),
Name: presp.GetName(),
}
return resp, nil
}
func (b *BasePluginClient) ConfigSchema() (*hclspec.Spec, error) {
presp, err := b.Client.ConfigSchema(b.DoneCtx, &proto.ConfigSchemaRequest{})
if err != nil {
return nil, grpcutils.HandleGrpcErr(err, b.DoneCtx)
}
return presp.GetSpec(), nil
}
func (b *BasePluginClient) SetConfig(c *Config) error {
// Send the config
_, err := b.Client.SetConfig(b.DoneCtx, &proto.SetConfigRequest{
MsgpackConfig: c.PluginConfig,
NomadConfig: c.AgentConfig.toProto(),
PluginApiVersion: c.ApiVersion,
})
return grpcutils.HandleGrpcErr(err, b.DoneCtx)
}