From 37ba071febb1576e97ec6030e75bb939de56a105 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Sat, 29 Oct 2016 09:52:38 +0000 Subject: [PATCH] Allow passing arguments to controls --- app/controls.go | 25 +++++++++++++++++++------ app/controls_test.go | 6 +++++- common/xfer/controls.go | 7 ++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/controls.go b/app/controls.go index 58eae55ab..1977f71f1 100644 --- a/app/controls.go +++ b/app/controls.go @@ -6,6 +6,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" + "github.com/ugorji/go/codec" "golang.org/x/net/context" "github.com/weaveworks/scope/common/xfer" @@ -29,14 +30,26 @@ func RegisterControlRoutes(router *mux.Router, cr ControlRouter) { func handleControl(cr ControlRouter) CtxHandlerFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { var ( - vars = mux.Vars(r) - probeID = vars["probeID"] - nodeID = vars["nodeID"] - control = vars["control"] + vars = mux.Vars(r) + probeID = vars["probeID"] + nodeID = vars["nodeID"] + control = vars["control"] + controlArgs map[string]string ) + + if r.ContentLength > 0 { + err := codec.NewDecoder(r.Body, &codec.JsonHandle{}).Decode(&controlArgs) + defer r.Body.Close() + if err != nil { + respondWith(w, http.StatusBadRequest, err) + return + } + } + result, err := cr.Handle(ctx, probeID, xfer.Request{ - NodeID: nodeID, - Control: control, + NodeID: nodeID, + Control: control, + ControlArgs: controlArgs, }) if err != nil { respondWith(w, http.StatusBadRequest, err.Error()) diff --git a/app/controls_test.go b/app/controls_test.go index bc2508eda..f5ecbdfe3 100644 --- a/app/controls_test.go +++ b/app/controls_test.go @@ -57,7 +57,11 @@ func TestControl(t *testing.T) { httpClient := http.Client{ Timeout: 1 * time.Second, } - resp, err := httpClient.Post(server.URL+"/api/control/foo/nodeid/control", "", nil) + resp, err := httpClient.Post( + server.URL+"/api/control/foo/nodeid/control", + "application/json", + strings.NewReader("{}"), + ) if err != nil { t.Fatal(err) } diff --git a/common/xfer/controls.go b/common/xfer/controls.go index 244d70add..7d77e418f 100644 --- a/common/xfer/controls.go +++ b/common/xfer/controls.go @@ -11,9 +11,10 @@ var ErrInvalidMessage = fmt.Errorf("Invalid Message") // Request is the UI -> App -> Probe message type for control RPCs type Request struct { - AppID string // filled in by the probe on receiving this request - NodeID string - Control string + AppID string // filled in by the probe on receiving this request + NodeID string + Control string + ControlArgs map[string]string } // Response is the Probe -> App -> UI message type for the control RPCs.