forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
156 lines (126 loc) · 3.54 KB
/
template.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
package template
import (
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/go-ucfg/yaml"
)
var (
// Defaults used in the template
defaultDateDetection = false
defaultTotalFieldsLimit = 10000
// Array to store dynamicTemplate parts in
dynamicTemplates []common.MapStr
)
type Template struct {
index string
beatVersion Version
esVersion Version
settings templateSettings
}
// New creates a new template instance
func New(beatVersion string, esVersion string, index string, settings templateSettings) (*Template, error) {
bV, err := NewVersion(beatVersion)
if err != nil {
return nil, err
}
// In case no esVersion is set, it is assumed the same as beat version
if esVersion == "" {
esVersion = beatVersion
}
esV, err := NewVersion(esVersion)
if err != nil {
return nil, err
}
return &Template{
index: index,
beatVersion: *bV,
esVersion: *esV,
settings: settings,
}, nil
}
// Load the given input and generates the input based on it
func (t *Template) Load(file string) (common.MapStr, error) {
fields, err := loadYaml(file)
if err != nil {
return nil, err
}
// Start processing at the root
properties := fields.process("", t.esVersion)
output := t.generate(properties, dynamicTemplates)
return output, nil
}
// GetName returns the name of the template which is {index}-{version}
func (t *Template) GetName() string {
return fmt.Sprintf("%s-%s", t.index, t.beatVersion.String())
}
// generate generates the full template
// The default values are taken from the default variable.
func (t *Template) generate(properties common.MapStr, dynamicTemplates []common.MapStr) common.MapStr {
// Add base dynamic template
var dynamicTemplateBase = common.MapStr{
"strings_as_keyword": common.MapStr{
"mapping": common.MapStr{
"ignore_above": 1024,
"type": "keyword",
},
"match_mapping_type": "string",
},
}
if t.esVersion.IsMajor(2) {
dynamicTemplateBase.Put("strings_as_keyword.mapping.type", "string")
dynamicTemplateBase.Put("strings_as_keyword.mapping.index", "not_analyzed")
}
dynamicTemplates = append(dynamicTemplates, dynamicTemplateBase)
indexSettings := common.MapStr{
"refresh_interval": "5s",
"mapping": common.MapStr{
"total_fields": common.MapStr{
"limit": defaultTotalFieldsLimit,
},
},
}
indexSettings.DeepUpdate(t.settings.Index)
// Load basic structure
basicStructure := common.MapStr{
"mappings": common.MapStr{
"_default_": common.MapStr{
"_meta": common.MapStr{
"version": t.beatVersion.String(),
},
"date_detection": defaultDateDetection,
"dynamic_templates": dynamicTemplates,
"properties": properties,
},
},
"order": 1,
"settings": common.MapStr{
"index": indexSettings,
},
}
if len(t.settings.Source) > 0 {
basicStructure.Put("mappings._default_._source", t.settings.Source)
}
// ES 6 moved from template to index_patterns: https://github.com/elastic/elasticsearch/pull/21009
if t.esVersion.major >= 6 {
basicStructure.Put("index_patterns", []string{t.GetName() + "-*"})
} else {
basicStructure.Put("template", t.GetName()+"-*")
}
if t.esVersion.IsMajor(2) || t.esVersion.IsMajor(5) {
basicStructure.Put("mappings._default_._all.norms.enabled", false)
}
return basicStructure
}
func loadYaml(path string) (Fields, error) {
keys := []Field{}
cfg, err := yaml.NewConfigWithFile(path)
if err != nil {
return nil, err
}
cfg.Unpack(&keys)
fields := Fields{}
for _, key := range keys {
fields = append(fields, key.Fields...)
}
return fields, nil
}