forked from outbrain/consult
/
query.go
149 lines (129 loc) · 3.72 KB
/
query.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
package main
import (
"github.com/hashicorp/consul/api"
"github.com/outbrain/consult/misc"
"github.com/wushilin/stream"
"gopkg.in/alecthomas/kingpin.v2"
)
type QueryCommand struct {
Command
misc.IQuery
service string
tags []string
tagsMerge string
}
type queryCommand struct {
QueryCommand
}
func queryRegisterCli(app *kingpin.Application, opts *appOpts) {
q := &queryCommand{}
q.IQuery = q
q.opts = opts
queryCmd := app.Command("query", "Query Consul catalog").Action(q.run)
queryCmd.Flag("tag", "Consul tag").Short('t').StringsVar(&q.tags)
queryCmd.Flag("service", "Consul service").Required().Short('s').StringVar(&q.service)
q.registerCli(queryCmd)
}
func (q *queryCommand) run(c *kingpin.ParseContext) error {
if results, err := q.queryServicesGeneric(); err != nil {
return err
} else {
short := make([]string, 0)
long := make([]string, 2)
long[0] = misc.StructHeaderLine(api.CatalogService{})
long[1] = ""
for dc, res := range results {
for _, service := range res {
short = append(short, dc+"\t"+service.Node)
long = append(long, dc+"\t"+misc.StructToString(service))
}
}
q.Output(results, long, short)
return nil
}
}
func (q *QueryCommand) registerCli(cmd *kingpin.CmdClause) {
cmd.Flag("tag", "Consul tag").Short('t').StringsVar(&q.tags)
cmd.Flag("service", "Consul service").Required().Short('s').StringVar(&q.service)
cmd.Flag("tags-mode", "Find nodes with *all* or *any* of the tags").Short('m').Default("all").EnumVar(&q.tagsMerge, "all", "any")
}
func (q *QueryCommand) queryServicesGeneric() (services map[string][]*api.CatalogService, err_ error) {
defer func() {
if r := recover(); r != nil {
err_ = r.(error)
}
}()
mergeFunc := intersectionMerge
if q.tagsMerge == "any" {
mergeFunc = unionMerge
}
results_by_dc, err := q.QueryWithClients(func(client *api.Client) interface{} {
return q.queryMulti(client, mergeFunc)
})
if err != nil {
return nil, err
}
typed_results := make(map[string][]*api.CatalogService)
for k, v := range results_by_dc {
typed_results[k] = v.([]*api.CatalogService)
}
return typed_results, nil
}
func (q *QueryCommand) queryMulti(
client *api.Client,
mergeFunc misc.CatalogServicesMerger,
) []*api.CatalogService {
// handle the case of no tags
if len(q.tags) == 0 {
return q.IQuery.Query(client, "")
}
res, ok := (&basePStream{stream.FromArray(q.tags)}).PMap(
func(tag interface{}) interface{} {
return q.IQuery.Query(client, tag.(string)) // use q.IQuery.Query so we can override for testing
}).Reduce(mergeFunc).Value()
if ok {
return res.([]*api.CatalogService)
} else {
// perhaps blow up instead?
return []*api.CatalogService{}
}
}
func (q *QueryCommand) Query(client *api.Client, tag string) []*api.CatalogService {
services, _, err := client.Catalog().Service(q.service, tag, &api.QueryOptions{AllowStale: true, RequireConsistent: false})
if err != nil {
panic(err)
return nil
}
return services
}
type CatalogServiceList []*api.CatalogService
func (c CatalogServiceList) Contains(elmnt *api.CatalogService) bool {
for _, x := range c {
if x.Node == elmnt.Node {
return true
}
}
return false
}
// i really don't care about efficiency. merge every two items with these functions
func unionMerge(a []*api.CatalogService, b []*api.CatalogService) []*api.CatalogService {
res := make(CatalogServiceList, 0, len(a)+len(b))
for _, x := range a {
res = append(res, x)
}
for _, x := range b {
if !res.Contains(x) {
res = append(res, x)
}
}
return res
}
func intersectionMerge(a []*api.CatalogService, b []*api.CatalogService) []*api.CatalogService {
res := make([]*api.CatalogService, 0)
for _, x := range a {
if CatalogServiceList(b).Contains(x) {
res = append(res, x)
}
}
return res
}