-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Aurora MySQL binding to reader endpoint (#550)
Aurora has the concept of Writer nodes and Reader nodes. Reader nodes are useful for High Availability, but they can also be used for read-only workloads. This features exposes the ability to bind to the reader endpoint for read-only workloads. This is achieved by passing the parameters '{"reader":true}' to the 'cf bind-service' command. [#183390030](https://www.pivotaltracker.com/story/show/183390030) Co-authored-by: Felisia Martini <fmartini@pivotal.io>
- Loading branch information
Showing
14 changed files
with
274 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
acceptance-tests/apps/mysqlapp/internal/connector/connect.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package connector | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type option func(*Connector, *mysql.Config) error | ||
|
||
func (c *Connector) Connect(opts ...option) (*sql.DB, error) { | ||
cfg := mysql.NewConfig() | ||
cfg.Net = "tcp" | ||
cfg.Addr = c.Host | ||
cfg.User = c.Username | ||
cfg.Passwd = c.Password | ||
cfg.DBName = c.Database | ||
withDefaults(opts...)(c, cfg) | ||
|
||
db, err := sql.Open("mysql", cfg.FormatDSN()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to database: %s", err) | ||
} | ||
db.SetConnMaxLifetime(time.Minute * 3) | ||
db.SetMaxOpenConns(10) | ||
db.SetMaxIdleConns(10) | ||
|
||
return db, nil | ||
} | ||
|
||
func withOptions(opts ...option) option { | ||
return func(conn *Connector, cfg *mysql.Config) error { | ||
for _, o := range opts { | ||
if err := o(conn, cfg); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func withDefaults(opts ...option) option { | ||
return withOptions(append([]option{withTLS()}, opts...)...) | ||
} | ||
|
||
func withTLS() option { | ||
return func(_ *Connector, cfg *mysql.Config) error { | ||
cfg.TLSConfig = "true" | ||
return nil | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
acceptance-tests/apps/mysqlapp/internal/connector/connector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package connector | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cloudfoundry-community/go-cfenv" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type Connector struct { | ||
Host string `mapstructure:"hostname"` | ||
Database string `mapstructure:"name"` | ||
Username string `mapstructure:"username"` | ||
Password string `mapstructure:"password"` | ||
Port int `mapstructure:"port"` | ||
} | ||
|
||
func New() (*Connector, error) { | ||
app, err := cfenv.Current() | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading app env: %w", err) | ||
} | ||
svs, err := app.Services.WithTag("mysql") | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading MySQL service details") | ||
} | ||
|
||
var c Connector | ||
if err := mapstructure.Decode(svs[0].Credentials, &c); err != nil { | ||
return nil, fmt.Errorf("failed to decode credentials: %w", err) | ||
} | ||
|
||
if err := c.Valid(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &c, nil | ||
} | ||
|
||
func (c *Connector) Valid() error { | ||
switch { | ||
case c.Host == "": | ||
return fmt.Errorf("missing hostname") | ||
case c.Username == "": | ||
return fmt.Errorf("missing username") | ||
case c.Password == "": | ||
return fmt.Errorf("missing password") | ||
case c.Database == "": | ||
return fmt.Errorf("missing database name") | ||
case c.Port == 0: | ||
return fmt.Errorf("missing port") | ||
} | ||
|
||
return nil | ||
} |
46 changes: 0 additions & 46 deletions
46
acceptance-tests/apps/mysqlapp/internal/credentials/credentials.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.