forked from ligato/vpp-agent
/
scheduler.go
74 lines (63 loc) · 1.92 KB
/
scheduler.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
package client
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/ligato/vpp-agent/api/types"
"github.com/ligato/vpp-agent/plugins/kvscheduler/api"
)
func (c *Client) SchedulerDump(ctx context.Context, opts types.SchedulerDumpOptions) ([]api.KVWithMetadata, error) {
type ProtoWithName struct {
ProtoMsgName string
ProtoMsgData string
}
type KVWithMetadata struct {
api.KVWithMetadata
Value ProtoWithName
}
var kvdump []KVWithMetadata
query := url.Values{}
query.Set("key-prefix", opts.KeyPrefix)
query.Set("view", opts.View)
resp, err := c.get(ctx, "/scheduler/dump", query, nil)
if err != nil {
return nil, err
}
if err := json.NewDecoder(resp.body).Decode(&kvdump); err != nil {
return nil, fmt.Errorf("decoding reply failed: %v", err)
}
var dump []api.KVWithMetadata
for _, kvd := range kvdump {
d := kvd.KVWithMetadata
if kvd.Value.ProtoMsgName == "" {
return nil, fmt.Errorf("empty proto message name for key %s", d.Key)
}
valueType := proto.MessageType(kvd.Value.ProtoMsgName)
if valueType == nil {
return nil, fmt.Errorf("unknown proto message defined for key %s", d.Key)
}
d.Value = reflect.New(valueType.Elem()).Interface().(proto.Message)
if err = jsonpb.UnmarshalString(kvd.Value.ProtoMsgData, d.Value); err != nil {
return nil, fmt.Errorf("decoding reply failed: %v", err)
}
dump = append(dump, d)
}
return dump, nil
}
func (c *Client) SchedulerValues(ctx context.Context, opts types.SchedulerValuesOptions) ([]*api.BaseValueStatus, error) {
query := url.Values{}
query.Set("key-prefix", opts.KeyPrefix)
resp, err := c.get(ctx, "/scheduler/status", query, nil)
if err != nil {
return nil, err
}
var status []*api.BaseValueStatus
if err := json.NewDecoder(resp.body).Decode(&status); err != nil {
return nil, fmt.Errorf("decoding reply failed: %v", err)
}
return status, nil
}