-
Notifications
You must be signed in to change notification settings - Fork 14
/
pluginServer.go
229 lines (205 loc) · 8.13 KB
/
pluginServer.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package grpc
import (
"fmt"
"github.com/hashicorp/go-plugin"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe-plugin-sdk/v5/anywhere"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
pluginshared "github.com/turbot/steampipe-plugin-sdk/v5/grpc/shared"
"github.com/turbot/steampipe-plugin-sdk/v5/row_stream"
"github.com/turbot/steampipe-plugin-sdk/v5/version"
)
type ExecuteFunc func(req *proto.ExecuteRequest, stream row_stream.Sender) error
type GetSchemaFunc func(string) (*PluginSchema, error)
type SetConnectionConfigFunc func(string, string) error
type SetAllConnectionConfigsFunc func([]*proto.ConnectionConfig, int) (map[string]error, error)
type UpdateConnectionConfigsFunc func([]*proto.ConnectionConfig, []*proto.ConnectionConfig, []*proto.ConnectionConfig) (map[string]error, error)
type SetCacheOptionsFunc func(*proto.SetCacheOptionsRequest) error
type SetConnectionCacheOptionsFunc func(*proto.SetConnectionCacheOptionsRequest) error
type SetRateLimitersFunc func(*proto.SetRateLimitersRequest) error
type GetRateLimitersFunc func() []*proto.RateLimiterDefinition
type EstablishMessageStreamFunc func(stream proto.WrapperPlugin_EstablishMessageStreamServer) error
type GetSchemaModeFunc func() string
// PluginServer is the server for a single plugin
type PluginServer struct {
proto.UnimplementedWrapperPluginServer
pluginName string
executeFunc ExecuteFunc
setConnectionConfigFunc SetConnectionConfigFunc
setAllConnectionConfigsFunc SetAllConnectionConfigsFunc
updateConnectionConfigsFunc UpdateConnectionConfigsFunc
getSchemaFunc GetSchemaFunc
establishMessageStreamFunc EstablishMessageStreamFunc
setCacheOptionsFunc SetCacheOptionsFunc
setConnectionCacheOptionsFunc SetConnectionCacheOptionsFunc
setRateLimitersFunc SetRateLimitersFunc
getRateLimitersFunc GetRateLimitersFunc
getSchemaModeFunc GetSchemaModeFunc
}
func NewPluginServer(pluginName string,
setConnectionConfigFunc SetConnectionConfigFunc,
setAllConnectionConfigsFunc SetAllConnectionConfigsFunc,
updateConnectionConfigsFunc UpdateConnectionConfigsFunc,
getSchemaFunc GetSchemaFunc,
executeFunc ExecuteFunc,
establishMessageStreamFunc EstablishMessageStreamFunc,
setCacheOptionsFunc SetCacheOptionsFunc,
setRateLimitersFunc SetRateLimitersFunc,
getRateLimitersFunc GetRateLimitersFunc,
setConnectionCacheOptionsFunc SetConnectionCacheOptionsFunc,
GetSchemaModeFunc GetSchemaModeFunc,
) *PluginServer {
return &PluginServer{
pluginName: pluginName,
executeFunc: executeFunc,
setConnectionConfigFunc: setConnectionConfigFunc,
setAllConnectionConfigsFunc: setAllConnectionConfigsFunc,
updateConnectionConfigsFunc: updateConnectionConfigsFunc,
getSchemaFunc: getSchemaFunc,
establishMessageStreamFunc: establishMessageStreamFunc,
setCacheOptionsFunc: setCacheOptionsFunc,
setRateLimitersFunc: setRateLimitersFunc,
getRateLimitersFunc: getRateLimitersFunc,
setConnectionCacheOptionsFunc: setConnectionCacheOptionsFunc,
getSchemaModeFunc: GetSchemaModeFunc,
}
}
func (s PluginServer) GetSchema(req *proto.GetSchemaRequest) (res *proto.GetSchemaResponse, err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
schema, err := s.getSchemaFunc(req.Connection)
if err != nil {
return nil, err
}
return &proto.GetSchemaResponse{
Schema: &proto.Schema{
Schema: schema.Schema,
Mode: schema.Mode,
SdkVersion: version.String(),
ProtocolVersion: version.ProtocolVersion,
},
}, err
}
// Execute implements the WrapperPluginServer interface and is used to execute calls vis GRPC
func (s PluginServer) Execute(req *proto.ExecuteRequest, stream proto.WrapperPlugin_ExecuteServer) (err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
// NOTE: Compatibility
// if a pre-v16 version of Steampipe is being used,
// the deprecated Connection, CacheEnabled and CacheTtl will be set, and ExecuteConnectionData will be nil
// populate ExecuteConnectionData
if req.ExecuteConnectionData == nil {
if req.Connection == "" {
return fmt.Errorf("either ExecuteConnectionData or Connection must be provided")
}
req.ExecuteConnectionData = map[string]*proto.ExecuteConnectionData{
req.Connection: {
Limit: req.QueryContext.Limit,
CacheEnabled: req.CacheEnabled,
CacheTtl: req.CacheTtl,
},
}
}
return s.executeFunc(req, stream)
}
// CallExecuteAsync directly calls the execute function and is used to execute in-process
func (s PluginServer) CallExecuteAsync(req *proto.ExecuteRequest, stream *anywhere.LocalPluginStream) {
go func() {
defer func() {
if r := recover(); r != nil {
stream.Error(helpers.ToError(r))
}
}()
err := s.executeFunc(req, stream)
if err != nil {
stream.Error(err)
}
}()
}
func (s PluginServer) SetConnectionConfig(req *proto.SetConnectionConfigRequest) (res *proto.SetConnectionConfigResponse, err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
err = s.setConnectionConfigFunc(req.ConnectionName, req.ConnectionConfig)
return &proto.SetConnectionConfigResponse{}, err
}
func (s PluginServer) SetAllConnectionConfigs(req *proto.SetAllConnectionConfigsRequest) (res *proto.SetConnectionConfigResponse, err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
failedConnections, err := s.setAllConnectionConfigsFunc(req.Configs, int(req.MaxCacheSizeMb))
res = &proto.SetConnectionConfigResponse{
FailedConnections: make(map[string]string),
}
for connectionName, err := range failedConnections {
res.FailedConnections[connectionName] = err.Error()
}
return
}
func (s PluginServer) UpdateConnectionConfigs(req *proto.UpdateConnectionConfigsRequest) (res *proto.UpdateConnectionConfigsResponse, err error) {
defer func() {
if r := recover(); r != nil {
err = helpers.ToError(r)
}
}()
failedConnections, err := s.updateConnectionConfigsFunc(req.Added, req.Deleted, req.Changed)
res = &proto.UpdateConnectionConfigsResponse{
FailedConnections: make(map[string]string),
}
for connectionName, err := range failedConnections {
res.FailedConnections[connectionName] = err.Error()
}
return &proto.UpdateConnectionConfigsResponse{}, err
}
func (s PluginServer) GetSupportedOperations(*proto.GetSupportedOperationsRequest) (*proto.GetSupportedOperationsResponse, error) {
return &proto.GetSupportedOperationsResponse{
QueryCache: true,
MultipleConnections: true,
MessageStream: true,
SetCacheOptions: true,
RateLimiters: true,
}, nil
}
func (s PluginServer) SetCacheOptions(req *proto.SetCacheOptionsRequest) (*proto.SetCacheOptionsResponse, error) {
err := s.setCacheOptionsFunc(req)
return &proto.SetCacheOptionsResponse{}, err
}
func (s PluginServer) SetConnectionCacheOptions(req *proto.SetConnectionCacheOptionsRequest) (*proto.SetConnectionCacheOptionsResponse, error) {
err := s.setConnectionCacheOptionsFunc(req)
return &proto.SetConnectionCacheOptionsResponse{}, err
}
func (s PluginServer) SetRateLimiters(req *proto.SetRateLimitersRequest) (*proto.SetRateLimitersResponse, error) {
err := s.setRateLimitersFunc(req)
return &proto.SetRateLimitersResponse{}, err
}
func (s PluginServer) GetRateLimiters(*proto.GetRateLimitersRequest) (*proto.GetRateLimitersResponse, error) {
rateLimiters := s.getRateLimitersFunc()
return &proto.GetRateLimitersResponse{Definitions: rateLimiters}, nil
}
func (s PluginServer) EstablishMessageStream(stream proto.WrapperPlugin_EstablishMessageStreamServer) error {
return s.establishMessageStreamFunc(stream)
}
func (s PluginServer) GetSchemaMode() string {
return s.getSchemaModeFunc()
}
func (s PluginServer) Serve() {
pluginMap := map[string]plugin.Plugin{
s.pluginName: &pluginshared.WrapperPlugin{Impl: s},
}
plugin.Serve(&plugin.ServeConfig{
Plugins: pluginMap,
GRPCServer: plugin.DefaultGRPCServer,
// A non-nil value here enables gRPC serving for this plugin...
HandshakeConfig: pluginshared.Handshake,
})
}