forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrds.go
239 lines (211 loc) · 7.66 KB
/
rds.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
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/jinzhu/gorm"
"errors"
"fmt"
"log"
)
type DBInstanceState uint8
const (
InstanceNotCreated DBInstanceState = iota // 0
InstanceInProgress // 1
InstanceReady // 2
InstanceGone // 3
InstanceNotGone // 4
)
type DBAdapter interface {
CreateDB(i *Instance, password string) (DBInstanceState, error)
BindDBToApp(i *Instance, password string) (map[string]string, error)
DeleteDB(i *Instance) (DBInstanceState, error)
}
// MockDBAdapter is a struct meant for testing.
// It should only be used in *_test.go files.
// It is only here because *_test.go files are only compiled during "go test"
// and it's referenced in non *_test.go code eg. InitializeAdapter in main.go.
type MockDBAdapter struct {
}
func (d *MockDBAdapter) CreateDB(i *Instance, password string) (DBInstanceState, error) {
// TODO
return InstanceReady, nil
}
func (d *MockDBAdapter) BindDBToApp(i *Instance, password string) (map[string]string, error) {
// TODO
return i.GetCredentials(password)
}
func (d *MockDBAdapter) DeleteDB(i *Instance) (DBInstanceState, error) {
// TODO
return InstanceGone, nil
}
// END MockDBAdpater
type SharedDBAdapter struct {
SharedDbConn *gorm.DB
}
func (d *SharedDBAdapter) CreateDB(i *Instance, password string) (DBInstanceState, error) {
if db := d.SharedDbConn.Exec(fmt.Sprintf("CREATE DATABASE %s;", i.Database)); db.Error != nil {
return InstanceNotCreated, db.Error
}
if db := d.SharedDbConn.Exec(fmt.Sprintf("CREATE USER %s WITH PASSWORD '%s';", i.Username, password)); db.Error != nil {
// TODO. Revert CREATE DATABASE.
return InstanceNotCreated, db.Error
}
if db := d.SharedDbConn.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE %s TO %s", i.Database, i.Username)); db.Error != nil {
// TODO. Revert CREATE DATABASE and CREATE USER.
return InstanceNotCreated, db.Error
}
return InstanceReady, nil
}
func (d *SharedDBAdapter) BindDBToApp(i *Instance, password string) (map[string]string, error) {
return i.GetCredentials(password)
}
func (d *SharedDBAdapter) DeleteDB(i *Instance) (DBInstanceState, error) {
if db := d.SharedDbConn.Exec(fmt.Sprintf("DROP DATABASE %s;", i.Database)); db.Error != nil {
return InstanceNotGone, db.Error
}
if db := d.SharedDbConn.Exec(fmt.Sprintf("DROP USER %s;", i.Username)); db.Error != nil {
return InstanceNotGone, db.Error
}
return InstanceGone, nil
}
type DedicatedDBAdapter struct {
InstanceType string
}
func (d *DedicatedDBAdapter) CreateDB(i *Instance, password string) (DBInstanceState, error) {
svc := rds.New(&aws.Config{Region: "us-east-1"})
var rdsTags []*rds.Tag
for k, v := range i.Tags {
var tag rds.Tag
tag = rds.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
rdsTags = append(rdsTags, &tag)
}
// Standard parameters
params := &rds.CreateDBInstanceInput{
// Everyone gets 10gb for now
AllocatedStorage: aws.Long(10),
// Instance class is defined by the plan
DBInstanceClass: &d.InstanceType,
DBInstanceIdentifier: &i.Database,
DBName: &i.Database,
Engine: aws.String("postgres"),
MasterUserPassword: &password,
MasterUsername: &i.Username,
AutoMinorVersionUpgrade: aws.Boolean(true),
MultiAZ: aws.Boolean(true),
StorageEncrypted: aws.Boolean(true),
Tags: rdsTags,
PubliclyAccessible: aws.Boolean(false),
DBSubnetGroupName: &i.DbSubnetGroup,
VPCSecurityGroupIDs: []*string{&i.SecGroup},
}
if *params.DBInstanceClass == "db.t2.micro" {
params.StorageEncrypted = aws.Boolean(false)
}
resp, err := svc.CreateDBInstance(params)
// Pretty-print the response data.
log.Println(awsutil.StringValue(resp))
// Decide if AWS service call was successful
if yes := d.DidAwsCallSucceed(err); yes {
return InstanceInProgress, nil
} else {
return InstanceNotCreated, nil
}
}
func (d *DedicatedDBAdapter) BindDBToApp(i *Instance, password string) (map[string]string, error) {
// First, we need to check if the instance is up and available before binding.
// Only search for details if the instance was not indicated as ready.
if i.State != InstanceReady {
svc := rds.New(&aws.Config{Region: "us-east-1"})
params := &rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(i.Database),
// MaxRecords: aws.Long(1),
}
resp, err := svc.DescribeDBInstances(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Generic AWS error with Code, Message, and original error (if any)
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
fmt.Println(err.Error())
}
return nil, err
}
// Pretty-print the response data.
fmt.Println(awsutil.StringValue(resp))
// Get the details (host and port) for the instance.
numOfInstances := len(resp.DBInstances)
if numOfInstances > 0 {
for _, value := range resp.DBInstances {
// First check that the instance is up.
if value.DBInstanceStatus != nil && *(value.DBInstanceStatus) == "available" {
if value.Endpoint != nil && value.Endpoint.Address != nil && value.Endpoint.Port != nil {
fmt.Printf("host: %s port: %d \n", *(value.Endpoint.Address), *(value.Endpoint.Port))
i.Port = *(value.Endpoint.Port)
i.Host = *(value.Endpoint.Address)
i.State = InstanceReady
// Should only be one regardless. Just return now.
break
} else {
// Something went horribly wrong. Should never get here.
return nil, errors.New("Inavlid memory for endpoint and/or endpoint members.")
}
} else {
// Instance not up yet.
return nil, errors.New("Instance not available yet. Please wait and try again..")
}
}
} else {
// Couldn't find any instances.
return nil, errors.New("Couldn't find any instances.")
}
}
// If we get here that means the instance is up and we have the information for it.
return i.GetCredentials(password)
}
func (d *DedicatedDBAdapter) DeleteDB(i *Instance) (DBInstanceState, error) {
svc := rds.New(&aws.Config{Region: "us-east-1"})
params := &rds.DeleteDBInstanceInput{
DBInstanceIdentifier: aws.String(i.Database), // Required
// FinalDBSnapshotIdentifier: aws.String("String"),
SkipFinalSnapshot: aws.Boolean(true),
}
resp, err := svc.DeleteDBInstance(params)
// Pretty-print the response data.
fmt.Println(awsutil.StringValue(resp))
// Decide if AWS service call was successful
if yes := d.DidAwsCallSucceed(err); yes {
return InstanceGone, nil
} else {
return InstanceNotGone, nil
}
}
func (d *DedicatedDBAdapter) DidAwsCallSucceed(err error) bool {
// TODO Eventually return a formatted error object.
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Generic AWS Error with Code, Message, and original error (if any)
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
// This case should never be hit, The SDK should alwsy return an
// error which satisfies the awserr.Error interface.
fmt.Println(err.Error())
}
return false
}
return true
}