forked from haproxytech/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
federation_state_endpoint.go
91 lines (74 loc) · 2.43 KB
/
federation_state_endpoint.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
package agent
import (
"net/http"
"strings"
"github.com/hashicorp/consul/agent/structs"
)
// GET /v1/internal/federation-state/<datacenter>
func (s *HTTPHandlers) FederationStateGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
datacenterName := strings.TrimPrefix(req.URL.Path, "/v1/internal/federation-state/")
if datacenterName == "" {
return nil, BadRequestError{Reason: "Missing datacenter name"}
}
args := structs.FederationStateQuery{
Datacenter: datacenterName,
}
if done := s.parse(resp, req, &args.TargetDatacenter, &args.QueryOptions); done {
return nil, nil
}
var out structs.FederationStateResponse
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC("FederationState.Get", &args, &out); err != nil {
return nil, err
}
if out.State == nil {
resp.WriteHeader(http.StatusNotFound)
return nil, nil
}
return out, nil
}
// GET /v1/internal/federation-states
func (s *HTTPHandlers) FederationStateList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.DCSpecificRequest
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
if args.Datacenter == "" {
args.Datacenter = s.agent.config.Datacenter
}
var out structs.IndexedFederationStates
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC("FederationState.List", &args, &out); err != nil {
return nil, err
}
// make sure we return an array and not nil
if out.States == nil {
out.States = make(structs.FederationStates, 0)
}
return out.States, nil
}
// GET /v1/internal/federation-states/mesh-gateways
func (s *HTTPHandlers) FederationStateListMeshGateways(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.DCSpecificRequest
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
if args.Datacenter == "" {
args.Datacenter = s.agent.config.Datacenter
}
var out structs.DatacenterIndexedCheckServiceNodes
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC("FederationState.ListMeshGateways", &args, &out); err != nil {
return nil, err
}
// make sure we return a arrays and not nils
if out.DatacenterNodes == nil {
out.DatacenterNodes = make(map[string]structs.CheckServiceNodes)
}
for dc, nodes := range out.DatacenterNodes {
if nodes == nil {
out.DatacenterNodes[dc] = make(structs.CheckServiceNodes, 0)
}
}
return out.DatacenterNodes, nil
}