-
Notifications
You must be signed in to change notification settings - Fork 66
/
convert_to_protobuf.go
442 lines (394 loc) · 14.1 KB
/
convert_to_protobuf.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package backend
import (
"errors"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"google.golang.org/protobuf/proto"
)
// ConvertToProtobuf has a collection of methods for converting the autogenerated
// protobuf go code to our SDK objects. This object exists to attach a collection
// of conversion methods to.
//
// This is used internally by the SDK and inside Grafana-server, plugin authors should not
// need this functionality.
type ConvertToProtobuf struct{}
// ToProto returns a new ConvertToProtobuf.
func ToProto() ConvertToProtobuf {
return ConvertToProtobuf{}
}
// User converts the SDK version of a User to the protobuf version.
func (t ConvertToProtobuf) User(user *User) *pluginv2.User {
if user == nil {
return nil
}
return &pluginv2.User{
Login: user.Login,
Name: user.Name,
Email: user.Email,
Role: user.Role,
}
}
// AppInstanceSettings converts the SDK version of an AppInstanceSettings to the protobuf version.
func (t ConvertToProtobuf) AppInstanceSettings(s *AppInstanceSettings) *pluginv2.AppInstanceSettings {
if s == nil {
return nil
}
return &pluginv2.AppInstanceSettings{
JsonData: s.JSONData,
DecryptedSecureJsonData: s.DecryptedSecureJSONData,
LastUpdatedMS: s.Updated.UnixNano() / int64(time.Millisecond),
ApiVersion: s.APIVersion,
}
}
func AppInstanceSettingsToProtoBytes(s *AppInstanceSettings) ([]byte, error) {
if s == nil {
return nil, nil
}
return proto.Marshal(ConvertToProtobuf{}.AppInstanceSettings(s))
}
func AppInstanceSettingsFromProto(body []byte) (*AppInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.AppInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.AppInstanceSettings(tmp), nil
}
func DataSourceInstanceSettingsToProtoBytes(s *DataSourceInstanceSettings) ([]byte, error) {
if s == nil {
return nil, nil
}
return proto.Marshal(ConvertToProtobuf{}.DataSourceInstanceSettings(s))
}
func DataSourceInstanceSettingsFromProto(body []byte, pluginID string) (*DataSourceInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.DataSourceInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.DataSourceInstanceSettings(tmp, pluginID), nil
}
// DataSourceInstanceSettings converts the SDK version of a DataSourceInstanceSettings to the protobuf version.
func (t ConvertToProtobuf) DataSourceInstanceSettings(s *DataSourceInstanceSettings) *pluginv2.DataSourceInstanceSettings {
if s == nil {
return nil
}
return &pluginv2.DataSourceInstanceSettings{
Id: s.ID,
Uid: s.UID,
Name: s.Name,
Url: s.URL,
User: s.User,
Database: s.Database,
BasicAuthEnabled: s.BasicAuthEnabled,
BasicAuthUser: s.BasicAuthUser,
JsonData: s.JSONData,
DecryptedSecureJsonData: s.DecryptedSecureJSONData,
LastUpdatedMS: s.Updated.UnixNano() / int64(time.Millisecond),
ApiVersion: s.APIVersion,
}
}
// UserAgent converts the SDK version of a useragent.UserAgent to the protobuf version.
func (t ConvertToProtobuf) UserAgent(ua *useragent.UserAgent) string {
if ua == nil {
return ""
}
return ua.String()
}
// PluginContext converts the SDK version of a PluginContext to the protobuf version.
func (t ConvertToProtobuf) PluginContext(pluginCtx PluginContext) *pluginv2.PluginContext {
return &pluginv2.PluginContext{
OrgId: pluginCtx.OrgID,
PluginId: pluginCtx.PluginID,
PluginVersion: pluginCtx.PluginVersion,
ApiVersion: pluginCtx.APIVersion,
User: t.User(pluginCtx.User),
AppInstanceSettings: t.AppInstanceSettings(pluginCtx.AppInstanceSettings),
DataSourceInstanceSettings: t.DataSourceInstanceSettings(pluginCtx.DataSourceInstanceSettings),
GrafanaConfig: t.GrafanaConfig(pluginCtx.GrafanaConfig),
UserAgent: t.UserAgent(pluginCtx.UserAgent),
}
}
// TimeRange converts the SDK version of a TimeRange to the protobuf version.
func (t ConvertToProtobuf) TimeRange(tr TimeRange) *pluginv2.TimeRange {
return &pluginv2.TimeRange{
FromEpochMS: tr.From.UnixNano() / int64(time.Millisecond),
ToEpochMS: tr.To.UnixNano() / int64(time.Millisecond),
}
}
// HealthStatus converts the SDK version of a HealthStatus to the protobuf version.
func (t ConvertToProtobuf) HealthStatus(status HealthStatus) pluginv2.CheckHealthResponse_HealthStatus {
switch status {
case HealthStatusUnknown:
return pluginv2.CheckHealthResponse_UNKNOWN
case HealthStatusOk:
return pluginv2.CheckHealthResponse_OK
case HealthStatusError:
return pluginv2.CheckHealthResponse_ERROR
}
panic("unsupported protobuf health status type in sdk")
}
// CheckHealthResponse converts the SDK version of a CheckHealthResponse to the protobuf version.
func (t ConvertToProtobuf) CheckHealthResponse(res *CheckHealthResult) *pluginv2.CheckHealthResponse {
return &pluginv2.CheckHealthResponse{
Status: t.HealthStatus(res.Status),
Message: res.Message,
JsonDetails: res.JSONDetails,
}
}
// DataQuery converts the SDK version of a DataQuery to the protobuf version.
func (t ConvertToProtobuf) DataQuery(q DataQuery) *pluginv2.DataQuery {
return &pluginv2.DataQuery{
RefId: q.RefID,
QueryType: q.QueryType,
MaxDataPoints: q.MaxDataPoints,
IntervalMS: q.Interval.Milliseconds(),
TimeRange: t.TimeRange(q.TimeRange),
Json: q.JSON,
}
}
// QueryDataRequest converts the SDK version of a QueryDataRequest to the protobuf version.
func (t ConvertToProtobuf) QueryDataRequest(req *QueryDataRequest) *pluginv2.QueryDataRequest {
queries := make([]*pluginv2.DataQuery, len(req.Queries))
for i, q := range req.Queries {
queries[i] = t.DataQuery(q)
}
return &pluginv2.QueryDataRequest{
PluginContext: t.PluginContext(req.PluginContext),
Headers: req.Headers,
Queries: queries,
}
}
// QueryDataResponse converts the SDK version of a QueryDataResponse to the protobuf version.
// It will set the RefID on the frames to the RefID key in Responses if a Frame's
// RefId property is an empty string.
func (t ConvertToProtobuf) QueryDataResponse(res *QueryDataResponse) (*pluginv2.QueryDataResponse, error) {
pQDR := &pluginv2.QueryDataResponse{
Responses: make(map[string]*pluginv2.DataResponse, len(res.Responses)),
}
for refID, dr := range res.Responses {
for _, f := range dr.Frames {
if f == nil {
return nil, errors.New("frame can not be nil")
}
if f.RefID == "" {
f.RefID = refID
}
}
encodedFrames, err := dr.Frames.MarshalArrow()
if err != nil {
return nil, err
}
pDR := pluginv2.DataResponse{
Frames: encodedFrames,
}
status := dr.Status
if dr.Error != nil {
pDR.Error = dr.Error.Error()
if !status.IsValid() {
status = statusFromError(dr.Error)
}
}
if status.IsValid() {
pDR.Status = int32(status)
} else if status == 0 {
pDR.Status = int32(StatusOK)
}
pDR.ErrorSource = string(dr.ErrorSource)
pQDR.Responses[refID] = &pDR
}
return pQDR, nil
}
// CallResourceResponse converts the SDK version of a CallResourceResponse to the protobuf version.
func (t ConvertToProtobuf) CallResourceResponse(resp *CallResourceResponse) *pluginv2.CallResourceResponse {
headers := map[string]*pluginv2.StringList{}
for key, values := range resp.Headers {
headers[key] = &pluginv2.StringList{Values: values}
}
return &pluginv2.CallResourceResponse{
Headers: headers,
Code: int32(resp.Status),
Body: resp.Body,
}
}
// CallResourceRequest converts the SDK version of a CallResourceRequest to the protobuf version.
func (t ConvertToProtobuf) CallResourceRequest(req *CallResourceRequest) *pluginv2.CallResourceRequest {
protoReq := &pluginv2.CallResourceRequest{
PluginContext: t.PluginContext(req.PluginContext),
Path: req.Path,
Method: req.Method,
Url: req.URL,
Body: req.Body,
}
if req.Headers == nil {
return protoReq
}
protoReq.Headers = make(map[string]*pluginv2.StringList, len(protoReq.Headers))
for k, values := range req.Headers {
protoReq.Headers[k] = &pluginv2.StringList{Values: values}
}
return protoReq
}
// RunStreamRequest ...
func (t ConvertToProtobuf) RunStreamRequest(req *RunStreamRequest) *pluginv2.RunStreamRequest {
protoReq := &pluginv2.RunStreamRequest{
PluginContext: t.PluginContext(req.PluginContext),
Path: req.Path,
Data: req.Data,
}
return protoReq
}
// SubscribeStreamRequest ...
func (t ConvertToProtobuf) SubscribeStreamRequest(req *SubscribeStreamRequest) *pluginv2.SubscribeStreamRequest {
return &pluginv2.SubscribeStreamRequest{
PluginContext: t.PluginContext(req.PluginContext),
Path: req.Path,
Data: req.Data,
}
}
// SubscribeStreamResponse ...
func (t ConvertToProtobuf) SubscribeStreamResponse(req *SubscribeStreamResponse) *pluginv2.SubscribeStreamResponse {
resp := &pluginv2.SubscribeStreamResponse{
Status: pluginv2.SubscribeStreamResponse_Status(req.Status),
}
if req.InitialData != nil {
resp.Data = req.InitialData.data
}
return resp
}
// PublishStreamRequest ...
func (t ConvertToProtobuf) PublishStreamRequest(req *PublishStreamRequest) *pluginv2.PublishStreamRequest {
return &pluginv2.PublishStreamRequest{
PluginContext: t.PluginContext(req.PluginContext),
Path: req.Path,
Data: req.Data,
}
}
// PublishStreamResponse ...
func (t ConvertToProtobuf) PublishStreamResponse(req *PublishStreamResponse) *pluginv2.PublishStreamResponse {
return &pluginv2.PublishStreamResponse{
Status: pluginv2.PublishStreamResponse_Status(req.Status),
Data: req.Data,
}
}
// StreamPacket ...
func (t ConvertToProtobuf) StreamPacket(p *StreamPacket) *pluginv2.StreamPacket {
protoReq := &pluginv2.StreamPacket{
Data: p.Data,
}
return protoReq
}
// CollectMetricsRequest converts the SDK version of a CollectMetricsRequest to the protobuf version.
func (t ConvertToProtobuf) CollectMetricsRequest(req *CollectMetricsRequest) *pluginv2.CollectMetricsRequest {
return &pluginv2.CollectMetricsRequest{
PluginContext: t.PluginContext(req.PluginContext),
}
}
// CollectMetricsResult converts the SDK version of a CollectMetricsResult to the protobuf version.
func (t ConvertToProtobuf) CollectMetricsResult(res *CollectMetricsResult) *pluginv2.CollectMetricsResponse {
return &pluginv2.CollectMetricsResponse{
Metrics: &pluginv2.CollectMetricsResponse_Payload{
Prometheus: res.PrometheusMetrics,
},
}
}
// StatusResult converts the SDK version of a StatusResult to the protobuf version.
func (t ConvertToProtobuf) StatusResult(s *StatusResult) *pluginv2.StatusResult {
if s == nil {
return nil
}
return &pluginv2.StatusResult{
Status: s.Status,
Message: s.Message,
Reason: s.Reason,
Code: s.Code,
}
}
// GroupVersionKind converts the SDK version of a GroupVersionKind to the protobuf version.
func (t ConvertToProtobuf) GroupVersionKind(req *GroupVersionKind) *pluginv2.GroupVersionKind {
return &pluginv2.GroupVersionKind{
Group: req.Group,
Version: req.Version,
Kind: req.Kind,
}
}
// GroupVersion converts the SDK version of a GroupVersion to the protobuf version.
func (t ConvertToProtobuf) GroupVersion(req *GroupVersion) *pluginv2.GroupVersion {
return &pluginv2.GroupVersion{
Group: req.Group,
Version: req.Version,
}
}
// RawObject converts the SDK version of a RawObject to the protobuf version.
func (t ConvertToProtobuf) RawObject(req RawObject) *pluginv2.RawObject {
return &pluginv2.RawObject{
Raw: req.Raw,
ContentType: req.ContentType,
}
}
// RawObjects converts the SDK version of a RawObject to the protobuf version.
func (t ConvertToProtobuf) RawObjects(req []RawObject) []*pluginv2.RawObject {
objects := make([]*pluginv2.RawObject, len(req))
for i, o := range req {
objects[i] = t.RawObject(o)
}
return objects
}
// AdmissionRequest converts the SDK version of a AdmissionRequest to the protobuf version.
func (t ConvertToProtobuf) AdmissionRequest(req *AdmissionRequest) *pluginv2.AdmissionRequest {
return &pluginv2.AdmissionRequest{
PluginContext: t.PluginContext(req.PluginContext),
Operation: pluginv2.AdmissionRequest_Operation(req.Operation),
Kind: t.GroupVersionKind(&req.Kind),
ObjectBytes: req.ObjectBytes,
OldObjectBytes: req.OldObjectBytes,
}
}
// ConversionRequest converts the SDK version of a ConversionRequest to the protobuf version.
func (t ConvertToProtobuf) ConversionRequest(req *ConversionRequest) *pluginv2.ConversionRequest {
return &pluginv2.ConversionRequest{
PluginContext: t.PluginContext(req.PluginContext),
Uid: req.UID,
TargetVersion: t.GroupVersion(&req.TargetVersion),
Objects: t.RawObjects(req.Objects),
}
}
// MutationResponse converts the SDK version of a MutationResponse to the protobuf version.
func (t ConvertToProtobuf) MutationResponse(rsp *MutationResponse) *pluginv2.MutationResponse {
return &pluginv2.MutationResponse{
Allowed: rsp.Allowed,
Result: t.StatusResult(rsp.Result),
Warnings: rsp.Warnings,
ObjectBytes: rsp.ObjectBytes,
}
}
// ValidationResponse converts the SDK version of a ValidationResponse to the protobuf version.
func (t ConvertToProtobuf) ValidationResponse(rsp *ValidationResponse) *pluginv2.ValidationResponse {
return &pluginv2.ValidationResponse{
Allowed: rsp.Allowed,
Result: t.StatusResult(rsp.Result),
Warnings: rsp.Warnings,
}
}
// ConversionResponse converts the SDK version of a ConversionResponse to the protobuf version.
func (t ConvertToProtobuf) ConversionResponse(rsp *ConversionResponse) *pluginv2.ConversionResponse {
return &pluginv2.ConversionResponse{
Uid: rsp.UID,
Result: t.StatusResult(rsp.Result),
Objects: t.RawObjects(rsp.Objects),
}
}
// GrafanaConfig converts the SDK version of a GrafanaCfg to the protobuf version.
func (t ConvertToProtobuf) GrafanaConfig(cfg *GrafanaCfg) map[string]string {
if cfg == nil {
return map[string]string{}
}
return cfg.config
}