forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
105 lines (87 loc) · 2.92 KB
/
config.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
99
100
101
102
103
104
105
package integration
import (
"errors"
"fmt"
"io/ioutil"
"os"
)
type Config struct {
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
GCPServiceAccountKeyPath string
GCPProjectID string
GCPRegion string
GCPZone string
GCPEnvPrefix string
StateFileDir string
StemcellPath string
GardenReleasePath string
ConcourseReleasePath string
ConcourseDeploymentPath string
EnableTerraformFlag bool
}
func LoadConfig() (Config, error) {
config := loadConfigFromEnvVars()
awsCredsValidationError := validateAWSCreds(config)
gcpCredsValidationError := validateGCPCreds(config)
if awsCredsValidationError == nil && gcpCredsValidationError == nil {
return Config{}, errors.New("Multiple IAAS Credentials provided:\n Provide a set of credentials for a single IAAS.")
}
if awsCredsValidationError != nil && gcpCredsValidationError != nil {
return Config{}, fmt.Errorf("Multiple Credential Errors Found: %s\nProvide a full set of credentials for a single IAAS.", gcpCredsValidationError)
}
if config.StateFileDir == "" {
dir, err := ioutil.TempDir("", "")
if err != nil {
return Config{}, err
}
config.StateFileDir = dir
}
return config, nil
}
func validateAWSCreds(config Config) error {
if config.AWSAccessKeyID == "" {
return errors.New("aws access key id is missing")
}
if config.AWSSecretAccessKey == "" {
return errors.New("aws secret access key is missing")
}
if config.AWSRegion == "" {
return errors.New("aws region is missing")
}
return nil
}
func validateGCPCreds(config Config) error {
if config.GCPServiceAccountKeyPath == "" {
return errors.New("gcp service account key path is missing")
}
if config.GCPProjectID == "" {
return errors.New("gcp project id is missing")
}
if config.GCPRegion == "" {
return errors.New("gcp region is missing")
}
if config.GCPZone == "" {
return errors.New("gcp zone is missing")
}
return nil
}
func loadConfigFromEnvVars() Config {
return Config{
AWSAccessKeyID: os.Getenv("BBL_AWS_ACCESS_KEY_ID"),
AWSSecretAccessKey: os.Getenv("BBL_AWS_SECRET_ACCESS_KEY"),
AWSRegion: os.Getenv("BBL_AWS_REGION"),
GCPServiceAccountKeyPath: os.Getenv("BBL_GCP_SERVICE_ACCOUNT_KEY"),
GCPProjectID: os.Getenv("BBL_GCP_PROJECT_ID"),
GCPRegion: os.Getenv("BBL_GCP_REGION"),
GCPZone: os.Getenv("BBL_GCP_ZONE"),
GCPEnvPrefix: os.Getenv("BBL_GCP_ENV_PREFIX"),
StateFileDir: os.Getenv("BBL_STATE_DIR"),
StemcellPath: os.Getenv("STEMCELL_PATH"),
GardenReleasePath: os.Getenv("GARDEN_RELEASE_PATH"),
ConcourseReleasePath: os.Getenv("CONCOURSE_RELEASE_PATH"),
ConcourseDeploymentPath: os.Getenv("CONCOURSE_DEPLOYMENT_PATH"),
EnableTerraformFlag: os.Getenv("ENABLE_TERRAFORM_FLAG") == "true",
}
}