api/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
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
package api
import "fmt"
// Node is a Consul-shaped representation of a cluster member.
type Node struct {
Node string
Address string
Meta map[string]string
}
// CatalogService describes a single service instance as exposed by the
// /v1/catalog/service/<name> endpoint.
type CatalogService struct {
Node string
Address string
ServiceID string
ServiceName string
ServiceAddress string
ServicePort int
ServiceTags []string
ServiceMeta map[string]string
}
// CatalogNode bundles a node and the services it owns, returned by
// /v1/catalog/node/<name>.
type CatalogNode struct {
Node *Node
Services map[string]*AgentService
}
// Catalog provides access to the cluster-wide read views over gossipped
// service state.
type Catalog struct {
c *Client
}
// Catalog returns a handle to the catalog endpoints.
func (c *Client) Catalog() *Catalog {
return &Catalog{c: c}
}
// Services returns the cluster-wide service-name -> tag-union view.
func (cat *Catalog) Services() (map[string][]string, error) {
r := cat.c.newRequest("GET", "/v1/catalog/services")
_, resp, err := requireOK(cat.c.doRequest(r))
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
var out map[string][]string
if err := decodeJSONBody(resp.Body, &out); err != nil {
return nil, err
}
return out, nil
}
// Service returns every known instance of the named service.
func (cat *Catalog) Service(name string) ([]*CatalogService, error) {
r := cat.c.newRequest("GET", fmt.Sprintf("/v1/catalog/service/%s", name))
_, resp, err := requireOK(cat.c.doRequest(r))
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
var out []*CatalogService
if err := decodeJSONBody(resp.Body, &out); err != nil {
return nil, err
}
return out, nil
}
// Nodes returns every node serf knows about.
func (cat *Catalog) Nodes() ([]*Node, error) {
r := cat.c.newRequest("GET", "/v1/catalog/nodes")
_, resp, err := requireOK(cat.c.doRequest(r))
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
var out []*Node
if err := decodeJSONBody(resp.Body, &out); err != nil {
return nil, err
}
return out, nil
}
// Node returns the node and the services it owns.
func (cat *Catalog) Node(name string) (*CatalogNode, error) {
r := cat.c.newRequest("GET", fmt.Sprintf("/v1/catalog/node/%s", name))
_, resp, err := requireOK(cat.c.doRequest(r))
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
var out CatalogNode
if err := decodeJSONBody(resp.Body, &out); err != nil {
return nil, err
}
return &out, nil
}