forked from jenningsloy318/redfish_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
executable file
·93 lines (78 loc) · 1.93 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
package main
import (
"fmt"
"sync"
"os"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Hosts map[string]HostConfig `yaml:"hosts"`
Groups map[string]HostConfig `yaml:"groups"`
Loglevel string `yaml:"loglevel"`
}
type SafeConfig struct {
sync.RWMutex
Config *Config
}
type HostConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// Read exporter config from file
func NewConfigFromFile(configFile string) (*Config, error) {
var config = &Config{}
yamlFile, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(yamlFile, config); err != nil {
return nil, err
}
return config, nil
}
func (sc *SafeConfig) ReloadConfig(configFile string) error {
var c, err = NewConfigFromFile(configFile)
if err != nil {
return err
}
sc.Lock()
sc.Config = c
sc.Unlock()
return nil
}
func (sc *SafeConfig) HostConfigForTarget(target string) (*HostConfig, error) {
sc.Lock()
defer sc.Unlock()
if hostConfig, ok := sc.Config.Hosts[target]; ok {
return &HostConfig{
Username: hostConfig.Username,
Password: hostConfig.Password,
}, nil
}
if hostConfig, ok := sc.Config.Hosts["default"]; ok {
return &HostConfig{
Username: hostConfig.Username,
Password: hostConfig.Password,
}, nil
}
return &HostConfig{}, fmt.Errorf("no credentials found for target %s", target)
}
// HostConfigForGroup checks the configuration for a matching group config and returns the configured HostConfig for
// that matched group.
func (sc *SafeConfig) HostConfigForGroup(group string) (*HostConfig, error) {
sc.Lock()
defer sc.Unlock()
if hostConfig, ok := sc.Config.Groups[group]; ok {
return &hostConfig, nil
}
return &HostConfig{}, fmt.Errorf("no credentials found for group %s", group)
}
func (sc *SafeConfig) AppLogLevel() string {
sc.Lock()
defer sc.Unlock()
logLevel := sc.Config.Loglevel
if logLevel != "" {
return logLevel
}
return "info"
}