-
Notifications
You must be signed in to change notification settings - Fork 24
/
discovery.go
74 lines (66 loc) · 1.98 KB
/
discovery.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 utils
import (
"context"
"fmt"
"os"
"strings"
"google.golang.org/protobuf/encoding/protojson"
cmdv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/cmd/v1"
)
// ParseEntities parses the entities from the cluster.
func ParseEntities(client IntrospectionClient, findBy, agentGroup string) error {
toPrint := ""
if findBy != "" {
find := strings.Split(findBy, "=")
if len(find) != 2 {
return fmt.Errorf("invalid findBy argument: %s", findBy)
}
switch find[0] {
case "name":
if find[1] == "" {
return fmt.Errorf("must provide a name to find by: %s", findBy)
}
resp, err := client.ListDiscoveryEntity(context.Background(), &cmdv1.ListDiscoveryEntityRequest{By: &cmdv1.ListDiscoveryEntityRequest_Name{Name: find[1]}})
if err != nil {
return err
}
entity, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(resp.Entity)
if err != nil {
return err
}
toPrint = string(entity)
case "ip":
if find[1] == "" {
return fmt.Errorf("invalid findBy argument: %s", findBy)
}
resp, err := client.ListDiscoveryEntity(context.Background(), &cmdv1.ListDiscoveryEntityRequest{By: &cmdv1.ListDiscoveryEntityRequest_IpAddress{IpAddress: find[1]}})
if err != nil {
return err
}
entity, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(resp.Entity)
if err != nil {
return err
}
toPrint = string(entity)
default:
return fmt.Errorf("invalid findBy argument: %s", findBy)
}
} else {
resp, err := client.ListDiscoveryEntities(context.Background(), &cmdv1.ListDiscoveryEntitiesRequest{
AgentGroup: agentGroup,
})
if err != nil {
return err
}
if resp.ErrorsCount != 0 {
fmt.Fprintf(os.Stderr, "Could not get answer from %d agents", resp.ErrorsCount)
}
entities, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(resp.Entities)
if err != nil {
return err
}
toPrint = string(entities)
}
fmt.Printf("%s\n", toPrint)
return nil
}