Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(data): rds implementation as DB interface #158

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@ package data
import (
"fmt"
"log"
"os"
"strconv"

"github.com/jinzhu/gorm"
)

const (
dBInstanceKey = "WORKFLOW_MANAGER_API_DBINSTANCE"
dBUserKey = "WORKFLOW_MANAGER_API_DBUSER"
dBPassKey = "WORKFLOW_MANAGER_API_DBPASS"
)

var (
dBInstance = os.Getenv(dBInstanceKey)
dBUser = os.Getenv(dBUserKey)
dBPass = os.Getenv(dBPassKey)
)

type errNoMoreRows struct {
tableName string
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/data/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package data

import (
"github.com/jinzhu/gorm"
)

// DB is an interface for managing a database
type DB interface {
Get() (*gorm.DB, error)
}
Copy link
Member

@arschles arschles Jun 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this interface. We used to have it, but removed it because there was no need for a factory to create multiple *gorm.DBs - just passing one around worked just fine. Is there a reason for adding this that I'm missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a commit that should answer that question. The interface will make it easier for us to slot in alternate (to RDS) db implementations, and for testing/mocking as well.

The question is: is this an effective approach toward the objectives outlined in #141?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This abstraction will solve #141, but why not just pass around a *sql.DB? I removed this interface a while back because that abstraction already does everything we need

49 changes: 23 additions & 26 deletions pkg/data/rds_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package data

import (
"log"
"os"
"strconv"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -12,43 +11,41 @@ import (
_ "github.com/lib/pq" // Pure Go Postgres driver for database/sql
)

const (
rDSRegionKey = "WORKFLOW_MANAGER_API_RDS_REGION"
)

var (
rDSRegion = os.Getenv(rDSRegionKey)
)

// NewRDSDB attempts to discover and connect to a postgres database managed by Amazon RDS
func NewRDSDB() (*gorm.DB, error) {
db, err := getRDSDB()
if err != nil {
return nil, err
}
return db, nil
// rdsDB is an implementation of the DB interface
type rdsDB struct {
config *aws.Config
user string
pass string
flavor string
instance *string
}

func getRDSSession() *rds.RDS {
return rds.New(session.New(), &aws.Config{Region: aws.String(rDSRegion)})
// NewRDSDB is a constructor that returns an instance of a DB
// that can connect to an Amazon RDS instance
func NewRDSDB(region string, user string, pass string, flavor string, instance string) DB {
return &rdsDB{
config: &aws.Config{Region: aws.String(region)},
user: user,
pass: pass,
flavor: flavor,
instance: &instance,
}
}

func getRDSDB() (*gorm.DB, error) {
svc := getRDSSession()
dbInstanceIdentifier := new(string)
dbInstanceIdentifier = &dBInstance
params := rds.DescribeDBInstancesInput{DBInstanceIdentifier: dbInstanceIdentifier}
func (r rdsDB) Get() (*gorm.DB, error) {
svc := rds.New(session.New(), r.config)
params := rds.DescribeDBInstancesInput{DBInstanceIdentifier: r.instance}
resp, err := svc.DescribeDBInstances(&params)
if err != nil {
return nil, err
}
if len(resp.DBInstances) > 1 {
log.Printf("more than one database instance returned for %s, using the 1st one", dBInstance)
log.Printf("more than one database instance returned for %s, using the 1st one", *r.instance)
}
instance := resp.DBInstances[0]
url := *instance.Endpoint.Address + ":" + strconv.FormatInt(*instance.Endpoint.Port, 10)
dataSourceName := "postgres://" + dBUser + ":" + dBPass + "@" + url + "/" + *instance.DBName + "?sslmode=require"
db, err := gorm.Open("postgres", dataSourceName)
dataSourceName := r.flavor + "://" + r.user + ":" + r.pass + "@" + url + "/" + *instance.DBName + "?sslmode=require"
db, err := gorm.Open(r.flavor, dataSourceName)
if err != nil {
log.Println("couldn't get a db connection!")
return nil, err
Expand Down
22 changes: 19 additions & 3 deletions pkg/swagger/restapi/configure_workflow_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package restapi
import (
"log"
"net/http"
"os"

"github.com/deis/workflow-manager-api/pkg/data"
"github.com/deis/workflow-manager-api/pkg/handlers"
Expand All @@ -13,6 +14,21 @@ import (
"github.com/jinzhu/gorm"
)

const (
rDSRegionKey = "WORKFLOW_MANAGER_API_RDS_REGION"
dBUserKey = "WORKFLOW_MANAGER_API_DBUSER"
dBPassKey = "WORKFLOW_MANAGER_API_DBPASS"
dBInstanceKey = "WORKFLOW_MANAGER_API_DBINSTANCE"
dBFlavor = "postgres"
)

var (
rdsRegion = os.Getenv(rDSRegionKey)
dBInstance = os.Getenv(dBInstanceKey)
dBUser = os.Getenv(dBUserKey)
dBPass = os.Getenv(dBPassKey)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably makes more sense to use envconfig or similar here. thoughts?


type GormDb struct {
db *gorm.DB
}
Expand All @@ -28,14 +44,14 @@ func getDb(api *operations.WorkflowManagerAPI) *gorm.DB {
return gormDb.db
}
}
rdsDB, err := data.NewRDSDB()
db, err := data.NewRDSDB(rdsRegion, dBUser, dBPass, dBFlavor, dBInstance).Get()
if err != nil {
log.Fatalf("unable to create connection to RDS DB (%s)", err)
}
if err := data.VerifyPersistentStorage(rdsDB); err != nil {
if err := data.VerifyPersistentStorage(db); err != nil {
log.Fatalf("unable to verify persistent storage\n%s", err)
}
return rdsDB
return db
}

func configureFlags(api *operations.WorkflowManagerAPI) {
Expand Down