forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
102 lines (90 loc) · 2.56 KB
/
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
package template
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/bus"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/processors"
ucfg "github.com/elastic/go-ucfg"
)
// Mapper maps config templates with conditions, if a match happens on a discover event
// the given template will be used as config
type Mapper []*ConditionMap
// ConditionMap maps a condition to the configs to use when it's triggered
type ConditionMap struct {
Condition *processors.Condition
Configs []*common.Config
}
// MapperSettings holds user settings to build Mapper
type MapperSettings []*struct {
ConditionConfig *processors.ConditionConfig `config:"condition"`
Configs []*common.Config `config:"config"`
}
// NewConfigMapper builds a template Mapper from given settings
func NewConfigMapper(configs MapperSettings) (*Mapper, error) {
var mapper Mapper
for _, c := range configs {
condition, err := processors.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
}
mapper = append(mapper, &ConditionMap{
Condition: condition,
Configs: c.Configs,
})
}
return &mapper, nil
}
// Event adapts MapStr to processors.ValuesMap interface
type Event common.MapStr
// GetValue extracts given key from an Event
func (e Event) GetValue(key string) (interface{}, error) {
val, err := common.MapStr(e).GetValue(key)
if err != nil {
return nil, err
}
return val, nil
}
// GetConfig returns a matching Config if any, nil otherwise
func (c *Mapper) GetConfig(event bus.Event) []*common.Config {
var result []*common.Config
for _, mapping := range *c {
if mapping.Configs != nil && !mapping.Condition.Check(Event(event)) {
continue
}
// unpack input
vars, err := ucfg.NewFrom(map[string]interface{}{
"data": event,
})
if err != nil {
logp.Err("Error building config: %v", err)
}
opts := []ucfg.Option{
ucfg.PathSep("."),
ucfg.Env(vars),
ucfg.ResolveEnv,
ucfg.VarExp,
}
for _, config := range mapping.Configs {
c, err := ucfg.NewFrom(config, opts...)
if err != nil {
logp.Err("Error parsing config: %v", err)
continue
}
// Unpack config to process any vars in the template:
var unpacked map[string]interface{}
c.Unpack(&unpacked, opts...)
if err != nil {
logp.Err("Error unpacking config: %v", err)
continue
}
// Repack again:
res, err := common.NewConfigFrom(unpacked)
if err != nil {
logp.Err("Error creating config from unpack: %v", err)
continue
}
result = append(result, res)
}
}
return result
}