forked from drnic/consul-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.go
71 lines (59 loc) · 2.16 KB
/
catalog.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
package consuldiscovery
// Catalog is a set of functions to find services information
type Catalog interface {
CatalogServices() CatalogServices
CatalogServiceByName(name string) CatalogServiceByName
}
// catalogServicesResponse maps GET /v1/catalog/services response
// From API response: {"consul":null,"simple_service":["tag1","tag2"]}
type catalogServicesResponse map[string][]string
// CatalogServices contains the available service names and their tags
type CatalogServices []CatalogService
// CatalogService contains a single available service name and its tags
type CatalogService struct {
Name string
Tags []string
}
// CatalogServiceByName contains the nodes composing a service
type CatalogServiceByName []CatalogServiceNode
// CatalogServiceNode describes a single node of a service
// From API response: {"Node":"skailhq.local","Address":"192.168.50.1","ServiceID":"simple_service","ServiceName":"simple_service","ServiceTags":["tag1","tag2"],"ServicePort":6666}
type CatalogServiceNode struct {
Node string
Address string
ServiceID string
ServiceName string
ServiceTags []string
ServicePort uint64
TaggedAddresses map[string]string
NodeMeta map[string]string
ID string
Datacenter string
ServiceAddress string
ServiceTaggedAddresses map[string]ServiceAddress
ServiceMeta map[string]string
Namespace string
Partition string
}
type ServiceAddress struct {
Address string
Port int
}
// CatalogServices returns a list of advertised service names and their tags
func (c *Client) CatalogServices() (result CatalogServices, err error) {
services := catalogServicesResponse{}
if err = c.doGET("catalog/services", &services); err != nil {
return
}
// Convert {"name" => ["tags"]} into []CatalogService
for name, tags := range services {
service := CatalogService{Name: name, Tags: tags}
result = append(result, service)
}
return
}
// CatalogServiceByName returns a list of nodes composing a service
func (c *Client) CatalogServiceByName(name string) (nodes CatalogServiceByName, err error) {
err = c.doGET("catalog/service/"+name, &nodes)
return
}