Adding catalog datacenter http methods

pull/19/head
Armon Dadgar 11 years ago
parent caaff11634
commit f062e31be7

@ -0,0 +1,13 @@
package agent
import (
"net/http"
)
func (s *HTTPServer) CatalogDatacenters(req *http.Request) (interface{}, error) {
var out []string
if err := s.agent.RPC("Catalog.ListDatacenters", struct{}{}, &out); err != nil {
return nil, err
}
return out, nil
}

@ -0,0 +1,23 @@
package agent
import (
"os"
"testing"
)
func TestCatalogDatacenters(t *testing.T) {
dir, srv := makeHTTPServer(t)
defer os.RemoveAll(dir)
defer srv.Shutdown()
defer srv.agent.Shutdown()
obj, err := srv.CatalogDatacenters(nil)
if err != nil {
t.Fatalf("err: %v", err)
}
dcs := obj.([]string)
if len(dcs) != 1 {
t.Fatalf("bad: %v", obj)
}
}

@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
"time"
)
// HTTPServer is used to wrap an Agent and expose various API's
@ -53,13 +54,18 @@ func (s *HTTPServer) Shutdown() {
func (s *HTTPServer) registerHandlers() {
s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeader))
s.mux.HandleFunc("/v1/status/peers", s.wrap(s.StatusPeers))
s.mux.HandleFunc("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
}
// wrap is used to wrap functions to make them more convenient
func (s *HTTPServer) wrap(handler func(req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
f := func(resp http.ResponseWriter, req *http.Request) {
// Invoke the handler
s.logger.Printf("[DEBUG] Request %v", req)
start := time.Now()
defer func() {
s.logger.Printf("[DEBUG] HTTP Request %v (%v)", req.URL, time.Now().Sub(start))
}()
obj, err := handler(req)
// Check for an error

Loading…
Cancel
Save