/
handler.go
133 lines (112 loc) · 4.43 KB
/
handler.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed 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 server
import (
"context"
"github.com/pivotal-cf/brokerapi"
)
// Handler is a fake implementation oif a brokerapi.ServiceBroker. It's useful as a mock
// because it has pre-canned response values for use in testing, and also keeps track of calls
// made to it. Handler is not concurrency-safe
type Handler struct {
Catalog []brokerapi.Service
// Since there are no data passed to catalog calls, this is just the number of calls
// that were made to the catalog endpoint
CatalogRequests int
ProvisionResp brokerapi.ProvisionedServiceSpec
ProvisionRespError error
ProvisionRequests []ProvisionRequest
DeprovisionResp brokerapi.DeprovisionServiceSpec
DeprovisonRespErr error
DeprovisionRequests []DeprovisionRequest
BindResp brokerapi.Binding
BindRespErr error
BindRequests []BindRequest
UnbindRespErr error
UnbindRequests []UnbindRequest
UpdateResp brokerapi.UpdateServiceSpec
UpdateRespErr error
UpdateRequests []UpdateRequest
LastOperationResp brokerapi.LastOperation
LastOperationRespErr error
LastOperationRequests []LastOperationRequest
}
// NewHandler creates a new fake server handler
func NewHandler() *Handler {
return &Handler{}
}
// Services increments h.CatalogRequests and returns h.Catalog
func (h *Handler) Services(ctx context.Context) []brokerapi.Service {
h.CatalogRequests++
return h.Catalog
}
// Provision adds an element to h.ProvisionRequests and returns
// h.ProvisionResp, h.ProvisionRespError
func (h *Handler) Provision(
ctx context.Context,
instanceID string,
details brokerapi.ProvisionDetails,
asyncAllowed bool,
) (brokerapi.ProvisionedServiceSpec, error) {
h.ProvisionRequests = append(h.ProvisionRequests, ProvisionRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.ProvisionResp, h.ProvisionRespError
}
// Deprovision adds an element to h.DeprovisionRequests and returns
// h.DeprovisionResp, h.DeprovisionRespErr
func (h *Handler) Deprovision(context context.Context, instanceID string, details brokerapi.DeprovisionDetails, asyncAllowed bool) (brokerapi.DeprovisionServiceSpec, error) {
h.DeprovisionRequests = append(h.DeprovisionRequests, DeprovisionRequest{
InstanceID: instanceID,
Details: details,
})
return h.DeprovisionResp, h.DeprovisonRespErr
}
// Bind adds an element to h.BindRequqests and returns h.BindResp, h.BindRespErr
func (h *Handler) Bind(context context.Context, instanceID, bindingID string, details brokerapi.BindDetails) (brokerapi.Binding, error) {
h.BindRequests = append(h.BindRequests, BindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.BindResp, h.BindRespErr
}
// Unbind adds an element to h.UnbindRequests and returns h.UnbindRespErr
func (h *Handler) Unbind(context context.Context, instanceID, bindingID string, details brokerapi.UnbindDetails) error {
h.UnbindRequests = append(h.UnbindRequests, UnbindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.UnbindRespErr
}
// Update adds an element to h.UpdateRequests and returns h.UpdateResp, h.UpdateRespErr
func (h *Handler) Update(context context.Context, instanceID string, details brokerapi.UpdateDetails, asyncAllowed bool) (brokerapi.UpdateServiceSpec, error) {
h.UpdateRequests = append(h.UpdateRequests, UpdateRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.UpdateResp, h.UpdateRespErr
}
// LastOperation adds an element to h.LastOperationRequests and returns
// h.LastOperationResp, h.LastOperationRespErr
func (h *Handler) LastOperation(context context.Context, instanceID, operationData string) (brokerapi.LastOperation, error) {
h.LastOperationRequests = append(h.LastOperationRequests, LastOperationRequest{
InstanceID: instanceID,
OperationData: operationData,
})
return h.LastOperationResp, h.LastOperationRespErr
}