-
Notifications
You must be signed in to change notification settings - Fork 57
/
service.go
40 lines (33 loc) · 919 Bytes
/
service.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
package actions
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
func DoActionCommand(ctx context.Context, req *backend.CallResourceRequest, handler ActionHandler, sender backend.CallResourceResponseSender) error {
rsp := ActionResponse{}
cmd := &ActionCommand{}
if req.Method == "POST" {
if err := json.Unmarshal(req.Body, cmd); err != nil {
return fmt.Errorf("error read")
}
rsp = handler.ExecuteAction(ctx, *cmd)
} else {
return sender.Send(&backend.CallResourceResponse{
Status: http.StatusMethodNotAllowed,
Body: []byte("Expects GET|POST"),
})
}
json, err := json.Marshal(rsp)
if err != nil {
return err
}
// Log results somewhere
backend.Logger.Info("ACTION", "user", req.PluginContext.User, "cmd", cmd, "result", rsp)
return sender.Send(&backend.CallResourceResponse{
Status: rsp.Code,
Body: json,
})
}