-
Notifications
You must be signed in to change notification settings - Fork 3
/
visitor.go
113 lines (90 loc) · 2.87 KB
/
visitor.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
package main
import (
"context"
"encoding/json"
"github.com/buger/jsonparser"
"github.com/pkg/errors"
)
type templateFragment struct {
fragment json.RawMessage
}
type templateVisitor interface {
VisitResource(ctx context.Context, t *templateFragment, name, resourceType string, resource json.RawMessage) error
}
type myvisitor struct {
props []albEventProperties
}
func (tf *templateFragment) resourceNames() ([]string, error) {
resources, _, _, err := jsonparser.Get(tf.fragment, "Resources")
var resourceNames []string
err = jsonparser.ObjectEach(resources, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
resourceNames = append(resourceNames, string(key))
return nil
})
if err != nil {
return nil, errors.Wrap(err, "iterating over resources")
}
return resourceNames, nil
}
func (tf *templateFragment) PutResource(name string, resource json.RawMessage) error {
f, err := jsonparser.Set(tf.fragment, resource, "Resources", name)
if err != nil {
return errors.WithStack(err)
}
tf.fragment = f
return nil
}
func (tf *templateFragment) Visit(ctx context.Context, visitor templateVisitor) error {
names, err := tf.resourceNames()
if err != nil {
return err
}
for _, name := range names {
resourceBytes, _, _, err := jsonparser.Get(tf.fragment, "Resources", name)
resType, err := jsonparser.GetString(resourceBytes, "Type")
if err != nil {
return errors.Wrap(err, "getting Resource type")
}
err = visitor.VisitResource(ctx, tf, name, resType, resourceBytes)
if err != nil {
return err
}
}
return nil
}
func (m *myvisitor) VisitResource(ctx context.Context, t *templateFragment, name, resourceType string, resource json.RawMessage) error {
if resourceType != "AWS::Serverless::Function" {
return nil
}
var keysToDelete []string
res := []byte(resource)
alias, _ := jsonparser.GetString(resource, "Properties", "AutoPublishAlias")
tagsjson, _, _, _ := jsonparser.Get(resource, "Properties", "Tags")
tags := map[string]json.RawMessage{}
if len(tagsjson) > 0 {
json.Unmarshal(tagsjson, &tags)
}
err := jsonparser.ObjectEach(resource, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
eventType, err := jsonparser.GetString(value, "Type")
if err != nil || eventType != "ALB" {
return errors.WithStack(err)
}
propjson, _, _, err := jsonparser.Get(value, "Properties")
prop := albEventProperties{}
json.Unmarshal(propjson, &prop)
prop.Resource = name
prop.EventName = string(key)
prop.Alias = alias
prop.Tags = tags
m.props = append(m.props, prop)
keysToDelete = append(keysToDelete, string(key))
return nil
}, "Properties", "Events")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return errors.WithStack(err)
}
for _, key := range keysToDelete {
res = jsonparser.Delete(res, "Properties", "Events", key)
}
return t.PutResource(name, res)
}