forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
215 lines (182 loc) · 5.85 KB
/
api.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
package main
import (
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
"github.com/martini-contrib/render"
"encoding/json"
"io/ioutil"
"net/http"
)
type Response struct {
Description string `json:"description"`
}
type Operation struct {
State string
Description string
AsyncPollIntervalSeconds int `json:"async_poll_interval_seconds, omitempty"`
}
type CreateResponse struct {
DashboardUrl string
LastOperation Operation
}
type ServiceReq struct {
ServiceId string `json:"service_id"`
PlainId string `json:"plan_id"`
OrganizationGuid string `json:"organization_guid"`
SpaceGuid string `json:"space_guid"`
}
// CreateInstance
// URL: /v2/service_instances/:id
// Request:
// {
// "service_id": "service-guid-here",
// "plan_id": "plan-guid-here",
// "organization_guid": "org-guid-here",
// "space_guid": "space-guid-here"
// }
func CreateInstance(p martini.Params, req *http.Request, r render.Render, brokerDb *gorm.DB, s *Settings) {
instance := Instance{}
brokerDb.Where("uuid = ?", p["id"]).First(&instance)
if instance.Id > 0 {
r.JSON(http.StatusConflict, Response{"The instance already exists"})
return
}
var sr ServiceReq
if req.Body == nil {
r.JSON(http.StatusBadRequest, Response{"No request"})
return
}
body, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(body, &sr)
plan := FindPlan(sr.PlainId)
if plan == nil {
r.JSON(http.StatusBadRequest, Response{"The plan requested does not exist"})
return
}
// Get the correct database logic depending on the type of plan. (shared vs dedicated)
adapter, _ := s.InitializeAdapter(plan, brokerDb)
err := instance.Init(
p["id"],
sr.OrganizationGuid,
sr.SpaceGuid,
plan,
s)
if err != nil {
desc := "There was an error initializing the instance. Error: " + err.Error()
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
// Create the database instance.
status, err := adapter.CreateDB(&instance, instance.ClearPassword)
if status == InstanceNotCreated {
desc := "There was an error creating the instance."
if err != nil {
desc = desc + " Error: " + err.Error()
}
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
instance.State = status
// FIXME
// Currently, if we are dealing with a shared database, it will not populate the host and port fields of the instance.
// Also, currently, the shared database instance just create a new database and user inside the intenral broker database.
// Eventually we want to register a DBConfig or a pool of database connections for the shared instances to get the host and port
// and move the logic of storing it in the instance in the SharedDB's CreateDB.
if instance.Adapter == "shared" {
instance.Host = s.DbConfig.Url
instance.Port = s.DbConfig.Port
}
brokerDb.Save(&instance)
r.JSON(http.StatusCreated, Response{"The instance was created"})
}
// BindInstance
// URL: /v2/service_instances/:instance_id/service_bindings/:binding_id
// Request:
// {
// "plan_id": "plan-guid-here",
// "service_id": "service-guid-here",
// "app_guid": "app-guid-here"
// }
func BindInstance(p martini.Params, r render.Render, brokerDb *gorm.DB, s *Settings) {
instance := Instance{}
brokerDb.Where("uuid = ?", p["instance_id"]).First(&instance)
if instance.Id == 0 {
r.JSON(404, Response{"Instance not found"})
return
}
password, err := instance.GetPassword(s.EncryptionKey)
if err != nil {
r.JSON(http.StatusInternalServerError, "Unable to get instance password.")
}
plan := FindPlan(instance.PlanId)
if plan == nil {
r.JSON(http.StatusBadRequest, Response{"The plan requested does not exist"})
return
}
// Get the correct database logic depending on the type of plan. (shared vs dedicated)
db, err := s.InitializeAdapter(plan, brokerDb)
if err != nil {
desc := "There was an error creating the instance. Error: " + err.Error()
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
var credentials map[string]string
// Bind the database instance to the application.
originalInstanceState := instance.State
if credentials, err = db.BindDBToApp(&instance, password); err != nil {
desc := "There was an error binding the database instance to the application."
if err != nil {
desc = desc + " Error: " + err.Error()
}
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
// If the state of the instance has changed, update it.
if instance.State != originalInstanceState {
brokerDb.Save(&instance)
}
response := map[string]interface{}{
"credentials": credentials,
}
r.JSON(http.StatusCreated, response)
}
// DeleteInstance
// URL: /v2/service_instances/:id
// Request:
// {
// "service_id": "service-id-here"
// "plan_id": "plan-id-here"
// }
func DeleteInstance(p martini.Params, r render.Render, brokerDb *gorm.DB, s *Settings) {
instance := Instance{}
brokerDb.Where("uuid = ?", p["id"]).First(&instance)
if instance.Id == 0 {
r.JSON(http.StatusNotFound, Response{"Instance not found"})
return
}
var plan *Plan
plan = FindPlan(instance.PlanId)
if plan == nil {
r.JSON(http.StatusBadRequest, Response{"The plan requested does not exist"})
return
}
// Get the correct database logic depending on the type of plan. (shared vs dedicated)
db, err := s.InitializeAdapter(plan, brokerDb)
if err != nil {
desc := "There was an error deleting the instance. Error: " + err.Error()
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
var status DBInstanceState
// Delete the database instance.
if status, err = db.DeleteDB(&instance); status == InstanceNotGone {
desc := "There was an error deleting the instance."
if err != nil {
desc = desc + " Error: " + err.Error()
}
r.JSON(http.StatusInternalServerError, Response{desc})
return
}
brokerDb.Delete(&instance)
r.JSON(http.StatusOK, Response{"The instance was deleted"})
}