forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
98 lines (81 loc) · 2.05 KB
/
settings.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
package main
import (
"github.com/jinzhu/gorm"
"encoding/json"
"errors"
"log"
"os"
"strconv"
)
type Settings struct {
EncryptionKey string
DbConfig *DBConfig
InstanceTags map[string]string
Environment string
SecGroup string
SubnetGroup string
}
// Main function to create database instances
func (s Settings) InitializeAdapter(plan *Plan,
sharedDbConn *gorm.DB) (DBAdapter, error) {
var dbAdapter DBAdapter
// For test environments, use a mock adapter.
if s.Environment == "test" {
dbAdapter = &MockDBAdapter{}
return dbAdapter, nil
}
switch plan.Adapter {
case "shared":
dbAdapter = &SharedDBAdapter{
SharedDbConn: sharedDbConn,
}
case "dedicated":
dbAdapter = &DedicatedDBAdapter{
InstanceType: plan.InstanceType,
}
default:
return nil, errors.New("Adapter not found")
}
return dbAdapter, nil
}
// Load settings from environment variables
func (s *Settings) LoadFromEnv() error {
log.Println("Loading settings")
// Load DB Settings
dbConfig := DBConfig{}
dbConfig.DbType = os.Getenv("DB_TYPE")
dbConfig.Url = os.Getenv("DB_URL")
dbConfig.Username = os.Getenv("DB_USER")
dbConfig.Password = os.Getenv("DB_PASS")
dbConfig.DbName = os.Getenv("DB_NAME")
if dbConfig.Sslmode = os.Getenv("DB_SSLMODE"); dbConfig.Sslmode == "" {
dbConfig.Sslmode = "require"
}
if os.Getenv("DB_PORT") != "" {
var err error
dbConfig.Port, err = strconv.ParseInt(os.Getenv("DB_PORT"), 10, 64)
// Just return nothing if we can't interpret the number.
if err != nil {
return errors.New("Couldn't load port number")
}
} else {
dbConfig.Port = 5432
}
s.DbConfig = &dbConfig
// Load Encryption Key
s.EncryptionKey = os.Getenv("ENC_KEY")
if s.EncryptionKey == "" {
return errors.New("An encryption key is required")
}
// Load tags
tags := os.Getenv("INSTANCE_TAGS")
if tags != "" {
json.Unmarshal([]byte(tags), &s.InstanceTags)
}
// Load AWS settings
s.SecGroup = os.Getenv("AWS_SEC_GROUP")
s.SubnetGroup = os.Getenv("AWS_DB_SUBNET_GROUP")
// Set env to production
s.Environment = "production"
return nil
}