/
bind.go
140 lines (123 loc) · 3.98 KB
/
bind.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
package handlers
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/gorilla/mux"
"github.com/pivotal-cf/brokerapi/domain"
"github.com/pivotal-cf/brokerapi/domain/apiresponses"
"github.com/pivotal-cf/brokerapi/middlewares"
"github.com/pivotal-cf/brokerapi/utils"
)
const (
invalidBindDetailsErrorKey = "invalid-bind-details"
bindLogKey = "bind"
)
func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
instanceID := vars["instance_id"]
bindingID := vars["binding_id"]
logger := h.logger.Session(bindLogKey, lager.Data{
instanceIDLogKey: instanceID,
bindingIDLogKey: bindingID,
}, utils.DataForContext(req.Context(), middlewares.CorrelationIDKey))
version := getAPIVersion(req)
asyncAllowed := false
if version.Minor >= 14 {
asyncAllowed = req.FormValue("accepts_incomplete") == "true"
}
var details domain.BindDetails
if err := json.NewDecoder(req.Body).Decode(&details); err != nil {
logger.Error(invalidBindDetailsErrorKey, err)
h.respond(w, http.StatusUnprocessableEntity, apiresponses.ErrorResponse{
Description: err.Error(),
})
return
}
if details.ServiceID == "" {
logger.Error(serviceIdMissingKey, serviceIdError)
h.respond(w, http.StatusBadRequest, apiresponses.ErrorResponse{
Description: serviceIdError.Error(),
})
return
}
if details.PlanID == "" {
logger.Error(planIdMissingKey, planIdError)
h.respond(w, http.StatusBadRequest, apiresponses.ErrorResponse{
Description: planIdError.Error(),
})
return
}
binding, err := h.serviceBroker.Bind(req.Context(), instanceID, bindingID, details, asyncAllowed)
if err != nil {
switch err := err.(type) {
case *apiresponses.FailureResponse:
statusCode := err.ValidatedStatusCode(logger)
errorResponse := err.ErrorResponse()
if err == apiresponses.ErrInstanceDoesNotExist {
// work around ErrInstanceDoesNotExist having different pre-refactor behaviour to other actions
errorResponse = apiresponses.ErrorResponse{
Description: err.Error(),
}
statusCode = http.StatusNotFound
}
logger.Error(err.LoggerAction(), err)
h.respond(w, statusCode, errorResponse)
default:
logger.Error(unknownErrorKey, err)
h.respond(w, http.StatusInternalServerError, apiresponses.ErrorResponse{
Description: err.Error(),
})
}
return
}
if binding.AlreadyExists {
h.respond(w, http.StatusOK, apiresponses.BindingResponse{
Credentials: binding.Credentials,
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
})
return
}
if binding.IsAsync {
h.respond(w, http.StatusAccepted, apiresponses.AsyncBindResponse{
OperationData: binding.OperationData,
})
return
}
if version.Minor == 8 || version.Minor == 9 {
experimentalVols := []domain.ExperimentalVolumeMount{}
for _, vol := range binding.VolumeMounts {
experimentalConfig, err := json.Marshal(vol.Device.MountConfig)
if err != nil {
logger.Error(unknownErrorKey, err)
h.respond(w, http.StatusInternalServerError, apiresponses.ErrorResponse{Description: err.Error()})
return
}
experimentalVols = append(experimentalVols, domain.ExperimentalVolumeMount{
ContainerPath: vol.ContainerDir,
Mode: vol.Mode,
Private: domain.ExperimentalVolumeMountPrivate{
Driver: vol.Driver,
GroupID: vol.Device.VolumeId,
Config: string(experimentalConfig),
},
})
}
experimentalBinding := apiresponses.ExperimentalVolumeMountBindingResponse{
Credentials: binding.Credentials,
RouteServiceURL: binding.RouteServiceURL,
SyslogDrainURL: binding.SyslogDrainURL,
VolumeMounts: experimentalVols,
}
h.respond(w, http.StatusCreated, experimentalBinding)
return
}
h.respond(w, http.StatusCreated, apiresponses.BindingResponse{
Credentials: binding.Credentials,
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
})
}