forked from owulveryck/toscalib
-
Notifications
You must be signed in to change notification settings - Fork 4
/
assignment.go
352 lines (318 loc) · 8.14 KB
/
assignment.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package toscalib
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
// Assignment supports Value evaluation
type Assignment struct {
Value interface{}
Function string
Args []interface{}
Expression ConstraintClause
}
// UnmarshalYAML converts YAML text to a type
func (p *Assignment) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err == nil {
p.Value = s
return nil
}
var m map[string]string
if err := unmarshal(&m); err == nil {
processed := false
for k, v := range m {
if isFunction(k) {
processed = true
p.Function = k
args := make([]interface{}, 1)
args[0] = v
p.Args = args
}
if isOperator(k) {
processed = true
p.Expression = ConstraintClause{Operator: k, Values: v}
}
}
if !processed {
p.Value = m
}
return nil
}
var mm map[string][]string
if err := unmarshal(&mm); err == nil {
processed := false
for k, v := range mm {
if isFunction(k) {
processed = true
p.Function = k
args := make([]interface{}, len(v))
for i, a := range v {
args[i] = a
}
p.Args = args
}
if isOperator(k) {
processed = true
p.Expression = ConstraintClause{Operator: k, Values: v}
}
}
if !processed {
p.Value = mm
}
return nil
}
// ex. concat function with another function embedded inside
var mmm map[string][]interface{}
if err := unmarshal(&mmm); err == nil {
processed := false
for k, v := range mmm {
if isFunction(k) {
processed = true
p.Function = k
p.Args = v
}
}
if !processed {
p.Value = mmm
}
return nil
}
// Value is list of values
var mmmm []interface{}
if err := unmarshal(&mmmm); err == nil {
p.Value = mmmm
return nil
}
// Value is map of values
var mmmmm map[string]interface{}
if err := unmarshal(&mmmmm); err == nil {
p.Value = mmmmm
return nil
}
var res interface{}
_ = unmarshal(&res)
return fmt.Errorf("Cannot parse Property %v", res)
}
func newAssignmentFunc(val interface{}) *Assignment {
rval := reflect.ValueOf(val)
switch rval.Kind() {
case reflect.Map:
for k, v := range rval.Interface().(map[interface{}]interface{}) {
if !isFunction(k.(string)) {
continue
}
// Convert it to a Assignment
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.String:
return &Assignment{Function: k.(string), Args: []interface{}{rv.Interface()}}
default:
return &Assignment{Function: k.(string), Args: rv.Interface().([]interface{})}
}
}
}
return nil
}
func (p *Assignment) lookupValueArg(arg string) interface{} {
v := reflect.ValueOf(p.Value)
switch v.Kind() {
case reflect.Slice:
if i, err := strconv.ParseInt(arg, 10, 0); err == nil {
return v.Index(int(i)).Interface()
}
case reflect.Map:
return v.MapIndex(reflect.ValueOf(arg)).Interface()
}
return v.Interface()
}
func (p *Assignment) evaluate(std *ServiceTemplateDefinition, ctx, arg string) interface{} {
if arg != "" {
val := p.lookupValueArg(arg)
// handle the scenario when the property value is another
// function call.
if pa := newAssignmentFunc(val); pa != nil {
return pa.Evaluate(std, ctx)
}
if len(p.Args) != 0 {
tmp := clone(*p)
pa, _ := tmp.(Assignment)
pa.Value = val
return pa.lookupValueArg(p.Args[0].(string))
}
return val
}
return p.Evaluate(std, ctx)
}
func getNTByArgs(std *ServiceTemplateDefinition, ctx string, args []interface{}) (*NodeTemplate, *NodeTemplate) {
nt := std.findNodeTemplate(args[0].(string), ctx)
if nt == nil {
return nil, nil
}
if r := nt.GetRequirement(args[1].(string)); r != nil {
if rnt := std.GetNodeTemplate(r.Node); rnt != nil {
return nt, rnt
}
}
return nt, nil
}
func (p *Assignment) evalConcat(std *ServiceTemplateDefinition, ctx string) interface{} {
var output string
for _, val := range p.Args {
switch reflect.TypeOf(val).Kind() {
case reflect.String, reflect.Int:
output = fmt.Sprintf("%s%s", output, val)
case reflect.Map:
if pa := newAssignmentFunc(val); pa != nil {
if o := pa.Evaluate(std, ctx); o != nil {
output = fmt.Sprintf("%s%s", output, o)
}
}
}
}
return output
}
func (p *Assignment) evalToken(std *ServiceTemplateDefinition, ctx string) interface{} {
var output string
var value string
var token string
for idx, val := range p.Args {
switch reflect.TypeOf(val).Kind() {
case reflect.String:
// the first and second arg must result be of type string or resolve
// to be a string.
if idx == 0 {
value = val.(string)
} else if idx == 1 {
token = val.(string)
}
case reflect.Map:
// the first input could actually be a lookup for the value
if idx == 0 {
if pa := newAssignmentFunc(val); pa != nil {
if o := pa.Evaluate(std, ctx); o != nil {
value = fmt.Sprintf("%s", o)
}
}
}
case reflect.Int:
// the 3rd arg must be an int
if idx == 2 && value != "" && token != "" {
output = strings.Split(value, token)[val.(int)]
}
}
}
if output == "" {
return nil
}
return output
}
func (p *Assignment) evalArtifact(std *ServiceTemplateDefinition, ctx string) interface{} {
nt, _ := getNTByArgs(std, ctx, p.Args)
if nt == nil {
return nil
}
if at, ok := nt.Artifacts[p.Args[1].(string)]; ok {
// set default location to the 'temp|tmp' directory to handle 'LOCAL_FILE' being specified
// or no location or deploy_path is specified.
location := os.TempDir()
if loc := get(2, p.Args); loc != "" && loc != LocalFile {
location = loc
} else if at.DeployPath != "" {
location = at.DeployPath
}
destFile, err := copyFile(at.File, location)
if err != nil {
return nil
}
return destFile
}
return nil
}
func (p *Assignment) evalProperty(std *ServiceTemplateDefinition, ctx string) interface{} {
nt, rnt := getNTByArgs(std, ctx, p.Args)
if nt == nil {
return nil
}
if len(p.Args) == 2 {
if prop := nt.findProperty(p.Args[1].(string), ""); prop != nil {
return prop.evaluate(std, nt.Name, "")
}
}
if len(p.Args) >= 3 {
if rnt != nil {
if prop := rnt.findProperty(p.Args[2].(string), p.Args[1].(string)); prop != nil {
prop.Args = remainder(3, p.Args)
return prop.evaluate(std, rnt.Name, get(3, p.Args))
}
}
if prop := nt.findProperty(p.Args[2].(string), p.Args[1].(string)); prop != nil {
prop.Args = remainder(3, p.Args)
return prop.evaluate(std, nt.Name, get(3, p.Args))
}
if prop := nt.findProperty(p.Args[1].(string), ""); prop != nil {
prop.Args = remainder(2, p.Args)
return prop.evaluate(std, nt.Name, get(2, p.Args))
}
}
return nil
}
func (p *Assignment) evalAttribute(std *ServiceTemplateDefinition, ctx string) interface{} {
nt, rnt := getNTByArgs(std, ctx, p.Args)
if nt == nil {
return nil
}
if len(p.Args) == 2 {
if attr := nt.findAttribute(p.Args[1].(string), ""); attr != nil {
return attr.evaluate(std, nt.Name, "")
}
}
if len(p.Args) >= 3 {
if rnt != nil {
if attr := rnt.findAttribute(p.Args[2].(string), p.Args[1].(string)); attr != nil {
attr.Args = remainder(3, p.Args)
return attr.evaluate(std, rnt.Name, get(3, p.Args))
}
}
if attr := nt.findAttribute(p.Args[2].(string), p.Args[1].(string)); attr != nil {
attr.Args = remainder(3, p.Args)
return attr.evaluate(std, nt.Name, get(3, p.Args))
}
if attr := nt.findAttribute(p.Args[1].(string), ""); attr != nil {
attr.Args = remainder(2, p.Args)
return attr.evaluate(std, nt.Name, get(2, p.Args))
}
}
return nil
}
// Evaluate gets the value of an Assignment, including the evaluation of expression or function
func (p *Assignment) Evaluate(std *ServiceTemplateDefinition, ctx string) interface{} {
// TODO(kenjones): Add support for the evaluation of ConstraintClause
if p.Value != nil {
return p.Value
}
switch p.Function {
case ConcatFunc:
return p.evalConcat(std, ctx)
case TokenFunc:
// there are 3 required args
if len(p.Args) == 3 {
return p.evalToken(std, ctx)
}
case GetArtifactFunc:
if len(p.Args) > 1 {
return p.evalArtifact(std, ctx)
}
case GetInputFunc:
if len(p.Args) == 1 {
return std.GetInputValue(p.Args[0].(string), false)
}
case GetPropFunc:
return p.evalProperty(std, ctx)
case GetAttrFunc:
return p.evalAttribute(std, ctx)
}
return nil
}