This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
/
update.go
369 lines (342 loc) · 11.9 KB
/
update.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package api
import (
"io/ioutil"
"net/http"
"reflect"
"strconv"
"github.com/Azure/open-service-broker-azure/pkg/service"
log "github.com/Sirupsen/logrus"
"github.com/deis/async"
"github.com/gorilla/mux"
)
func (s *server) update(w http.ResponseWriter, r *http.Request) {
instanceID := mux.Vars(r)["instance_id"]
logFields := log.Fields{
"instanceID": instanceID,
}
log.WithFields(logFields).Debug("received updating request")
// This broker updates everything asynchronously. If a client doesn't
// explicitly indicate that they will accept an incomplete result, the
// spec says to respond with a 422
acceptsIncompleteStr := r.URL.Query().Get("accepts_incomplete")
if acceptsIncompleteStr == "" {
logFields["parameter"] = "accepts_incomplete=true" // nolint: goconst
log.WithFields(logFields).Debug(
"bad updating request: request is missing required query parameter",
)
s.writeResponse(
w,
http.StatusUnprocessableEntity,
generateAsyncRequiredResponse(),
)
return
}
acceptsIncomplete, err := strconv.ParseBool(acceptsIncompleteStr)
if err != nil || !acceptsIncomplete {
logFields["accepts_incomplete"] = acceptsIncompleteStr
log.WithFields(logFields).Debug(
`bad updating request: query parameter has invalid value; only ` +
`"true" is accepted`,
)
s.writeResponse(
w,
http.StatusUnprocessableEntity,
generateAsyncRequiredResponse(),
)
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
logFields["error"] = err
log.WithFields(logFields).Error(
"pre-updating error: error reading request body",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
defer r.Body.Close() // nolint: errcheck
updatingRequest, err := NewUpdatingRequestFromJSON(bodyBytes)
if err != nil {
logFields["error"] = err
log.WithFields(logFields).Debug(
"bad updating request: error unmarshaling request body",
)
// krancour: Choosing to interpret this scenario as a bad request, as a
// valid request, obviously contains valid, well-formed JSON
// TODO: Write a more detailed response
s.writeResponse(w, http.StatusBadRequest, generateEmptyResponse())
return
}
if updatingRequest.ServiceID == "" {
logFields["field"] = "service_id"
log.WithFields(logFields).Debug(
"bad updating request: required request body field is missing",
)
s.writeResponse(w, http.StatusBadRequest, generateServiceIDRequiredResponse())
return
}
svc, ok := s.catalog.GetService(updatingRequest.ServiceID)
if !ok {
logFields["serviceID"] = updatingRequest.ServiceID
log.WithFields(logFields).Debug(
"bad updating request: invalid serviceID",
)
s.writeResponse(w, http.StatusBadRequest, generateInvalidServiceIDResponse())
return
}
var plan service.Plan
if updatingRequest.PlanID != "" {
plan, ok = svc.GetPlan(updatingRequest.PlanID)
if !ok {
logFields["serviceID"] = updatingRequest.ServiceID
logFields["planID"] = updatingRequest.PlanID
log.WithFields(logFields).Debug(
"bad updating request: invalid planID for service",
)
s.writeResponse(w, http.StatusBadRequest, generateInvalidPlanIDResponse())
return
}
}
instance, ok, err := s.store.GetInstance(instanceID)
if err != nil {
logFields["error"] = err
log.WithFields(logFields).Error(
"pre-updating error: error retrieving instance by id",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
if !ok {
log.WithFields(logFields).Debug(
"bad updating request: the instance does not exist",
)
// The instance to update does not exist
// krancour: Choosing to interpret this scenario as a bad request
// TODO: Write a more detailed response
s.writeResponse(w, http.StatusBadRequest, generateEmptyResponse())
return
}
// Our broker doesn't actually require the serviceID and previousValues that,
// per spec, are passed to us in the request body (since this broker is
// stateful, we can get these details from the instance we already
// retrieved), BUT if serviceID and previousValues were provided, they BETTER
// be the same as what's in the instance-- or else we obviously have a
// conflict.
if (updatingRequest.ServiceID != instance.ServiceID) ||
(updatingRequest.PreviousValues.PlanID != "" &&
updatingRequest.PreviousValues.PlanID != instance.PlanID) {
logFields["serviceID"] = instance.ServiceID
logFields["requestServiceID"] = updatingRequest.ServiceID
logFields["previousPlanID"] = instance.PlanID
logFields["requestPreviousPlanID"] = updatingRequest.PreviousValues.PlanID
log.WithFields(logFields).Debug(
"bad updating request: serviceID or previousPlanID does not match " +
"serviceID or previousPlanID on the instance",
)
// TODO: Write a more detailed response
s.writeResponse(w, http.StatusConflict, generateEmptyResponse())
return
}
serviceManager := svc.GetServiceManager()
// Merge update parameters with the instance's provisioning params to build
// a complete set of params
rawUpdatingParameters := updatingRequest.Parameters
if instance.ProvisioningParameters != nil {
rawUpdatingParameters = mergeUpdateParameters(
instance.ProvisioningParameters.Data,
updatingRequest.Parameters,
)
}
// This determines whether the parameters of the update request are already
// reflected in the provisioning parameters of a fully provisioned (or fully
// updated) instance OR the parameters of the update request are already
// reflected in the existing updating parameters of an in-progress update.
existingParams := map[string]interface{}{}
switch instance.Status {
case service.InstanceStateProvisioning:
fallthrough
case service.InstanceStateProvisioned:
if instance.ProvisioningParameters != nil {
existingParams = instance.ProvisioningParameters.Data
}
case service.InstanceStateUpdating:
if instance.UpdatingParameters != nil {
existingParams = instance.UpdatingParameters.Data
}
default:
// If instance isn't fully provisioned (or updated) and there isn't an
// update in-progress, we cannot handle this request. It's a conflict.
s.writeResponse(w, http.StatusConflict, generateEmptyResponse())
return
}
if !reflect.DeepEqual(existingParams, rawUpdatingParameters) {
if instance.Status == service.InstanceStateUpdating {
// We cannot handle two updates at once. This is a conflict.
s.writeResponse(w, http.StatusConflict, generateEmptyResponse())
return
}
} else {
if instance.Status == service.InstanceStateProvisioned {
// In this case, the requested update is already completed
s.writeResponse(w, http.StatusOK, generateEmptyResponse())
return
}
// In this case, the requested update is already in-progress
s.writeResponse(w, http.StatusAccepted, generateUpdateAcceptedResponse())
return
}
// Only one scenario gets us to this point-- the instance is fully provisioned
// (or fully updated) and the parameters of the update request indicate the
// need for a new update.
// Carry out schema-driven update request parameters validation.
if err :=
instance.Plan.GetSchemas().ServiceInstances.UpdatingParametersSchema.Validate( // nolint: lll
updatingRequest.Parameters,
); err != nil {
var validationErr *service.ValidationError
validationErr, ok = err.(*service.ValidationError)
if ok {
logFields["field"] = validationErr.Field
logFields["issue"] = validationErr.Issue
log.WithFields(logFields).Debug(
"bad updating request: validation error",
)
s.writeResponse(
w,
http.StatusBadRequest,
generateValidationFailedResponse(validationErr),
)
return
}
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
// Wrap the updating parameters with a "params" object that guides access
// to the parameters using schema. This uses provisioning schema instead of
// updating schema so that when persisting, we will be able to persist the
// full combined provisioning + updating parameters instea of just the subset
// that are updating params.
pps := plan.GetSchemas().ServiceInstances.ProvisioningParametersSchema
updatingParameters := &service.ProvisioningParameters{
Parameters: service.Parameters{
Schema: &pps,
Data: rawUpdatingParameters,
},
}
// This uses module-specific logic to weigh update parameters against current
// instance state to detect any invalid state changes. An example of this
// might be reducing the amound of storage allocated to a database.
instance.UpdatingParameters = updatingParameters
if err := serviceManager.ValidateUpdatingParameters(instance); err != nil {
var validationErr *service.ValidationError
validationErr, ok = err.(*service.ValidationError)
if ok {
logFields["field"] = validationErr.Field
logFields["issue"] = validationErr.Issue
log.WithFields(logFields).Debug(
"bad updating request: validation error",
)
s.writeResponse(
w,
http.StatusBadRequest,
generateValidationFailedResponse(validationErr),
)
return
}
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
// If we get to here, we need to update the instance.
if plan == nil {
plan, ok = svc.GetPlan(instance.PlanID)
if !ok {
logFields["serviceID"] = updatingRequest.ServiceID
logFields["planID"] = instance.PlanID
log.WithFields(logFields).Error(
"pre-updating error: no Plan found for planID in Service",
)
s.writeResponse(
w,
http.StatusInternalServerError,
generateInvalidPlanIDResponse(),
)
return
}
}
updater, err := serviceManager.GetUpdater(plan)
if err != nil {
logFields["serviceID"] = updatingRequest.ServiceID
logFields["planID"] = updatingRequest.PlanID
logFields["error"] = err
log.WithFields(logFields).Error(
"pre-updating error: error retrieving updater for service and plan",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
firstStepName, ok := updater.GetFirstStepName()
if !ok {
logFields["serviceID"] = updatingRequest.ServiceID
logFields["planID"] = updatingRequest.PlanID
log.WithFields(logFields).Error(
"pre-updating error: no steps found for updating service and plan",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
instance.Status = service.InstanceStateUpdating
instance.PlanID = updatingRequest.PlanID
if err := s.store.WriteInstance(instance); err != nil {
logFields["error"] = err
log.WithFields(logFields).Error(
"updating error: error persisting updated instance",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
task := async.NewTask(
"executeUpdatingStep",
map[string]string{
"stepName": firstStepName,
"instanceID": instanceID,
},
)
if err := s.asyncEngine.SubmitTask(task); err != nil {
logFields["step"] = firstStepName
logFields["error"] = err
log.WithFields(logFields).Error(
"updating error: error submitting updating task",
)
s.writeResponse(w, http.StatusInternalServerError, generateEmptyResponse())
return
}
// If we get all the way to here, we've been successful!
s.writeResponse(w, http.StatusAccepted, generateUpdateAcceptedResponse())
log.WithFields(logFields).Debug("asynchronous updating initiated")
}
func mergeUpdateParameters(
pp map[string]interface{},
up map[string]interface{},
) map[string]interface{} {
// If there are no provisioning parameters, the merged
// set is the updating parameters
if pp == nil {
return up
}
ppCopy := map[string]interface{}{}
for key, value := range pp {
ppCopy[key] = value
}
// The OSB spec states that if the request doesn't include a
// previously specified parameter value, it should remain unchanged.
// This iterates through the updating params and replace the
// corresponding provision params if the value in updating
// params is not empty. This will result in a merged copy
// of the two that reflects the actual requested instance state
// using both the previously specificed parameters and the new
// parameters.
for key, value := range up {
ppCopy[key] = value
}
return ppCopy
}