This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.go
95 lines (81 loc) · 2.32 KB
/
conf.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
package uyuniapi
import (
"github.com/go-yaml/yaml"
"io/ioutil"
"os"
)
type configLayout struct {
Context struct {
Http_address string
Openapi_path string
Uyuni struct {
Default map[string]interface{}
Hosts map[string]map[string]interface{}
}
}
}
type APIConfig struct {
config configLayout
DEFAULT_ADDR string
DEFAULT_OPENAPI_PATH string
}
func NewAPIConfig(path string) *APIConfig {
cfg := new(APIConfig)
cfg.DEFAULT_ADDR = ":8080"
cfg.DEFAULT_OPENAPI_PATH = "/usr/share/mgrapi/sui"
cfg.config = configLayout{}
cfg.readConfig(path)
return cfg
}
func (cfg *APIConfig) readConfig(path string) {
fh, err := os.Open(path)
if err != nil {
panic("Error open configuration:" + err.Error())
}
defer fh.Close()
mapBytes, err := ioutil.ReadAll(fh)
if err != nil {
panic("Error reading configuration:" + err.Error())
}
if err := yaml.Unmarshal(mapBytes, &cfg.config); err != nil {
panic("Error parsing configuration:" + err.Error())
}
}
// Get OpenAPI path to a static UI
func (cfg *APIConfig) GetOpenAPIStaticPath() string {
return cfg.config.Context.Openapi_path
}
// Set OpenAPI path
func (cfg *APIConfig) SetOpenAPIStaticPath(path string) {
if path != "" && path != cfg.DEFAULT_OPENAPI_PATH {
cfg.config.Context.Openapi_path = path
}
}
// TODO: Get default hosts by area. Currently only one area is there: Uyuni
func (cfg *APIConfig) GetHosts(area string) map[string]map[string]interface{} {
return cfg.config.Context.Uyuni.Hosts
}
// TODO: Get default setup by area. Currently only one area is there: Uyuni
func (cfg *APIConfig) GetDefaultSetup(area string) map[string]interface{} {
return cfg.config.Context.Uyuni.Default
}
// Get reference server configuration
func (cfg *APIConfig) GetRefServerConfig(fqdn string) map[string]interface{} {
serverConf := cfg.config.Context.Uyuni.Default
if localConfig, exists := cfg.config.Context.Uyuni.Hosts[fqdn]; exists {
for k, v := range localConfig {
serverConf[k] = v
}
} else {
panic("No host is configured as " + fqdn) // This actually should not happen, as fqdn should come from the Hosts variable anyway
}
return serverConf
}
// Get HTTP server address to run on
func (cfg *APIConfig) GetHTTPAddress() string {
if cfg.config.Context.Http_address == "" {
return cfg.DEFAULT_ADDR
} else {
return cfg.config.Context.Http_address
}
}