-
Notifications
You must be signed in to change notification settings - Fork 1
/
params.go
187 lines (165 loc) · 4.4 KB
/
params.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
187
package ansible
import (
"fmt"
"io/ioutil"
"reflect"
"github.com/ekara-platform/model"
"gopkg.in/yaml.v2"
)
type ParamValues map[string]string
type keyValue struct {
key string
value string
}
// BaseParam contains the extra vars to be passed to a playbook
//
// The BaseParam content is supposed to be serialized in yaml in order to be passed
// to a playbook
type BaseParam struct {
// The content of the BaseParam
Body model.Parameters
}
// BuildBaseParam the common parameters required by all playbooks.
//
// Parameters:
// name: the qualified name of the environment
// nodesetName: the name of the nodeset we are working with
// provider: the name of the provider where to create the nodeset
// pubK: the public SSH key to connect on the created nodeset ( the name of the file)
// privK: the private SSH key to connect on the created nodeset ( the name of the file)
//
func BuildBaseParam(env *model.Environment, pubK string, privK string, nodesetName string) BaseParam {
baseParam := BaseParam{}
baseParam.Body = make(map[string]interface{})
sshM := make(map[string]interface{})
if pubK != "" {
sshM["public_key"] = pubK
}
if privK != "" {
sshM["private_key"] = privK
}
baseParam.Body["ssh"] = sshM
environmentM := make(map[string]interface{})
if env.QualifiedName().String() != "" {
environmentM["id"] = env.QualifiedName().String()
}
if env.Name != "" {
environmentM["name"] = env.Name
}
if env.Qualifier != "" {
environmentM["qualifier"] = env.Qualifier
}
if nodesetName != "" {
environmentM["nodeset"] = nodesetName
}
baseParam.Body["environment"] = environmentM
return baseParam
}
//AddNamedMap adds a parameter of type map[string]interface{} for the given name
func (bp *BaseParam) AddNamedMap(name string, c map[string]interface{}) {
bp.Body[name] = c
}
//AddMap adds parameters of interface{} for all the given map entries
func (bp *BaseParam) AddMap(m map[string]interface{}) {
for k, v := range m {
bp.AddInterface(k, v)
}
}
//AddInterface adds a parameter of type interface{} for the given name
func (bp *BaseParam) AddInterface(name string, i interface{}) {
bp.Body[name] = i
}
//AddInt adds a int parameter for the given name
func (bp *BaseParam) AddInt(name string, i int) {
bp.Body[name] = i
}
//AddString adds a string parameter for the given name
func (bp *BaseParam) AddString(name string, s string) {
bp.Body[name] = s
}
// AddBuffer adds the parameters coming from the given buffer
//
// Only the "Param" content of the buffer will be processed.
func (bp *BaseParam) AddBuffer(b Buffer) {
bp.AddMap(b.Param)
}
// Content returns the yaml representation of the content
func (bp BaseParam) Content() (b []byte, e error) {
b, e = yaml.Marshal(&bp.Body)
return
}
// Copy returns a copy of the base parameter
func (bp BaseParam) Copy() BaseParam {
result := BaseParam{}
result.Body = make(map[string]interface{})
for k, v := range bp.Body {
val, okType := v.(map[string]interface{})
if okType {
m := make(map[string]interface{})
for kVal, vVal := range val {
m[kVal] = vVal
}
result.Body[k] = m
} else {
result.Body[k] = v
}
}
return result
}
//ParseParamValues parses a yaml file into a map of "key:value"
// All the nested yaml levels will be concatenated to create the keys of the map.
//
// As example :
// level1:
// level2:value
//
// Will generate the following key/value
// level1.level2=value
func ParseParamValues(path string) (ParamValues, error) {
r := make(map[string]string)
b, err := ioutil.ReadFile(path)
if err != nil {
return r, err
}
cKv := make(chan keyValue)
exit := make(chan bool)
env := make(map[interface{}]interface{})
err = yaml.Unmarshal(b, &env)
if err != nil {
panic(err)
}
go readMap(cKv, exit, "", env)
for {
select {
case <-exit:
return r, nil
case kv := <-cKv:
r[kv.key] = kv.value
}
}
}
func readMap(cKv chan keyValue, exit chan bool, location string, m map[interface{}]interface{}) {
if location != "" {
location += "."
}
for k, v := range m {
ks := fmt.Sprintf("%v", k)
if reflect.ValueOf(v).Kind() == reflect.Map {
// The value is a map so we go deeper...
readMap(cKv, exit, location+ks, v.(map[interface{}]interface{}))
} else {
if v != nil {
vs := fmt.Sprintf("%v", v)
// The located leaf is returned...
cKv <- keyValue{
key: location + ks,
value: vs,
}
}
}
}
// Only the root map can trigger the exit
if location == "" {
exit <- true
}
}