-
Notifications
You must be signed in to change notification settings - Fork 357
/
response_parser.rb
328 lines (281 loc) · 11.1 KB
/
response_parser.rb
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
module VCAP::Services
module ServiceBrokers
module V2
class ResponseParser
SERVICE_BINDINGS_REGEX = %r{/v2/service_instances/[[:alnum:]-]+/service_bindings}
def initialize(url)
@url = url
@logger = Steno.logger('cc.service_broker.v2.client')
end
def parse_provision_or_bind(path, response)
unvalidated_response = UnvalidatedResponse.new(:put, @url, path, response)
validator =
case unvalidated_response.code
when 200
JsonObjectValidator.new(@logger,
SuccessValidator.new(state: 'succeeded'))
when 201
JsonObjectValidator.new(@logger,
SuccessValidator.new(state: 'succeeded'))
when 202
JsonObjectValidator.new(@logger,
IfElsePathMatchValidator.new(SERVICE_BINDINGS_REGEX,
FailingValidator.new(Errors::ServiceBrokerBadResponse),
SuccessValidator.new(state: 'in progress')))
when 409
FailingValidator.new(Errors::ServiceBrokerConflict)
when 422
FailWhenValidator.new('error', ['AsyncRequired'], Errors::AsyncRequired,
FailingValidator.new(Errors::ServiceBrokerBadResponse))
else
FailingValidator.new(Errors::ServiceBrokerBadResponse)
end
validator = CommonErrorValidator.new(validator)
validator.validate(unvalidated_response.to_hash)
end
def parse_deprovision_or_unbind(path, response)
unvalidated_response = UnvalidatedResponse.new(:delete, @url, path, response)
validator =
case unvalidated_response.code
when 200
JsonObjectValidator.new(@logger,
SuccessValidator.new(state: 'succeeded'))
when 201
IgnoreDescriptionKeyFailingValidator.new(Errors::ServiceBrokerBadResponse)
when 202
JsonObjectValidator.new(@logger,
IfElsePathMatchValidator.new(SERVICE_BINDINGS_REGEX,
FailingValidator.new(Errors::ServiceBrokerBadResponse),
SuccessValidator.new(state: 'in progress')))
when 204
SuccessValidator.new { |res| {} }
when 410
@logger.warn("Already deleted: #{unvalidated_response.uri}")
SuccessValidator.new { |res| {} }
when 422
FailWhenValidator.new('error', ['AsyncRequired'], Errors::AsyncRequired,
FailingValidator.new(Errors::ServiceBrokerBadResponse))
else
FailingValidator.new(Errors::ServiceBrokerBadResponse)
end
validator = CommonErrorValidator.new(validator)
validator.validate(unvalidated_response.to_hash)
end
def parse_catalog(path, response)
unvalidated_response = UnvalidatedResponse.new(:get, @url, path, response)
validator =
case unvalidated_response.code
when 200
JsonObjectValidator.new(@logger, SuccessValidator.new)
when 201, 202
JsonObjectValidator.new(@logger,
FailingValidator.new(Errors::ServiceBrokerBadResponse))
else
FailingValidator.new(Errors::ServiceBrokerBadResponse)
end
validator = CommonErrorValidator.new(validator)
validator.validate(unvalidated_response.to_hash)
end
def parse_update(path, response)
unvalidated_response = UnvalidatedResponse.new(:patch, @url, path, response)
validator =
case unvalidated_response.code
when 200
JsonObjectValidator.new(@logger,
SuccessValidator.new(state: 'succeeded'))
when 201
IgnoreDescriptionKeyFailingValidator.new(Errors::ServiceBrokerBadResponse)
when 202
JsonObjectValidator.new(@logger,
SuccessValidator.new(state: 'in progress'))
when 422
FailWhenValidator.new('error', ['AsyncRequired'], Errors::AsyncRequired,
FailingValidator.new(Errors::ServiceBrokerRequestRejected))
else
FailingValidator.new(Errors::ServiceBrokerBadResponse)
end
validator = CommonErrorValidator.new(validator)
validator.validate(unvalidated_response.to_hash)
end
def parse_fetch_state(path, response)
unvalidated_response = UnvalidatedResponse.new(:get, @url, path, response)
validator =
case unvalidated_response.code
when 200
JsonObjectValidator.new(@logger,
StateValidator.new(['succeeded', 'failed', 'in progress'],
SuccessValidator.new))
when 201, 202
JsonObjectValidator.new(@logger,
FailingValidator.new(Errors::ServiceBrokerBadResponse))
when 410
SuccessValidator.new { |res| {} }
else
FailingValidator.new(Errors::ServiceBrokerBadResponse)
end
validator = CommonErrorValidator.new(validator)
validator.validate(unvalidated_response.to_hash)
end
class UnvalidatedResponse
attr_reader :code, :uri
def initialize(method, uri, path, response)
@method = method
@code = response.code.to_i
@uri = URI(uri + path)
@response = response
end
def body
response.body
end
def to_hash
{
method: @method,
uri: @uri,
code: @code,
response: @response,
}
end
end
class IfElsePathMatchValidator
attr_reader :error_class
def initialize(path_regex, if_validator, else_validator)
@path_regex = path_regex
@if_validator = if_validator
@else_validator = else_validator
end
def validate(method:, uri:, code:, response:)
if @path_regex.match(uri.to_s)
@if_validator.validate(method: method, uri: uri, code: code, response: response)
else
@else_validator.validate(method: method, uri: uri, code: code, response: response)
end
end
end
class FailWhenValidator
def initialize(key, values, error_class, validator)
@key = key
@values = values
@error_class = error_class
@validator = validator
end
def validate(method:, uri:, code:, response:)
begin
parsed_response = MultiJson.load(response.body)
rescue MultiJson::ParseError
@validator.validate(method: method, uri: uri, code: code, response: response)
return
end
if @values.include?(parsed_response[@key])
raise @error_class.new(uri.to_s, method, response)
else
@validator.validate(method: method, uri: uri, code: code, response: response)
end
end
end
class SuccessValidator
def initialize(state: nil, &block)
if block_given?
@processor = block
else
@processor = ->(response) do
broker_response = MultiJson.load(response.body)
state ||= broker_response.delete('state')
return broker_response unless state
base_body = {
'last_operation' => {
'state' => state
}
}
description = broker_response.delete('description')
base_body['last_operation']['description'] = description if description
broker_response.merge(base_body)
end
end
end
def validate(method:, uri:, code:, response:)
@processor.call(response)
end
end
class FailingValidator
def initialize(error_class)
@error_class = error_class
end
def validate(method:, uri:, code:, response:)
raise @error_class.new(uri.to_s, method, response)
end
end
class IgnoreDescriptionKeyFailingValidator
def initialize(error_class)
@error_class = error_class
end
def validate(method:, uri:, code:, response:)
raise @error_class.new(uri.to_s, method, response, ignore_description_key: true)
end
end
class JsonObjectValidator
def initialize(logger, validator)
@logger = logger
@validator = validator
end
def validate(method:, uri:, code:, response:)
begin
parsed_response = MultiJson.load(response.body)
rescue MultiJson::ParseError
@logger.warn("MultiJson parse error `#{response.try(:body).inspect}'")
end
unless parsed_response.is_a?(Hash)
raise Errors::ServiceBrokerResponseMalformed.new(uri, @method, response, error_description(response))
end
@validator.validate(method: method, uri: uri, code: code, response: response)
end
private
def error_description(response)
"expected valid JSON object in body, broker returned '#{response.body}'"
end
end
class StateValidator
def initialize(valid_states, validator)
@valid_states = valid_states
@validator = validator
end
def validate(method:, uri:, code:, response:)
parsed_response = MultiJson.load(response.body)
state = state_from_parsed_response(parsed_response)
if @valid_states.include?(state)
@validator.validate(method: method, uri: uri, code: code, response: response)
else
raise Errors::ServiceBrokerResponseMalformed.new(uri.to_s, @method, response, error_description(state))
end
end
private
def state_from_parsed_response(parsed_response)
parsed_response ||= {}
parsed_response['state']
end
def error_description(actual)
"expected state was 'succeeded', broker returned '#{actual}'."
end
end
class CommonErrorValidator
def initialize(validator)
@validator = validator
end
def validate(method:, uri:, code:, response:)
case code
when 401
raise Errors::ServiceBrokerApiAuthenticationFailed.new(uri.to_s, method, response)
when 408
raise Errors::ServiceBrokerApiTimeout.new(uri.to_s, method, response)
when 409, 410, 422
when 400..499
raise Errors::ServiceBrokerRequestRejected.new(uri.to_s, method, response)
when 500..599
raise Errors::ServiceBrokerBadResponse.new(uri.to_s, method, response)
end
@validator.validate(method: method, uri: uri, code: code, response: response)
end
end
end
end
end
end