-
Notifications
You must be signed in to change notification settings - Fork 113
/
lookup.go
73 lines (65 loc) · 1.56 KB
/
lookup.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
package habapi
// ServiceInfoByName searches the passed slice of ServiceInfo structs
// and returns a pointer to the ServiceInfo for the named service.
func ServiceInfoByName(svcs []ServiceInfo, svcName string) (*ServiceInfo, bool) {
for i, svc := range svcs {
if svc.Pkg.Name == svcName {
return &svcs[i], true
}
}
return nil, false
}
// PortForService retrieves the GRPC port for the named service from
// the []ServiceInfo.
func PortForService(svcs []ServiceInfo, svcName string) (int32, bool) {
for _, svc := range svcs {
if svc.Pkg.Name == svcName {
return portFromConfig(svc.Cfg, svcName)
}
}
return 0, false
}
func portFromConfig(cfg map[string]interface{}, name string) (int32, bool) {
switch name {
case "automate-dex":
svcCfgMap, ok := getMap("grpc", cfg)
if !ok {
return 0, false
}
return getInt32("port", svcCfgMap)
case "automate-gateway":
svcCfgMap, ok := getMap("service", cfg)
if !ok {
return 0, false
}
return getInt32("grpc_port", svcCfgMap)
default:
svcCfgMap, ok := getMap("service", cfg)
if !ok {
return 0, false
}
return getInt32("port", svcCfgMap)
}
}
func getMap(key string, data map[string]interface{}) (map[string]interface{}, bool) {
config, ok := data[key]
if !ok {
return nil, false
}
aMap, ok := config.(map[string]interface{})
if !ok {
return nil, false
}
return aMap, true
}
func getInt32(key string, data map[string]interface{}) (int32, bool) {
port, ok := data[key]
if !ok {
return 0, false
}
flt, ok := port.(float64)
if !ok {
return 0, false
}
return int32(flt), true
}