2015-07-29 16:33:25 -07:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 07:07:42 -07:00
|
|
|
"fmt"
|
2015-07-29 16:33:25 -07:00
|
|
|
"net/http"
|
2015-07-30 11:31:35 -07:00
|
|
|
"sort"
|
2017-10-26 14:24:42 +02:00
|
|
|
"strings"
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 07:07:42 -07:00
|
|
|
|
2017-07-06 12:34:00 +02:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2015-07-29 16:33:25 -07:00
|
|
|
)
|
|
|
|
|
2017-11-28 13:57:45 -08:00
|
|
|
// checkCoordinateDisabled will return a standard response if coordinates are
|
|
|
|
// disabled. This returns true if they are disabled and we should not continue.
|
2020-09-04 14:42:15 -04:00
|
|
|
func (s *HTTPHandlers) checkCoordinateDisabled(resp http.ResponseWriter, req *http.Request) bool {
|
2017-11-28 13:57:45 -08:00
|
|
|
if !s.agent.config.DisableCoordinates {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-23 21:19:11 +02:00
|
|
|
resp.WriteHeader(http.StatusUnauthorized)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 07:07:42 -07:00
|
|
|
fmt.Fprint(resp, "Coordinate support disabled")
|
2017-11-28 13:57:45 -08:00
|
|
|
return true
|
2015-07-30 11:23:09 -07:00
|
|
|
}
|
|
|
|
|
2015-07-30 11:31:35 -07:00
|
|
|
// sorter wraps a coordinate list and implements the sort.Interface to sort by
|
|
|
|
// node name.
|
|
|
|
type sorter struct {
|
2015-10-23 15:19:14 -07:00
|
|
|
coordinates structs.Coordinates
|
2015-07-30 11:31:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// See sort.Interface.
|
|
|
|
func (s *sorter) Len() int {
|
|
|
|
return len(s.coordinates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See sort.Interface.
|
|
|
|
func (s *sorter) Swap(i, j int) {
|
|
|
|
s.coordinates[i], s.coordinates[j] = s.coordinates[j], s.coordinates[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// See sort.Interface.
|
|
|
|
func (s *sorter) Less(i, j int) bool {
|
|
|
|
return s.coordinates[i].Node < s.coordinates[j].Node
|
|
|
|
}
|
|
|
|
|
2015-07-29 16:33:25 -07:00
|
|
|
// CoordinateDatacenters returns the WAN nodes in each datacenter, along with
|
|
|
|
// raw network coordinates.
|
2020-09-04 14:42:15 -04:00
|
|
|
func (s *HTTPHandlers) CoordinateDatacenters(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 13:57:45 -08:00
|
|
|
if s.checkCoordinateDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-09-26 08:11:19 +02:00
|
|
|
|
2015-07-29 16:33:25 -07:00
|
|
|
var out []structs.DatacenterMap
|
|
|
|
if err := s.agent.RPC("Coordinate.ListDatacenters", struct{}{}, &out); err != nil {
|
2015-07-30 11:31:35 -07:00
|
|
|
for i := range out {
|
|
|
|
sort.Sort(&sorter{out[i].Coordinates})
|
|
|
|
}
|
2015-07-29 16:33:25 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-14 21:05:37 -08:00
|
|
|
|
|
|
|
// Use empty list instead of nil (these aren't really possible because
|
|
|
|
// Serf will give back a default coordinate and there's always one DC,
|
|
|
|
// but it's better to be explicit about what we want here).
|
2017-04-20 11:42:22 -07:00
|
|
|
for i := range out {
|
2015-11-14 21:05:37 -08:00
|
|
|
if out[i].Coordinates == nil {
|
|
|
|
out[i].Coordinates = make(structs.Coordinates, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
out = make([]structs.DatacenterMap, 0)
|
|
|
|
}
|
2015-07-29 16:33:25 -07:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CoordinateNodes returns the LAN nodes in the given datacenter, along with
|
|
|
|
// raw network coordinates.
|
2020-09-04 14:42:15 -04:00
|
|
|
func (s *HTTPHandlers) CoordinateNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 13:57:45 -08:00
|
|
|
if s.checkCoordinateDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-09-26 08:11:19 +02:00
|
|
|
|
2015-07-29 16:33:25 -07:00
|
|
|
args := structs.DCSpecificRequest{}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.IndexedCoordinates
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("Coordinate.ListNodes", &args, &out); err != nil {
|
2015-07-30 11:31:35 -07:00
|
|
|
sort.Sort(&sorter{out.Coordinates})
|
2015-07-29 16:33:25 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-14 21:05:37 -08:00
|
|
|
|
2017-10-31 15:08:14 -07:00
|
|
|
return filterCoordinates(req, out.Coordinates), nil
|
2017-10-26 14:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CoordinateNode returns the LAN node in the given datacenter, along with
|
|
|
|
// raw network coordinates.
|
2020-09-04 14:42:15 -04:00
|
|
|
func (s *HTTPHandlers) CoordinateNode(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 13:57:45 -08:00
|
|
|
if s.checkCoordinateDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-08-14 07:36:07 -07:00
|
|
|
|
2017-10-26 19:16:40 -07:00
|
|
|
node := strings.TrimPrefix(req.URL.Path, "/v1/coordinate/node/")
|
|
|
|
args := structs.NodeSpecificRequest{Node: node}
|
2017-10-26 14:24:42 +02:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.IndexedCoordinates
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2017-10-26 19:16:40 -07:00
|
|
|
if err := s.agent.RPC("Coordinate.Node", &args, &out); err != nil {
|
2017-10-26 14:24:42 +02:00
|
|
|
return nil, err
|
2017-08-14 07:36:07 -07:00
|
|
|
}
|
|
|
|
|
2017-10-31 15:08:14 -07:00
|
|
|
result := filterCoordinates(req, out.Coordinates)
|
|
|
|
if len(result) == 0 {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2017-10-26 14:24:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 15:08:14 -07:00
|
|
|
func filterCoordinates(req *http.Request, in structs.Coordinates) structs.Coordinates {
|
2017-10-26 14:24:42 +02:00
|
|
|
out := structs.Coordinates{}
|
|
|
|
|
|
|
|
if in == nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
segment := ""
|
|
|
|
v, filterBySegment := req.URL.Query()["segment"]
|
|
|
|
if filterBySegment && len(v) > 0 {
|
|
|
|
segment = v[0]
|
2017-08-14 07:36:07 -07:00
|
|
|
}
|
|
|
|
|
2017-10-26 14:24:42 +02:00
|
|
|
for _, c := range in {
|
|
|
|
if filterBySegment && c.Segment != segment {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, c)
|
|
|
|
}
|
|
|
|
return out
|
2015-07-29 16:33:25 -07:00
|
|
|
}
|
2017-10-23 17:44:50 -07:00
|
|
|
|
|
|
|
// CoordinateUpdate inserts or updates the LAN coordinate of a node.
|
2020-09-04 14:42:15 -04:00
|
|
|
func (s *HTTPHandlers) CoordinateUpdate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 13:57:45 -08:00
|
|
|
if s.checkCoordinateDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-10-23 17:44:50 -07:00
|
|
|
|
|
|
|
args := structs.CoordinateUpdateRequest{}
|
2019-10-29 11:13:36 -07:00
|
|
|
if err := decodeBody(req.Body, &args); err != nil {
|
2017-10-23 17:44:50 -07:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(resp, "Request decode failed: %v", err)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2018-02-15 11:58:02 -08:00
|
|
|
s.parseToken(req, &args.Token)
|
2017-10-23 17:44:50 -07:00
|
|
|
|
|
|
|
var reply struct{}
|
|
|
|
if err := s.agent.RPC("Coordinate.Update", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|