forked from cloud-gov/aws-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
74 lines (65 loc) · 2.14 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
package main
import (
"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/rds"
"github.com/jinzhu/gorm"
"net/http"
)
func findBroker(serviceID string, c *catalog.Catalog, brokerDb *gorm.DB, settings *config.Settings) (base.Broker, response.Response) {
switch serviceID {
// RDS Service
case c.RdsService.ID:
return rds.InitRDSBroker(brokerDb, settings), 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) response.Response {
createRequest, resp := request.ExtractRequest(req)
if resp != nil {
return resp
}
broker, resp := findBroker(createRequest.ServiceID, c, brokerDb, settings)
if resp != nil {
return resp
}
// Create instance
resp = broker.CreateInstance(c, id, createRequest)
if resp.GetResponseType() != response.ErrorResponseType {
instance := base.Instance{Uuid: id, Request: createRequest}
brokerDb.NewRecord(instance)
brokerDb.Create(&instance)
// TODO check save error
}
return resp
}
func bindInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings) response.Response {
instance, resp := base.FindBaseInstance(brokerDb, id)
if resp != nil {
return resp
}
broker, resp := findBroker(instance.ServiceID, c, brokerDb, settings)
if resp != nil {
return resp
}
return broker.BindInstance(c, id, instance)
}
func deleteInstance(req *http.Request, c *catalog.Catalog, brokerDb *gorm.DB, id string, settings *config.Settings) response.Response {
instance, resp := base.FindBaseInstance(brokerDb, id)
if resp != nil {
return resp
}
broker, resp := findBroker(instance.ServiceID, c, brokerDb, settings)
if resp != nil {
return resp
}
resp = broker.DeleteInstance(c, id, instance)
if resp.GetResponseType() != response.ErrorResponseType {
brokerDb.Unscoped().Delete(&instance)
// TODO check delete error
}
return resp
}