-
Notifications
You must be signed in to change notification settings - Fork 27
/
module_config.go
186 lines (163 loc) · 4.54 KB
/
module_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package utils
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/davecgh/go-spew/spew"
utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum"
)
var (
ModuleEnabled = true
ModuleDisabled = false
)
type ModuleConfig struct {
ModuleName string
IsEnabled *bool
// module values, don't read it directly, use GetValues() for reading
values Values
}
// String returns description of ModuleConfig values.
func (mc *ModuleConfig) String() string {
return fmt.Sprintf("Module(Name=%s IsEnabled=%v Values:\n%s)", mc.ModuleName, mc.IsEnabled, mc.values.DebugString())
}
// ModuleConfigKey transforms module kebab-case name to the config camelCase name
func (mc *ModuleConfig) ModuleConfigKey() string {
return ModuleNameToValuesKey(mc.ModuleName)
}
// ModuleEnabledKey transforms module kebab-case name to the config camelCase name with 'Enabled' suffix
func (mc *ModuleConfig) ModuleEnabledKey() string {
return ModuleNameToValuesKey(mc.ModuleName) + "Enabled"
}
// GetEnabled returns string description of enabled status.
func (mc *ModuleConfig) GetEnabled() string {
if mc == nil {
return ""
}
switch {
case mc.IsEnabled == nil:
return "n/d"
case *mc.IsEnabled:
return "true"
default:
return "false"
}
}
func NewModuleConfig(moduleName string, values Values) *ModuleConfig {
if values == nil {
values = make(Values)
}
return &ModuleConfig{
ModuleName: moduleName,
IsEnabled: nil,
values: values,
}
}
// GetValuesWithModuleName enrich module values with module's name top level key
// if key is already present - returns values as it
// module: test-module with values {"a": "b", "c": "d} will return:
//
// testModule:
// a: b
// c: d
//
// Deprecated: use GetValues instead
func (mc *ModuleConfig) GetValuesWithModuleName() Values {
return mc.values
}
// GetValues returns values but without moduleName
//
// a: b
// c: d
func (mc *ModuleConfig) GetValues() Values {
if len(mc.values) == 0 {
return mc.values
}
valuesModuleName := ModuleNameToValuesKey(mc.ModuleName)
if mc.values.HasKey(valuesModuleName) {
return mc.values.GetKeySection(valuesModuleName)
}
return mc.values
}
// DropValues removes values from module config
func (mc *ModuleConfig) DropValues() {
mc.values = Values{}
}
// LoadFromValues loads module config from a map.
//
// Values for module in `values` map are addressed by a key.
// This key should be produced with ModuleNameToValuesKey.
func (mc *ModuleConfig) LoadFromValues(values Values) (*ModuleConfig, error) {
if moduleValuesData, hasModuleData := values[mc.ModuleConfigKey()]; hasModuleData {
switch v := moduleValuesData.(type) {
case map[string]interface{}, []interface{}:
data := map[string]interface{}{mc.ModuleConfigKey(): v}
values, err := NewValues(data)
if err != nil {
return nil, err
}
mc.values = values
default:
return nil, fmt.Errorf("load '%s' values: module config should be array or map. Got: %s", mc.ModuleName, spew.Sdump(moduleValuesData))
}
}
if moduleEnabled, hasModuleEnabled := values[mc.ModuleEnabledKey()]; hasModuleEnabled {
switch v := moduleEnabled.(type) {
case bool:
mc.IsEnabled = &v
default:
return nil, fmt.Errorf("load '%s' enable config: enabled value should be bool. Got: %#v", mc.ModuleName, moduleEnabled)
}
}
return mc, nil
}
// FromYaml loads module config from a yaml string.
//
// Example:
//
// simpleModule:
//
// param1: 10
// param2: 120
//
// simpleModuleEnabled: true
func (mc *ModuleConfig) FromYaml(yamlString []byte) (*ModuleConfig, error) {
values, err := NewValuesFromBytes(yamlString)
if err != nil {
return nil, fmt.Errorf("load module '%s' yaml config: %s\n%s", mc.ModuleName, err, string(yamlString))
}
return mc.LoadFromValues(values)
}
func (mc *ModuleConfig) Checksum() string {
vChecksum := mc.values.Checksum()
enabled := ""
if mc.IsEnabled != nil {
enabled = strconv.FormatBool(*mc.IsEnabled)
}
return utils_checksum.CalculateChecksum(enabled, vChecksum)
}
func ModuleEnabledValue(i interface{}) (*bool, error) {
switch v := i.(type) {
case string:
switch strings.ToLower(v) {
case "true":
return &ModuleEnabled, nil
case "false":
return &ModuleDisabled, nil
}
// TODO(nabokihms): we only need to check the json.RawMessage after the patch refactoring.
case json.RawMessage:
switch strings.ToLower(string(v)) {
case "true":
return &ModuleEnabled, nil
case "false":
return &ModuleDisabled, nil
}
case bool:
if v {
return &ModuleEnabled, nil
}
return &ModuleDisabled, nil
}
return nil, fmt.Errorf("unsupported module enabled value: %v", i)
}