Adds coordinate of agent to self endpoint.

This commit is contained in:
James Phillips 2015-07-30 12:02:37 -07:00
parent 6289764ea2
commit f9da231a1c
3 changed files with 37 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package agent
import (
"fmt"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
"net/http"
"strconv"
@ -11,12 +12,23 @@ import (
type AgentSelf struct {
Config *Config
Coord *coordinate.Coordinate
Member serf.Member
}
func (s *HTTPServer) AgentSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var coord *coordinate.Coordinate
if !s.agent.config.DisableCoordinates {
var err error
coord, err = s.agent.server.GetLANCoordinate()
if err != nil {
return nil, err
}
}
return AgentSelf{
Config: s.agent.config,
Coord: coord,
Member: s.agent.LocalMember(),
}, nil
}

View File

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
@ -81,7 +82,7 @@ func TestHTTPAgentSelf(t *testing.T) {
obj, err := srv.AgentSelf(nil, req)
if err != nil {
t.Fatalf("Err: %v", err)
t.Fatalf("err: %v", err)
}
val := obj.(AgentSelf)
@ -92,6 +93,24 @@ func TestHTTPAgentSelf(t *testing.T) {
if int(val.Config.Ports.SerfLan) != srv.agent.config.Ports.SerfLan {
t.Fatalf("incorrect port: %v", obj)
}
c, err := srv.agent.server.GetLANCoordinate()
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(c, val.Coord) {
t.Fatalf("coordinates are not equal: %v != %v", c, val.Coord)
}
srv.agent.config.DisableCoordinates = true
obj, err = srv.AgentSelf(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
}
val = obj.(AgentSelf)
if val.Coord != nil {
t.Fatalf("should have been nil: %v", val.Coord)
}
}
func TestHTTPAgentMembers(t *testing.T) {

View File

@ -163,6 +163,11 @@ It returns a JSON body like this:
"EnableSyslog": false,
"RejoinAfterLeave": false
},
"Coord": {
"Adjustment": 0,
"Error": 1.5,
"Vec": [0,0,0,0,0,0,0,0]
},
"Member": {
"Name": "foobar",
"Addr": "10.1.10.12",