forked from pivotal-cf/brokerapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_broker.go
306 lines (247 loc) · 11.3 KB
/
service_broker.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
// Copyright (C) 2015-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package brokerapi
import (
"context"
"encoding/json"
"errors"
"net/http"
)
//go:generate counterfeiter -o fakes/auto_fake_service_broker.go -fake-name AutoFakeServiceBroker . ServiceBroker
//Each method of the ServiceBroker interface maps to an individual endpoint of the Open Service Broker API.
//
//The specification is available here: https://github.com/openservicebrokerapi/servicebroker/blob/v2.14/spec.md
//
//The OpenAPI documentation is available here: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/openservicebrokerapi/servicebroker/v2.14/openapi.yaml
type ServiceBroker interface {
// Services gets the catalog of services offered by the service broker
// GET /v2/catalog
Services(ctx context.Context) ([]Service, error)
// Provision creates a new service instance
// PUT /v2/service_instances/{instance_id}
Provision(ctx context.Context, instanceID string, details ProvisionDetails, asyncAllowed bool) (ProvisionedServiceSpec, error)
// Deprovision deletes an existing service instance
// DELETE /v2/service_instances/{instance_id}
Deprovision(ctx context.Context, instanceID string, details DeprovisionDetails, asyncAllowed bool) (DeprovisionServiceSpec, error)
// GetInstance fetches information about a service instance
// GET /v2/service_instances/{instance_id}
GetInstance(ctx context.Context, instanceID string) (GetInstanceDetailsSpec, error)
// Update modifies an existing service instance
// PATCH /v2/service_instances/{instance_id}
Update(ctx context.Context, instanceID string, details UpdateDetails, asyncAllowed bool) (UpdateServiceSpec, error)
// LastOperation fetches last operation state for a service instance
// GET /v2/service_instances/{instance_id}/last_operation
LastOperation(ctx context.Context, instanceID string, details PollDetails) (LastOperation, error)
// Bind creates a new service binding
// PUT /v2/service_instances/{instance_id}/service_bindings/{binding_id}
Bind(ctx context.Context, instanceID, bindingID string, details BindDetails, asyncAllowed bool) (Binding, error)
// Unbind deletes an existing service binding
// DELETE /v2/service_instances/{instance_id}/service_bindings/{binding_id}
Unbind(ctx context.Context, instanceID, bindingID string, details UnbindDetails, asyncAllowed bool) (UnbindSpec, error)
// GetBinding fetches an existing service binding
// GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}
GetBinding(ctx context.Context, instanceID, bindingID string) (GetBindingSpec, error)
// LastBindingOperation fetches last operation state for a service binding
// GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation
LastBindingOperation(ctx context.Context, instanceID, bindingID string, details PollDetails) (LastOperation, error)
}
type DetailsWithRawParameters interface {
GetRawParameters() json.RawMessage
}
type DetailsWithRawContext interface {
GetRawContext() json.RawMessage
}
func (d ProvisionDetails) GetRawContext() json.RawMessage {
return d.RawContext
}
func (d ProvisionDetails) GetRawParameters() json.RawMessage {
return d.RawParameters
}
func (d BindDetails) GetRawContext() json.RawMessage {
return d.RawContext
}
func (d BindDetails) GetRawParameters() json.RawMessage {
return d.RawParameters
}
func (d UpdateDetails) GetRawParameters() json.RawMessage {
return d.RawParameters
}
type ProvisionDetails struct {
ServiceID string `json:"service_id"`
PlanID string `json:"plan_id"`
OrganizationGUID string `json:"organization_guid"`
SpaceGUID string `json:"space_guid"`
RawContext json.RawMessage `json:"context,omitempty"`
RawParameters json.RawMessage `json:"parameters,omitempty"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info,omitempty"`
}
type ProvisionedServiceSpec struct {
IsAsync bool
DashboardURL string
OperationData string
}
type GetInstanceDetailsSpec struct {
ServiceID string `json:"service_id"`
PlanID string `json:"plan_id"`
DashboardURL string `json:"dashboard_url"`
Parameters interface{} `json:"parameters"`
}
type UnbindSpec struct {
IsAsync bool
OperationData string
}
type BindDetails struct {
AppGUID string `json:"app_guid"`
PlanID string `json:"plan_id"`
ServiceID string `json:"service_id"`
BindResource *BindResource `json:"bind_resource,omitempty"`
RawContext json.RawMessage `json:"context,omitempty"`
RawParameters json.RawMessage `json:"parameters,omitempty"`
}
type BindResource struct {
AppGuid string `json:"app_guid,omitempty"`
SpaceGuid string `json:"space_guid,omitempty"`
Route string `json:"route,omitempty"`
CredentialClientID string `json:"credential_client_id,omitempty"`
}
type UnbindDetails struct {
PlanID string `json:"plan_id"`
ServiceID string `json:"service_id"`
}
type UpdateServiceSpec struct {
IsAsync bool
DashboardURL string
OperationData string
}
type DeprovisionServiceSpec struct {
IsAsync bool
OperationData string
}
type DeprovisionDetails struct {
PlanID string `json:"plan_id"`
ServiceID string `json:"service_id"`
}
type UpdateDetails struct {
ServiceID string `json:"service_id"`
PlanID string `json:"plan_id"`
RawParameters json.RawMessage `json:"parameters,omitempty"`
PreviousValues PreviousValues `json:"previous_values"`
RawContext json.RawMessage `json:"context,omitempty"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info,omitempty"`
}
type PreviousValues struct {
PlanID string `json:"plan_id"`
ServiceID string `json:"service_id"`
OrgID string `json:"organization_id"`
SpaceID string `json:"space_id"`
}
type PollDetails struct {
ServiceID string `json:"service_id"`
PlanID string `json:"plan_id"`
OperationData string `json:"operation"`
}
type LastOperation struct {
State LastOperationState
Description string
}
type LastOperationState string
const (
InProgress LastOperationState = "in progress"
Succeeded LastOperationState = "succeeded"
Failed LastOperationState = "failed"
)
type Binding struct {
IsAsync bool `json:"is_async"`
OperationData string `json:"operation_data"`
Credentials interface{} `json:"credentials"`
SyslogDrainURL string `json:"syslog_drain_url"`
RouteServiceURL string `json:"route_service_url"`
VolumeMounts []VolumeMount `json:"volume_mounts"`
}
type GetBindingSpec struct {
Credentials interface{}
SyslogDrainURL string
RouteServiceURL string
VolumeMounts []VolumeMount
Parameters interface{}
}
type VolumeMount struct {
Driver string `json:"driver"`
ContainerDir string `json:"container_dir"`
Mode string `json:"mode"`
DeviceType string `json:"device_type"`
Device SharedDevice `json:"device"`
}
type SharedDevice struct {
VolumeId string `json:"volume_id"`
MountConfig map[string]interface{} `json:"mount_config"`
}
const (
instanceExistsMsg = "instance already exists"
instanceDoesntExistMsg = "instance does not exist"
serviceLimitReachedMsg = "instance limit for this service has been reached"
servicePlanQuotaExceededMsg = "The quota for this service plan has been exceeded. Please contact your Operator for help."
serviceQuotaExceededMsg = "The quota for this service has been exceeded. Please contact your Operator for help."
bindingExistsMsg = "binding already exists"
bindingDoesntExistMsg = "binding does not exist"
bindingNotFoundMsg = "binding cannot be fetched"
asyncRequiredMsg = "This service plan requires client support for asynchronous service operations."
planChangeUnsupportedMsg = "The requested plan migration cannot be performed"
rawInvalidParamsMsg = "The format of the parameters is not valid JSON"
appGuidMissingMsg = "app_guid is a required field but was not provided"
concurrentInstanceAccessMsg = "instance is being updated and cannot be retrieved"
maintenanceInfoConflictMsg = "passed maintenance_info does not match the catalog maintenance_info"
maintenanceInfoNilConflictMsg = "maintenance_info was passed, but the broker catalog contains no maintenance_info"
)
var (
ErrInstanceAlreadyExists = NewFailureResponseBuilder(
errors.New(instanceExistsMsg), http.StatusConflict, instanceAlreadyExistsErrorKey,
).WithEmptyResponse().Build()
ErrInstanceDoesNotExist = NewFailureResponseBuilder(
errors.New(instanceDoesntExistMsg), http.StatusGone, instanceMissingErrorKey,
).WithEmptyResponse().Build()
ErrInstanceLimitMet = NewFailureResponse(
errors.New(serviceLimitReachedMsg), http.StatusInternalServerError, instanceLimitReachedErrorKey,
)
ErrBindingAlreadyExists = NewFailureResponse(
errors.New(bindingExistsMsg), http.StatusConflict, bindingAlreadyExistsErrorKey,
)
ErrBindingDoesNotExist = NewFailureResponseBuilder(
errors.New(bindingDoesntExistMsg), http.StatusGone, bindingMissingErrorKey,
).WithEmptyResponse().Build()
ErrBindingNotFound = NewFailureResponseBuilder(
errors.New(bindingNotFoundMsg), http.StatusNotFound, bindingNotFoundErrorKey,
).WithEmptyResponse().Build()
ErrAsyncRequired = NewFailureResponseBuilder(
errors.New(asyncRequiredMsg), http.StatusUnprocessableEntity, asyncRequiredKey,
).WithErrorKey("AsyncRequired").Build()
ErrPlanChangeNotSupported = NewFailureResponseBuilder(
errors.New(planChangeUnsupportedMsg), http.StatusUnprocessableEntity, planChangeNotSupportedKey,
).WithErrorKey("PlanChangeNotSupported").Build()
ErrRawParamsInvalid = NewFailureResponse(
errors.New(rawInvalidParamsMsg), http.StatusUnprocessableEntity, invalidRawParamsKey,
)
ErrAppGuidNotProvided = NewFailureResponse(
errors.New(appGuidMissingMsg), http.StatusUnprocessableEntity, appGuidNotProvidedErrorKey,
)
ErrPlanQuotaExceeded = errors.New(servicePlanQuotaExceededMsg)
ErrServiceQuotaExceeded = errors.New(serviceQuotaExceededMsg)
ErrConcurrentInstanceAccess = NewFailureResponseBuilder(
errors.New(concurrentInstanceAccessMsg), http.StatusUnprocessableEntity, concurrentAccessKey,
).WithErrorKey("ConcurrencyError")
ErrMaintenanceInfoConflict = NewFailureResponseBuilder(
errors.New(maintenanceInfoConflictMsg), http.StatusUnprocessableEntity, maintenanceInfoConflictKey,
).WithErrorKey("MaintenanceInfoConflict").Build()
ErrMaintenanceInfoNilConflict = NewFailureResponseBuilder(
errors.New(maintenanceInfoNilConflictMsg), http.StatusUnprocessableEntity, maintenanceInfoConflictKey,
).WithErrorKey("MaintenanceInfoConflict").Build()
)