2015-07-29 23:33:25 +00: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 14:07:42 +00:00
|
|
|
"fmt"
|
2015-07-29 23:33:25 +00:00
|
|
|
"net/http"
|
2015-07-30 18:31:35 +00:00
|
|
|
"sort"
|
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 14:07:42 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2015-07-29 23:33:25 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 18:23:09 +00:00
|
|
|
// coordinateDisabled handles all the endpoints when coordinates are not enabled,
|
|
|
|
// returning an error message.
|
|
|
|
func coordinateDisabled(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-08-23 19:19:11 +00: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 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Coordinate support disabled")
|
2015-07-30 18:23:09 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:31:35 +00:00
|
|
|
// sorter wraps a coordinate list and implements the sort.Interface to sort by
|
|
|
|
// node name.
|
|
|
|
type sorter struct {
|
2015-10-23 22:19:14 +00:00
|
|
|
coordinates structs.Coordinates
|
2015-07-30 18:31:35 +00: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 23:33:25 +00:00
|
|
|
// CoordinateDatacenters returns the WAN nodes in each datacenter, along with
|
|
|
|
// raw network coordinates.
|
|
|
|
func (s *HTTPServer) CoordinateDatacenters(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
var out []structs.DatacenterMap
|
|
|
|
if err := s.agent.RPC("Coordinate.ListDatacenters", struct{}{}, &out); err != nil {
|
2015-07-30 18:31:35 +00:00
|
|
|
for i := range out {
|
|
|
|
sort.Sort(&sorter{out[i].Coordinates})
|
|
|
|
}
|
2015-07-29 23:33:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00: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 18:42:22 +00:00
|
|
|
for i := range out {
|
2015-11-15 05:05:37 +00:00
|
|
|
if out[i].Coordinates == nil {
|
|
|
|
out[i].Coordinates = make(structs.Coordinates, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
out = make([]structs.DatacenterMap, 0)
|
|
|
|
}
|
2015-07-29 23:33:25 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CoordinateNodes returns the LAN nodes in the given datacenter, along with
|
|
|
|
// raw network coordinates.
|
|
|
|
func (s *HTTPServer) CoordinateNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
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 18:31:35 +00:00
|
|
|
sort.Sort(&sorter{out.Coordinates})
|
2015-07-29 23:33:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil.
|
|
|
|
if out.Coordinates == nil {
|
|
|
|
out.Coordinates = make(structs.Coordinates, 0)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
|
|
|
|
// Filter by segment if applicable
|
|
|
|
if v, ok := req.URL.Query()["segment"]; ok && len(v) > 0 {
|
|
|
|
segment := v[0]
|
|
|
|
filtered := make(structs.Coordinates, 0)
|
|
|
|
for _, coord := range out.Coordinates {
|
|
|
|
if coord.Segment == segment {
|
|
|
|
filtered = append(filtered, coord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.Coordinates = filtered
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
return out.Coordinates, nil
|
|
|
|
}
|