From 134f66e3d48a46b952f57b63b70c097d9dd0670b Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Fri, 1 Jul 2016 10:04:58 -0700 Subject: [PATCH] agent: parseSource still subs for _agent --- command/agent/http.go | 8 +++++++- command/agent/http_test.go | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index 0cfbafbe1..92247ef2c 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -531,7 +531,13 @@ func (s *HTTPServer) parseToken(req *http.Request, token *string) { // DC in the request, if given, or else the agent's DC. func (s *HTTPServer) parseSource(req *http.Request, source *structs.QuerySource) { s.parseDC(req, &source.Datacenter) - source.Node = req.URL.Query().Get("near") + if node := req.URL.Query().Get("near"); node != "" { + if node == "_agent" { + source.Node = s.agent.config.NodeName + } else { + source.Node = node + } + } } // parse is a convenience method for endpoints that need diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 231fd4328..b6618977f 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -345,8 +345,8 @@ func TestParseSource(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Default is agent's DC and no node (since the user didn't care, - // then just give them the cheapest possible query). + // Default is agent's DC and no node (since the user didn't care, then + // just give them the cheapest possible query). req, err := http.NewRequest("GET", "/v1/catalog/nodes", nil) if err != nil { @@ -382,6 +382,18 @@ func TestParseSource(t *testing.T) { if source.Datacenter != "foo" || source.Node != "bob" { t.Fatalf("bad: %v", source) } + + // The magic "_agent" node name will use the agent's local node name. + req, err = http.NewRequest("GET", + "/v1/catalog/nodes?near=_agent", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + source = structs.QuerySource{} + srv.parseSource(req, &source) + if source.Datacenter != "dc1" || source.Node != srv.agent.config.NodeName { + t.Fatalf("bad: %v", source) + } } func TestParseWait(t *testing.T) {