/
qdmanage.go
89 lines (77 loc) · 2.97 KB
/
qdmanage.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
package qdrmanagement
import (
"encoding/json"
"github.com/fgiorgetti/qpid-dispatch-go-tests/pkg/framework"
entities2 "github.com/fgiorgetti/qpid-dispatch-go-tests/pkg/framework/qdrmanagement/entities"
"reflect"
"time"
)
const (
timeout time.Duration = 60 * time.Second
)
var (
queryCommand = []string{"qdmanage", "query", "--type"}
)
// QdmanageQuery executes a "qdmanage query" command on the given pod
// to retrieve all entities for the given <entity> type.
func QdmanageQuery(ctxData framework.ContextData, pod string, entity string) (string, error) {
command := append(queryCommand, entity)
kubeExec := framework.NewKubectlExecCommand(ctxData, pod, timeout, command...)
return kubeExec.Exec()
}
// QdmanageQueryConnections use qdmanage to query existing connections on the given pod
func QdmanageQueryConnections(ctxData framework.ContextData, pod string) ([]entities2.Connection, error) {
return QdmanageQueryConnectionsFilter(ctxData, pod, nil)
}
// QdmanageQueryConnectionsFilter use qdmanage to query existing connections on the given pod
// filtering entities using the provided filter function (if one is given)
func QdmanageQueryConnectionsFilter(ctxData framework.ContextData, pod string, filter func(entity interface{}) bool) ([]entities2.Connection, error) {
jsonString, err := QdmanageQuery(ctxData, pod, entities2.Connection{}.GetEntityId())
var connections []entities2.Connection
if err == nil {
err = json.Unmarshal([]byte(jsonString), &connections)
filtered := FilterEntities(connections, filter)
connections = nil
for _, v := range filtered {
connections = append(connections, v.(entities2.Connection))
}
}
return connections, err
}
// QdmanageQueryNodes use qdmanage to query existing nodes on the given pod
func QdmanageQueryNodes(ctxData framework.ContextData, pod string) ([]entities2.Node, error) {
return QdmanageQueryNodesFilter(ctxData, pod, nil)
}
// QdmanageQueryNodesFilter use qdmanage to query existing nodes on the given pod
// filtering entities using the provided filter function (if one given)
func QdmanageQueryNodesFilter(ctxData framework.ContextData, pod string, filter func(entity interface{}) bool) ([]entities2.Node, error) {
jsonString, err := QdmanageQuery(ctxData, pod, entities2.Node{}.GetEntityId())
var nodes []entities2.Node
if err == nil {
err = json.Unmarshal([]byte(jsonString), &nodes)
filtered := FilterEntities(nodes, filter)
nodes = nil
for _, v := range filtered {
nodes = append(nodes, v.(entities2.Node))
}
}
return nodes, err
}
// filter is an internal method to be invoked by specific Query<Entity> methods
// so all methods can reuse the same code for filtering entities
func FilterEntities(i interface{}, fn func(i interface{}) bool) []interface{} {
s := reflect.ValueOf(i)
if s.Kind() != reflect.Slice {
panic("Expecting a slice")
}
var ret []interface{}
ri := 0
for j := 0; j < s.Len(); j++ {
ii := s.Index(j).Interface()
if fn == nil || fn(ii) {
ret = append(ret, ii)
ri++
}
}
return ret
}