Skip to content

Commit

Permalink
[CHP-3104] - Enable db client to accept options (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
wei-intuit authored May 10, 2024
1 parent b497c8b commit 1ce3c8e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
5 changes: 4 additions & 1 deletion service/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,21 @@ type SQLClient struct {
database string
user string
password string
options map[string]string
}

const (
ProjectEntryDB = "projects"
TokenEntryDB = "tokens"
)

func NewSQLClient(host, database, user, password string) (SQLClient, error) {
func NewSQLClient(host, database, user, password string, options map[string]string) (SQLClient, error) {
return SQLClient{
host: host,
database: database,
user: user,
password: password,
options: options,
}, nil
}

Expand All @@ -68,6 +70,7 @@ func (d SQLClient) createSession() (db.Session, error) {
Database: d.database,
User: d.user,
Password: d.password,
Options: d.options,
}

return postgresql.Open(settings)
Expand Down
1 change: 1 addition & 0 deletions service/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Vars struct {
DBUser string `split_words:"true" required:"true"`
DBPassword string `split_words:"true" required:"true"`
DBName string `split_words:"true" required:"true"`
DBOptions string `split_words:"true"`
ImageURIs []string `envconfig:"IMAGE_URIS"`
}

Expand Down
2 changes: 2 additions & 0 deletions service/internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var prefixedEnvVars = map[string]string{
"_DB_NAME": "argocloudops",
"_DB_USER": "argoco",
"_DB_PASSWORD": "1234",
"_DB_OPTIONS": "sslrootcert=rds-ca.pem sslmode=verify-full",
}

var nonPrefixedEnvVars = map[string]string{
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestGetEnv(t *testing.T) {
assert.Equal(t, "argocloudops", vars.DBName)
assert.Equal(t, "argoco", vars.DBUser)
assert.Equal(t, "1234", vars.DBPassword)
assert.Equal(t, "sslrootcert=rds-ca.pem sslmode=verify-full", vars.DBOptions)
}

func TestDefaults(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cello-proj/cello/service/internal/env"
"github.com/cello-proj/cello/service/internal/git"
"github.com/cello-proj/cello/service/internal/workflow"
"github.com/cello-proj/cello/service/util"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/go-kit/log"
Expand Down Expand Up @@ -54,7 +55,7 @@ func main() {
// The Argo context is needed for any Argo client method calls or else, nil errors.
argoCtx, argoClient := client.NewAPIClient()

dbClient, err := db.NewSQLClient(env.DBHost, env.DBName, env.DBUser, env.DBPassword)
dbClient, err := db.NewSQLClient(env.DBHost, env.DBName, env.DBUser, env.DBPassword, util.OptionsToMap(env.DBOptions))
if err != nil {
level.Error(errLogger).Log("message", "error creating db client", "error", err)
os.Exit(1)
Expand Down
18 changes: 18 additions & 0 deletions service/util/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package util

import "strings"

func OptionsToMap(options string) map[string]string {
opts := map[string]string{}
options = strings.TrimSpace(options)

if options != "" {
kvPairs := strings.Split(options, " ")
for _, entry := range kvPairs {
kv := strings.SplitN(entry, "=", 2)
opts[kv[0]] = kv[1]
}
}

return opts
}
45 changes: 45 additions & 0 deletions service/util/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestOptionsToMap(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]string
}{
{
name: "input is not empty string",
input: "sslrootcert=rds-ca.pem sslmode=verify-full",
expected: map[string]string{
"sslrootcert": "rds-ca.pem",
"sslmode": "verify-full",
},
},
{
name: "input is empty string",
input: "",
expected: map[string]string{},
},
{
name: "option has multiple equal signs",
input: "sslrootcert=rds-ca.pem sslmode=verify-full option=value=value",
expected: map[string]string{
"sslrootcert": "rds-ca.pem",
"sslmode": "verify-full",
"option": "value=value",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := OptionsToMap(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}

0 comments on commit 1ce3c8e

Please sign in to comment.