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 ( import (
"fmt" "fmt"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
"net/http" "net/http"
"strconv" "strconv"
@ -11,12 +12,23 @@ import (
type AgentSelf struct { type AgentSelf struct {
Config *Config Config *Config
Coord *coordinate.Coordinate
Member serf.Member Member serf.Member
} }
func (s *HTTPServer) AgentSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 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{ return AgentSelf{
Config: s.agent.config, Config: s.agent.config,
Coord: coord,
Member: s.agent.LocalMember(), Member: s.agent.LocalMember(),
}, nil }, nil
} }

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"reflect"
"testing" "testing"
"time" "time"
@ -81,7 +82,7 @@ func TestHTTPAgentSelf(t *testing.T) {
obj, err := srv.AgentSelf(nil, req) obj, err := srv.AgentSelf(nil, req)
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("err: %v", err)
} }
val := obj.(AgentSelf) val := obj.(AgentSelf)
@ -92,6 +93,24 @@ func TestHTTPAgentSelf(t *testing.T) {
if int(val.Config.Ports.SerfLan) != srv.agent.config.Ports.SerfLan { if int(val.Config.Ports.SerfLan) != srv.agent.config.Ports.SerfLan {
t.Fatalf("incorrect port: %v", obj) 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) { func TestHTTPAgentMembers(t *testing.T) {

View File

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