Skip to content

Commit

Permalink
Allow passing arguments to controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta authored and foot committed Oct 31, 2016
1 parent 403b70d commit 37ba071
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
25 changes: 19 additions & 6 deletions app/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand Down
6 changes: 5 additions & 1 deletion app/controls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions common/xfer/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 37ba071

Please sign in to comment.