forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.go
58 lines (50 loc) · 1.46 KB
/
signal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package api
import (
"encoding/json"
"net/http"
"github.com/puppetlabs/wash/activity"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
// swagger:route POST /fs/signal signal signalEntry
//
// Sends a signal to the entry at the specified path.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200:
// 400: errorResp
// 404: errorResp
// 500: errorResp
var signalHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *errorResponse {
ctx := r.Context()
entry, path, errResp := getEntryFromRequest(r)
if errResp != nil {
return errResp
}
if !plugin.SignalAction().IsSupportedOn(entry) {
return unsupportedActionResponse(path, plugin.SignalAction())
}
if r.Body == nil {
return badActionRequestResponse(path, plugin.SignalAction(), "Please send a JSON request body")
}
var body apitypes.SignalBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
return badActionRequestResponse(path, plugin.SignalAction(), err.Error())
}
if err := plugin.SignalWithAnalytics(ctx, entry.(plugin.Signalable), body.Signal); err != nil {
if plugin.IsInvalidInputErr(err) {
return badActionRequestResponse(path, plugin.SignalAction(), err.Error())
}
return erroredActionResponse(path, plugin.SignalAction(), err.Error())
}
activity.Record(ctx, "API: Signal %v %v", path, body.Signal)
return nil
}}