From 291455f475ec2611f86b4375ed6d5069afccb8f8 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Mon, 23 Oct 2017 17:44:50 -0700 Subject: [PATCH] Added coordinate update http endpoint --- agent/coordinate_endpoint.go | 22 ++++++++++++++++++++++ agent/http.go | 2 ++ api/coordinate.go | 17 +++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/agent/coordinate_endpoint.go b/agent/coordinate_endpoint.go index e9ac566d95..6e17b766f4 100644 --- a/agent/coordinate_endpoint.go +++ b/agent/coordinate_endpoint.go @@ -104,3 +104,25 @@ func (s *HTTPServer) CoordinateNodes(resp http.ResponseWriter, req *http.Request return out.Coordinates, nil } + +// CoordinateUpdate inserts or updates the LAN coordinate of a node. +func (s *HTTPServer) CoordinateUpdate(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + if req.Method != "PUT" { + return nil, MethodNotAllowedError{req.Method, []string{"PUT"}} + } + + args := structs.CoordinateUpdateRequest{} + if err := decodeBody(req, &args, nil); err != nil { + resp.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(resp, "Request decode failed: %v", err) + return nil, nil + } + s.parseDC(req, &args.Datacenter) + + var reply struct{} + if err := s.agent.RPC("Coordinate.Update", &args, &reply); err != nil { + return nil, err + } + + return nil, nil +} diff --git a/agent/http.go b/agent/http.go index 10189517d1..815e0ba9b4 100644 --- a/agent/http.go +++ b/agent/http.go @@ -139,9 +139,11 @@ func (s *HTTPServer) handler(enableDebug bool) http.Handler { if !s.agent.config.DisableCoordinates { handleFuncMetrics("/v1/coordinate/datacenters", s.wrap(s.CoordinateDatacenters)) handleFuncMetrics("/v1/coordinate/nodes", s.wrap(s.CoordinateNodes)) + handleFuncMetrics("/v1/coordinate/update", s.wrap(s.CoordinateUpdate)) } else { handleFuncMetrics("/v1/coordinate/datacenters", s.wrap(coordinateDisabled)) handleFuncMetrics("/v1/coordinate/nodes", s.wrap(coordinateDisabled)) + handleFuncMetrics("/v1/coordinate/update", s.wrap(coordinateDisabled)) } handleFuncMetrics("/v1/event/fire/", s.wrap(s.EventFire)) handleFuncMetrics("/v1/event/list", s.wrap(s.EventList)) diff --git a/api/coordinate.go b/api/coordinate.go index 90214e392c..c717097833 100644 --- a/api/coordinate.go +++ b/api/coordinate.go @@ -66,3 +66,20 @@ func (c *Coordinate) Nodes(q *QueryOptions) ([]*CoordinateEntry, *QueryMeta, err } return out, qm, nil } + +// Update inserts or updates the LAN coordinate of a node. +func (c *Coordinate) Update(coord *CoordinateEntry, q *WriteOptions) (*WriteMeta, error) { + r := c.c.newRequest("PUT", "/v1/coordinate/update") + r.setWriteOptions(q) + r.obj = coord + rtt, resp, err := requireOK(c.c.doRequest(r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + wm := &WriteMeta{} + wm.RequestTime = rtt + + return wm, nil +}