/
api_server.go
60 lines (49 loc) · 2.17 KB
/
api_server.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
package flexvolume
import (
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
type apiServer struct {
client Client
}
func newAPIServer(client Client) *apiServer {
return &apiServer{client}
}
func (a *apiServer) Init(_ context.Context, _ *empty.Empty) (_ *empty.Empty, err error) {
defer func(start time.Time) { log("Init", nil, nil, err, time.Since(start)) }(time.Now())
return checkClientError(a.client.Init())
}
func (a *apiServer) Attach(_ context.Context, request *AttachRequest) (_ *empty.Empty, err error) {
defer func(start time.Time) { log("Attach", request, nil, err, time.Since(start)) }(time.Now())
return checkClientError(a.client.Attach(request.JsonOptions))
}
func (a *apiServer) Detach(_ context.Context, request *DetachRequest) (_ *empty.Empty, err error) {
defer func(start time.Time) { log("Detach", request, nil, err, time.Since(start)) }(time.Now())
return checkClientError(a.client.Detach(request.MountDevice, nil))
}
func (a *apiServer) Mount(_ context.Context, request *MountRequest) (_ *empty.Empty, err error) {
defer func(start time.Time) { log("Mount", request, nil, err, time.Since(start)) }(time.Now())
return checkClientError(a.client.Mount(request.TargetMountDir, request.MountDevice, request.JsonOptions))
}
func (a *apiServer) Unmount(_ context.Context, request *UnmountRequest) (_ *empty.Empty, err error) {
defer func(start time.Time) { log("Unmount", request, nil, err, time.Since(start)) }(time.Now())
return checkClientError(a.client.Unmount(request.MountDir, nil))
}
func checkClientError(err error) (*empty.Empty, error) {
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func log(methodName string, request proto.Message, response proto.Message, err error, duration time.Duration) {
if err != nil {
logrus.Errorf("Method: %v Request: %v Response: %v Error: %v Duration: %v",
methodName, request.String(), response.String(), err.Error(), duration.String())
} else {
logrus.Infof("Method: %v Request: %v Response: %v Error: %v Duration: %v",
methodName, request.String(), response.String(), err.Error(), duration.String())
}
}