forked from cloudfoundry/bosh-softlayer-cpi-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
executable file
·166 lines (125 loc) · 3.42 KB
/
json.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package dispatcher
import (
"encoding/json"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
bslcaction "github.com/maximilien/bosh-softlayer-cpi/action"
bslcapi "github.com/maximilien/bosh-softlayer-cpi/api"
)
const (
jsonLogTag = "json"
jsonCloudErrorType = "Bosh::Clouds::CloudError"
jsonCpiErrorType = "Bosh::Clouds::CpiError"
jsonNotImplementedErrorType = "Bosh::Clouds::NotImplemented"
)
type Request struct {
Method string `json:"method"`
Arguments []interface{} `json:"arguments"`
// context key is ignored
}
type Response struct {
Result interface{} `json:"result"`
Error *ResponseError `json:"error"`
Log string `json:"log"`
}
type ResponseError struct {
Type string `json:"type"`
Message string `json:"message"`
CanRetry bool `json:"ok_to_retry"`
}
type JSON struct {
actionFactory bslcaction.Factory
caller Caller
logger boshlog.Logger
}
func NewJSON(
actionFactory bslcaction.Factory,
caller Caller,
logger boshlog.Logger,
) JSON {
return JSON{
actionFactory: actionFactory,
caller: caller,
logger: logger,
}
}
func (c JSON) Dispatch(reqBytes []byte) []byte {
var req Request
c.logger.DebugWithDetails(jsonLogTag, "Request bytes", string(reqBytes))
err := json.Unmarshal(reqBytes, &req)
if err != nil {
return c.buildCpiError("Must provide valid JSON payload")
}
c.logger.DebugWithDetails(jsonLogTag, "Deserialized request", req)
if req.Method == "" {
return c.buildCpiError("Must provide method key")
}
if req.Arguments == nil {
return c.buildCpiError("Must provide arguments key")
}
action, err := c.actionFactory.Create(req.Method)
if err != nil {
return c.buildNotImplementedError()
}
result, err := c.caller.Call(action, req.Arguments)
if err != nil {
return c.buildCloudError(err)
}
resp := Response{
Result: result,
}
c.logger.DebugWithDetails(jsonLogTag, "Deserialized response", resp)
respBytes, err := json.Marshal(resp)
if err != nil {
return c.buildCpiError("Failed to serialize result")
}
c.logger.DebugWithDetails(jsonLogTag, "Response bytes", string(respBytes))
return respBytes
}
func (c JSON) buildCloudError(err error) []byte {
respErr := Response{
Error: &ResponseError{},
}
if typedErr, ok := err.(bslcapi.CloudError); ok {
respErr.Error.Type = typedErr.Type()
} else {
respErr.Error.Type = jsonCloudErrorType
}
respErr.Error.Message = err.Error()
if typedErr, ok := err.(bslcapi.RetryableError); ok {
respErr.Error.CanRetry = typedErr.CanRetry()
}
respErrBytes, err := json.Marshal(respErr)
if err != nil {
panic(err)
}
c.logger.DebugWithDetails(jsonLogTag, "CloudError response bytes", string(respErrBytes))
return respErrBytes
}
func (c JSON) buildCpiError(message string) []byte {
respErr := Response{
Error: &ResponseError{
Type: jsonCpiErrorType,
Message: message,
},
}
respErrBytes, err := json.Marshal(respErr)
if err != nil {
panic(err)
}
c.logger.DebugWithDetails(jsonLogTag, "CpiError response bytes", string(respErrBytes))
return respErrBytes
}
func (c JSON) buildNotImplementedError() []byte {
respErr := Response{
Error: &ResponseError{
Type: jsonNotImplementedErrorType,
Message: "Must call implemented method",
},
}
respErrBytes, err := json.Marshal(respErr)
if err != nil {
panic(err)
}
c.logger.DebugWithDetails(jsonLogTag, "NotImplementedError response bytes", string(respErrBytes))
return respErrBytes
}