Allow passing arguments to controls

This commit is contained in:
Alfonso Acosta
2016-10-29 09:52:38 +00:00
committed by Simon Howe
parent 403b70dde8
commit 37ba071feb
3 changed files with 28 additions and 10 deletions

View File

@@ -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())

View File

@@ -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)
}

View File

@@ -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.