-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_client.go
114 lines (100 loc) · 2.72 KB
/
remote_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package script
import (
"fmt"
"github.com/bearded-web/bearded/models/plan"
"github.com/bearded-web/bearded/models/report"
"github.com/bearded-web/bearded/pkg/agent/api"
"github.com/bearded-web/bearded/pkg/transport"
"golang.org/x/net/context"
)
type RemoteClient struct {
transp transport.Transport
FakeClient // TODO (m0sth8): remove when implement all methods
connected chan struct{}
}
// Check compile time interface compatibilities
var _ ClientV1 = (*RemoteClient)(nil)
// Remote client helps communicate plugins with agent through transport
func NewRemoteClient(transport transport.Transport) (*RemoteClient, error) {
return &RemoteClient{
transp: transport,
connected: make(chan struct{}, 1),
}, nil
}
func (s *RemoteClient) Handle(ctx context.Context, msg transport.Extractor) (interface{}, error) {
req := api.RequestV1{}
err := msg.Extract(&req)
if err != nil {
return nil, err
}
switch req.Method {
case api.Connect:
s.connected <- struct{}{}
return nil, nil
default:
return nil, fmt.Errorf("Unknown method requested %s", req.Method)
}
}
func (s *RemoteClient) WaitForConnection(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-s.connected:
return nil
}
}
// Client Methods
func (c *RemoteClient) GetConfig(ctx context.Context) (*plan.Conf, error) {
req := api.RequestV1{
Method: api.GetConfig,
}
resp := api.ResponseV1{}
if err := c.transp.Request(ctx, req, &resp); err != nil {
return nil, err
}
return resp.GetConfig, nil
}
func (c *RemoteClient) GetPlugin(ctx context.Context, name string) (*Plugin, error) {
req := api.RequestV1{
Method: api.GetPluginVersions,
GetPluginVersions: name,
}
resp := api.ResponseV1{}
if err := c.transp.Request(ctx, req, &resp); err != nil {
return nil, err
}
return NewPlugin(name, c, resp.GetPluginVersions...), nil
}
func (c *RemoteClient) RunPlugin(ctx context.Context, step *plan.WorkflowStep) (*report.Report, error) {
req := api.RequestV1{
Method: api.RunPlugin,
RunPlugin: step,
}
resp := api.ResponseV1{}
if err := c.transp.Request(ctx, req, &resp); err != nil {
return nil, err
}
return resp.RunPlugin, nil
}
func (c *RemoteClient) SendReport(ctx context.Context, rep *report.Report) error {
req := api.RequestV1{
Method: api.SendReport,
SendReport: rep,
}
resp := api.ResponseV1{}
if err := c.transp.Request(ctx, req, &resp); err != nil {
return err
}
return nil
}
func (c *RemoteClient) DownloadFile(ctx context.Context, fileId string) ([]byte, error) {
req := api.RequestV1{
Method: api.DownloadFile,
DownloadFile: fileId,
}
resp := api.ResponseV1{}
if err := c.transp.Request(ctx, req, &resp); err != nil {
return nil, err
}
return resp.DownloadFile, nil
}