forked from micromdm/micromdm
/
block_device.go
87 lines (74 loc) · 2 KB
/
block_device.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package remove
import (
"context"
"net/http"
"net/url"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/micromdm/micromdm/pkg/httputil"
"github.com/pkg/errors"
)
func (svc *RemoveService) BlockDevice(ctx context.Context, udid string) error {
return svc.store.Save(&Device{UDID: udid})
}
type blockDeviceRequest struct {
UDID string
}
type blockDeviceResponse struct {
Err error `json:"err,omitempty"`
}
func (r blockDeviceResponse) Failed() error { return r.Err }
func decodeBlockDeviceRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var errBadRoute = errors.New("bad route")
var req blockDeviceRequest
vars := mux.Vars(r)
udid, ok := vars["udid"]
if !ok {
return 0, errBadRoute
}
req.UDID = udid
return req, nil
}
func encodeBlockDeviceRequest(_ context.Context, r *http.Request, request interface{}) error {
req := request.(blockDeviceRequest)
udid := url.QueryEscape(req.UDID)
r.Method, r.URL.Path = "POST", "/v1/devices/"+udid+"/block"
return nil
}
func decodeBlockDeviceResponse(_ context.Context, r *http.Response) (interface{}, error) {
var resp blockDeviceResponse
err := httputil.DecodeJSONResponse(r, &resp)
return resp, err
}
func MakeBlockDeviceEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(blockDeviceRequest)
err = svc.BlockDevice(ctx, req.UDID)
return &blockDeviceResponse{
Err: err,
}, nil
}
}
func (e Endpoints) BlockDevice(ctx context.Context, udid string) error {
request := blockDeviceRequest{
UDID: udid,
}
resp, err := e.BlockDeviceEndpoint(ctx, request)
if err != nil {
return err
}
return resp.(blockDeviceResponse).Err
}
func (mw logmw) BlockDevice(ctx context.Context, udid string) (err error) {
defer func(begin time.Time) {
_ = mw.logger.Log(
"method", "BlockDevice",
"udid", udid,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
err = mw.next.BlockDevice(ctx, udid)
return
}