/
config.go
80 lines (67 loc) · 1.78 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
package config
import (
"io/ioutil"
"log"
"os"
"time"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Providers []Provider `yaml:"providers"`
EnableMonitorDeletion bool `yaml:"enableMonitorDeletion"`
MonitorNameTemplate string `yaml:"monitorNameTemplate"`
ResyncPeriod int `yaml:"resyncPeriod,omitempty"`
CreationDelay time.Duration `yaml:"creationDelay,omitempty"`
}
// UnmarshalYAML interface to deserialize specific types
func (c *Config) UnmarshalYAML(data []byte) error {
type Alias Config
aux := struct {
CreationDelay string `yaml:"creationDelay,omitempty"`
*Alias
}{
Alias: (*Alias)(c),
}
if err := yaml.Unmarshal(data, &aux); err != nil {
return err
}
delay, err := time.ParseDuration(aux.CreationDelay)
if err != nil {
return err
}
c.CreationDelay = delay
return nil
}
type Provider struct {
Name string `yaml:"name"`
ApiKey string `yaml:"apiKey"`
ApiURL string `yaml:"apiURL"`
AlertContacts string `yaml:"alertContacts"`
AlertIntegrations string `yaml:"alertIntegrations"`
Username string `yaml:"username"`
Password string `yaml:"password"`
AccountEmail string `yaml:"accountEmail"`
}
func ReadConfig(filePath string) Config {
var config Config
// Read YML
log.Println("Reading YAML Configuration", filePath)
source, err := ioutil.ReadFile(filePath)
if err != nil {
log.Panic(err)
}
// Unmarshall
err = yaml.Unmarshal(source, &config)
if err != nil {
log.Panic(err)
}
return config
}
func GetControllerConfig() Config {
configFilePath := os.Getenv("CONFIG_FILE_PATH")
if len(configFilePath) == 0 {
configFilePath = "../../configs/testConfigs/test-config.yaml"
}
config := ReadConfig(configFilePath)
return config
}