Skip to content

Commit 674193f

Browse files
authored
Add support for default parameter groups (#3)
* add support for default parameter groups * update deps * bump Buffalo version * fixup
1 parent 6bc3a2e commit 674193f

File tree

9 files changed

+208
-109
lines changed

9 files changed

+208
-109
lines changed

Gopkg.lock

+111-98
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ This API provides simple restful API access to Amazon's RDS service.
66

77
This API uses the standard format for input and output of parameters as defined by the AWS SDK (for reference see https://docs.aws.amazon.com/sdk-for-go/api/service/rds/).
88

9+
### Config
10+
911
You can define multiple _accounts_ in your `config.json` file which are mapped to endpoints by the API and allow RDS instances to be created in different AWS accounts. See [example config](config/config.example.json)
1012

13+
In each account you can optionally define certain defaults that will be used if those parameters are not specified in the POST request when creating a database:
14+
- `defaultSubnetGroup` - the subnet group that will be used if one is not given
15+
- `defaultDBParameterGroupName` - map of ParameterGroupFamily to ParameterGroupName's
16+
- `defaultDBClusterParameterGroupName` - map of ParameterGroupFamily to ClusterParameterGroupName's
17+
18+
_Note that any default parameters need to refer to existing resources (groups), i.e. they need to be created separately outside of this API._
19+
1120
### Creating a database
1221

1322
You can specify both database cluster and instance information in the POST to create just an instance or a cluster and a member instance.

actions/databases.go

+36
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,27 @@ func DatabasesPost(c buffalo.Context) error {
163163

164164
// create rds cluster first, if specified
165165
if input.Cluster != nil {
166+
// set default subnet group
166167
if input.Cluster.DBSubnetGroupName == nil {
167168
input.Cluster.DBSubnetGroupName = aws.String(rdsClient.DefaultSubnetGroup)
168169
}
170+
// set default cluster parameter group
171+
if input.Cluster.DBClusterParameterGroupName == nil {
172+
pgFamily, pgErr := rdsClient.DetermineParameterGroupFamily(input.Cluster.Engine, input.Cluster.EngineVersion)
173+
if pgErr != nil {
174+
log.Println(pgErr.Error())
175+
return c.Error(400, pgErr)
176+
}
177+
log.Println("Determined ParameterGroupFamily based on Engine:", pgFamily)
178+
cPg, ok := rdsClient.DefaultDBClusterParameterGroupName[pgFamily]
179+
if !ok {
180+
log.Println("No matching DefaultDBClusterParameterGroupName found in config, using AWS default PG")
181+
} else {
182+
log.Println("Using DefaultDBClusterParameterGroupName:", cPg)
183+
input.Cluster.DBClusterParameterGroupName = aws.String(cPg)
184+
}
185+
}
186+
169187
if clusterOutput, err = rdsClient.Service.CreateDBClusterWithContext(c, input.Cluster); err != nil {
170188
log.Println(err.Error())
171189
if aerr, ok := err.(awserr.Error); ok {
@@ -177,9 +195,27 @@ func DatabasesPost(c buffalo.Context) error {
177195
}
178196

179197
// create rds instance
198+
// set default subnet group
180199
if input.Instance.DBSubnetGroupName == nil {
181200
input.Instance.DBSubnetGroupName = aws.String(rdsClient.DefaultSubnetGroup)
182201
}
202+
// set default parameter group
203+
if input.Instance.DBParameterGroupName == nil {
204+
pgFamily, pgErr := rdsClient.DetermineParameterGroupFamily(input.Instance.Engine, input.Instance.EngineVersion)
205+
if pgErr != nil {
206+
log.Println(pgErr.Error())
207+
return c.Error(400, pgErr)
208+
}
209+
log.Println("Determined ParameterGroupFamily based on Engine:", pgFamily)
210+
pg, ok := rdsClient.DefaultDBParameterGroupName[pgFamily]
211+
if !ok {
212+
log.Println("No matching DefaultDBParameterGroupName found in config, using AWS default PG")
213+
} else {
214+
log.Println("Using DefaultDBParameterGroupName:", pg)
215+
input.Instance.DBParameterGroupName = aws.String(pg)
216+
}
217+
}
218+
183219
if instanceOutput, err = rdsClient.Service.CreateDBInstanceWithContext(c, input.Instance); err != nil {
184220
log.Println(err.Error())
185221
if input.Cluster != nil {

config/config.example.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
"region": "us-east-1",
1111
"akid": "AKID",
1212
"secret": "SECRET",
13-
"defaultSubnetGroup": "default-subnets"
13+
"defaultSubnetGroup": "default-subnets",
14+
"defaultDBParameterGroupName": {
15+
"postgres9.6": "my-postgres96",
16+
"postgres10": "my-postgres10"
17+
},
18+
"defaultDBClusterParameterGroupName": {
19+
"aurora5.6": "my-aurora56",
20+
"aurora-mysql5.7": "my-aurora-mysql57"
21+
}
1422
}
1523
},
1624
"token": "TOKEN"

docker/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This is a multi-stage Dockerfile and requires >= Docker 17.05
22
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
3-
FROM gobuffalo/buffalo:v0.13.7 as builder
3+
FROM gobuffalo/buffalo:v0.13.13 as builder
44

55
RUN mkdir -p $GOPATH/src/github.com/YaleSpinup/rds-api
66
WORKDIR $GOPATH/src/github.com/YaleSpinup/rds-api

docker/Dockerfile.local

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This is a multi-stage Dockerfile and requires >= Docker 17.05
22
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
3-
FROM gobuffalo/buffalo:v0.13.7 as builder
3+
FROM gobuffalo/buffalo:v0.13.13 as builder
44

55
RUN mkdir -p $GOPATH/src/github.com/YaleSpinup/rds-api
66
WORKDIR $GOPATH/src/github.com/YaleSpinup/rds-api

pkg/common/config.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ type Config struct {
1818

1919
// Account is the configuration for an individual account
2020
type Account struct {
21-
Region string
22-
Akid string
23-
Secret string
24-
DefaultSubnetGroup string
21+
Region string
22+
Akid string
23+
Secret string
24+
DefaultSubnetGroup string
25+
DefaultDBParameterGroupName map[string]string
26+
DefaultDBClusterParameterGroupName map[string]string
2527
}
2628

2729
// LoadConfig loads the JSON configuration from the specified filename and returns a Config struct

pkg/rds/client.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313

1414
// Client struct contains the initialized RDS service and other RDS-related parameters
1515
type Client struct {
16-
Service rdsiface.RDSAPI
17-
DefaultSubnetGroup string
16+
Service rdsiface.RDSAPI
17+
DefaultSubnetGroup string
18+
DefaultDBParameterGroupName map[string]string
19+
DefaultDBClusterParameterGroupName map[string]string
1820
}
1921

2022
// NewSession creates an AWS session for RDS and returns an RDSClient
@@ -26,7 +28,9 @@ func NewSession(c common.Account) Client {
2628
}))
2729

2830
return Client{
29-
Service: rds.New(sess),
30-
DefaultSubnetGroup: c.DefaultSubnetGroup,
31+
Service: rds.New(sess),
32+
DefaultSubnetGroup: c.DefaultSubnetGroup,
33+
DefaultDBParameterGroupName: c.DefaultDBParameterGroupName,
34+
DefaultDBClusterParameterGroupName: c.DefaultDBClusterParameterGroupName,
3135
}
3236
}

pkg/rds/parameter_groups.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package rds
2+
3+
import (
4+
"errors"
5+
6+
"github.com/aws/aws-sdk-go/service/rds"
7+
)
8+
9+
// DetermineParameterGroupFamily returns the DBParameterGroupFamily based on the
10+
// given database Engine and EngineVersion
11+
// e.g. given engine "postgres" and engineVersion "10.5" it will return "postgres10"
12+
func (cl Client) DetermineParameterGroupFamily(engine, engineVersion *string) (string, error) {
13+
input := &rds.DescribeDBEngineVersionsInput{
14+
Engine: engine,
15+
EngineVersion: engineVersion,
16+
}
17+
18+
evResult, err := cl.Service.DescribeDBEngineVersions(input)
19+
if err != nil {
20+
return "", err
21+
}
22+
if len(evResult.DBEngineVersions) == 0 {
23+
return "", errors.New("Unable to find any matching database engine/version")
24+
}
25+
26+
return *evResult.DBEngineVersions[0].DBParameterGroupFamily, nil
27+
}

0 commit comments

Comments
 (0)