forked from elkargig/awless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
62 lines (54 loc) · 1.41 KB
/
validation.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
package template
import (
"bytes"
"errors"
"fmt"
"github.com/wallix/awless/cloud"
)
type Validator interface {
Execute(t *Template) []error
}
type LookupGraphFunc func(key string) (cloud.GraphAPI, bool)
type UniqueNameValidator struct {
LookupGraph LookupGraphFunc
}
func (v *UniqueNameValidator) Execute(t *Template) (errs []error) {
for _, cmd := range t.CommandNodesIterator() {
if cmd.Action == "create" {
name := cmd.ParamNodes["name"]
g, ok := v.LookupGraph(cmd.Entity)
if !ok {
continue
}
resources, err := g.FindWithProperties(map[string]interface{}{"Name": name})
if err != nil {
errs = append(errs, err)
}
if len(resources) > 0 {
for _, r := range resources {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("'%s' name already used for %s %s", name, r.Type(), r.Id()))
if state, ok := r.Properties()["State"].(string); ok {
buf.WriteString(fmt.Sprintf(" (state: '%s')", state))
}
errs = append(errs, errors.New(buf.String()))
}
}
}
}
return
}
type ParamIsSetValidator struct {
Entity, Action, Param, WarningMessage string
}
func (v *ParamIsSetValidator) Execute(t *Template) (errs []error) {
for _, cmd := range t.CommandNodesIterator() {
if cmd.Action == v.Action && cmd.Entity == v.Entity {
_, hasParam := cmd.ParamNodes[v.Param]
if !hasParam {
errs = append(errs, fmt.Errorf(v.WarningMessage))
}
}
}
return
}