-
Notifications
You must be signed in to change notification settings - Fork 15
/
manager.go
195 lines (168 loc) · 5.76 KB
/
manager.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
package main
import (
"net/http"
"github.com/18F/aws-broker/base"
"github.com/18F/aws-broker/catalog"
"github.com/18F/aws-broker/config"
"github.com/18F/aws-broker/helpers/request"
"github.com/18F/aws-broker/helpers/response"
"github.com/18F/aws-broker/services/elasticsearch"
"github.com/18F/aws-broker/services/rds"
"github.com/18F/aws-broker/services/redis"
"github.com/18F/aws-broker/taskqueue"
brokertags "github.com/cloud-gov/go-broker-tags"
"github.com/jinzhu/gorm"
)
type mockTagGenerator struct {
tags map[string]string
}
func (mt *mockTagGenerator) GenerateTags(
action brokertags.Action,
serviceName string,
servicePlanName string,
resourceGUIDs brokertags.ResourceGUIDs,
getMissingResources bool,
) (map[string]string, error) {
return mt.tags, nil
}
func findBroker(serviceID string, c *catalog.Catalog, brokerDb *gorm.DB, settings *config.Settings, taskqueue *taskqueue.QueueManager) (base.Broker, response.Response) {
var tagManager brokertags.TagManager
if settings.Environment == "test" {
tagManager = &mockTagGenerator{}
} else {
var err error
tagManager, err = brokertags.NewCFTagManager(
"AWS broker",
settings.Environment,
settings.CfApiUrl,
settings.CfApiClientId,
settings.CfApiClientSecret,
)
if err != nil {
return nil, response.NewErrorResponse(http.StatusInternalServerError, err.Error())
}
}
switch serviceID {
// RDS Service
case c.RdsService.ID:
return rds.InitRDSBroker(brokerDb, settings, tagManager), nil
case c.RedisService.ID:
return redis.InitRedisBroker(brokerDb, settings, tagManager), nil
case c.ElasticsearchService.ID:
broker, err := elasticsearch.InitElasticsearchBroker(brokerDb, settings, taskqueue, tagManager)
if err != nil {
return nil, response.NewErrorResponse(http.StatusInternalServerError, err.Error())
}
return broker, nil
}
return nil, response.NewErrorResponse(http.StatusNotFound, catalog.ErrNoServiceFound.Error())
}
func createInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings, taskqueue *taskqueue.QueueManager) response.Response {
createRequest, err := request.ExtractRequest(req)
if err != nil {
return err
}
broker, err := findBroker(createRequest.ServiceID, c, brokerDb, settings, taskqueue)
if err != nil {
return err
}
asyncAllowed := req.FormValue("accepts_incomplete") == "true"
if !asyncAllowed {
return response.ErrUnprocessableEntityResponse
}
// Create instance
resp := broker.CreateInstance(c, id, createRequest)
if resp.GetResponseType() != response.ErrorResponseType {
instance := base.Instance{Uuid: id, Request: createRequest}
brokerDb.NewRecord(instance)
err := brokerDb.Create(&instance).Error
if err != nil {
return response.NewErrorResponse(http.StatusBadRequest, err.Error())
}
}
return resp
}
func modifyInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings, taskqueue *taskqueue.QueueManager) response.Response {
// Extract the request information.
modifyRequest, err := request.ExtractRequest(req)
if err != nil {
return err
}
// Find the requested instance in the broker.
instance, err := base.FindBaseInstance(brokerDb, id)
if err != nil {
return err
}
// Retrieve the correct broker.
broker, err := findBroker(instance.ServiceID, c, brokerDb, settings, taskqueue)
if err != nil {
return err
}
// Check if async calls are allowed.
asyncAllowed := req.FormValue("accepts_incomplete") == "true"
if !asyncAllowed {
return response.ErrUnprocessableEntityResponse
}
// Attempt to modify the database instance.
resp := broker.ModifyInstance(c, id, modifyRequest, instance)
if resp.GetResponseType() != response.ErrorResponseType {
err := brokerDb.Save(&instance).Error
if err != nil {
return response.NewErrorResponse(http.StatusBadRequest, err.Error())
}
}
return resp
}
func lastOperation(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings, taskqueue *taskqueue.QueueManager) response.Response {
instance, resp := base.FindBaseInstance(brokerDb, id)
if resp != nil {
return resp
}
broker, resp := findBroker(instance.ServiceID, c, brokerDb, settings, taskqueue)
if resp != nil {
return resp
}
// pass in the operation parameter from request
operation := req.URL.Query().Get("operation")
return broker.LastOperation(c, id, instance, operation)
}
func bindInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings, taskqueue *taskqueue.QueueManager) response.Response {
// Extract the request information.
bindRequest, err := request.ExtractRequest(req)
if err != nil {
return err
}
instance, resp := base.FindBaseInstance(brokerDb, id)
if resp != nil {
return resp
}
broker, resp := findBroker(instance.ServiceID, c, brokerDb, settings, taskqueue)
if resp != nil {
return resp
}
return broker.BindInstance(c, id, bindRequest, instance)
}
func deleteInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings, taskqueue *taskqueue.QueueManager) response.Response {
instance, resp := base.FindBaseInstance(brokerDb, id)
if resp != nil {
return resp
}
broker, resp := findBroker(instance.ServiceID, c, brokerDb, settings, taskqueue)
if resp != nil {
return resp
}
if broker.AsyncOperationRequired(c, instance, base.DeleteOp) {
// Check if async calls are allowed.
asyncAllowed := req.FormValue("accepts_incomplete") == "true"
if !asyncAllowed {
return response.ErrUnprocessableEntityResponse
}
}
resp = broker.DeleteInstance(c, id, instance)
//only delete from DB if it was a sync delete and succeeded
if resp.GetResponseType() == response.SuccessDeleteResponseType {
brokerDb.Unscoped().Delete(&instance)
// TODO check delete error
}
return resp
}