forked from cloudfoundry-samples/go_service_broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
263 lines (211 loc) · 7.07 KB
/
controller.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
package web_server
import (
"errors"
"fmt"
"net/http"
client "github.com/cloudfoundry-samples/go_service_broker/client"
utils "github.com/cloudfoundry-samples/go_service_broker/utils"
model "github.com/cloudfoundry-samples/go_service_broker/model"
)
const (
DEFAULT_POLLING_INTERVAL_SECONDS = 10
)
type Controller struct {
cloudName string
cloudClient client.Client
instanceMap map[string]*model.ServiceInstance
bindingMap map[string]*model.ServiceBinding
}
func CreateController(cloudName string, instanceMap map[string]*model.ServiceInstance, bindingMap map[string]*model.ServiceBinding) (*Controller, error) {
cloudClient, err := createCloudClient(cloudName)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create cloud: %s client, message: %s", cloudName, err.Error()))
}
return &Controller{
cloudName: cloudName,
cloudClient: cloudClient,
instanceMap: instanceMap,
bindingMap: bindingMap,
}, nil
}
func (c *Controller) Catalog(w http.ResponseWriter, r *http.Request) {
fmt.Println("Get Service Broker Catalog...")
var catalog model.Catalog
catalogFileName := "catalog.json"
if c.cloudName == utils.AWS {
catalogFileName = "catalog.AWS.json"
} else if c.cloudName == utils.SOFTLAYER || c.cloudName == utils.SL {
catalogFileName = "catalog.SoftLayer.json"
}
err := utils.ReadAndUnmarshal(&catalog, conf.CatalogPath, catalogFileName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
utils.WriteResponse(w, http.StatusOK, catalog)
}
func (c *Controller) CreateServiceInstance(w http.ResponseWriter, r *http.Request) {
fmt.Println("Create Service Instance...")
var instance model.ServiceInstance
err := utils.ProvisionDataFromRequest(r, &instance)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
instanceId, err := c.cloudClient.CreateInstance(instance.Parameters)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
instance.InternalId = instanceId
instance.DashboardUrl = "http://dashbaord_url"
instance.Id = utils.ExtractVarsFromRequest(r, "service_instance_guid")
instance.LastOperation = &model.LastOperation{
State: "in progress",
Description: "creating service instance...",
AsyncPollIntervalSeconds: DEFAULT_POLLING_INTERVAL_SECONDS,
}
c.instanceMap[instance.Id] = &instance
err = utils.MarshalAndRecord(c.instanceMap, conf.DataPath, conf.ServiceInstancesFileName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
response := model.CreateServiceInstanceResponse{
DashboardUrl: instance.DashboardUrl,
LastOperation: instance.LastOperation,
}
utils.WriteResponse(w, http.StatusAccepted, response)
}
func (c *Controller) GetServiceInstance(w http.ResponseWriter, r *http.Request) {
fmt.Println("Get Service Instance State....")
instanceId := utils.ExtractVarsFromRequest(r, "service_instance_guid")
instance := c.instanceMap[instanceId]
if instance == nil {
w.WriteHeader(http.StatusNotFound)
return
}
state, err := c.cloudClient.GetInstanceState(instance.InternalId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if state == "pending" {
instance.LastOperation.State = "in progress"
instance.LastOperation.Description = "creating service instance..."
} else if state == "running" {
instance.LastOperation.State = "succeeded"
instance.LastOperation.Description = "successfully created service instance"
} else {
instance.LastOperation.State = "failed"
instance.LastOperation.Description = "failed to create service instance"
}
response := model.CreateServiceInstanceResponse{
DashboardUrl: instance.DashboardUrl,
LastOperation: instance.LastOperation,
}
utils.WriteResponse(w, http.StatusOK, response)
}
func (c *Controller) RemoveServiceInstance(w http.ResponseWriter, r *http.Request) {
fmt.Println("Remove Service Instance...")
instanceId := utils.ExtractVarsFromRequest(r, "service_instance_guid")
instance := c.instanceMap[instanceId]
if instance == nil {
w.WriteHeader(http.StatusGone)
return
}
err := c.cloudClient.DeleteInstance(instance.InternalId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
delete(c.instanceMap, instanceId)
utils.MarshalAndRecord(c.instanceMap, conf.DataPath, conf.ServiceInstancesFileName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = c.deleteAssociatedBindings(instanceId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
utils.WriteResponse(w, http.StatusOK, "{}")
}
func (c *Controller) Bind(w http.ResponseWriter, r *http.Request) {
fmt.Println("Bind Service Instance...")
bindingId := utils.ExtractVarsFromRequest(r, "service_binding_guid")
instanceId := utils.ExtractVarsFromRequest(r, "service_instance_guid")
instance := c.instanceMap[instanceId]
if instance == nil {
w.WriteHeader(http.StatusNotFound)
return
}
ip, userName, privateKey, err := c.cloudClient.InjectKeyPair(instance.InternalId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
credential := model.Credential{
PublicIP: ip,
UserName: userName,
PrivateKey: privateKey,
}
response := model.CreateServiceBindingResponse{
Credentials: credential,
}
c.bindingMap[bindingId] = &model.ServiceBinding{
Id: bindingId,
ServiceId: instance.ServiceId,
ServicePlanId: instance.PlanId,
PrivateKey: privateKey,
ServiceInstanceId: instance.Id,
}
err = utils.MarshalAndRecord(c.bindingMap, conf.DataPath, conf.ServiceBindingsFileName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
utils.WriteResponse(w, http.StatusCreated, response)
}
func (c *Controller) UnBind(w http.ResponseWriter, r *http.Request) {
fmt.Println("Unbind Service Instance...")
bindingId := utils.ExtractVarsFromRequest(r, "service_binding_guid")
instanceId := utils.ExtractVarsFromRequest(r, "service_instance_guid")
instance := c.instanceMap[instanceId]
if instance == nil {
w.WriteHeader(http.StatusGone)
return
}
err := c.cloudClient.RevokeKeyPair(instance.InternalId, c.bindingMap[bindingId].PrivateKey)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
delete(c.bindingMap, bindingId)
err = utils.MarshalAndRecord(c.bindingMap, conf.DataPath, conf.ServiceBindingsFileName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
utils.WriteResponse(w, http.StatusOK, "{}")
}
// Private instance methods
func (c *Controller) deleteAssociatedBindings(instanceId string) error {
for id, binding := range c.bindingMap {
if binding.ServiceInstanceId == instanceId {
delete(c.bindingMap, id)
}
}
return utils.MarshalAndRecord(c.bindingMap, conf.DataPath, conf.ServiceBindingsFileName)
}
// Private methods
func createCloudClient(cloudName string) (client.Client, error) {
switch cloudName {
case utils.AWS:
return client.NewAWSClient("us-east-1"), nil
case utils.SOFTLAYER, utils.SL:
return client.NewSoftLayerClient(), nil
}
return nil, errors.New(fmt.Sprintf("Invalid cloud name: %s", cloudName))
}