代码拉取完成,页面将自动刷新
package template
import (
"fmt"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/paths"
)
// TemplateLoader is a subset of the Elasticsearch client API capable of
// loading the template.
type ESClient interface {
LoadJSON(path string, json map[string]interface{}) ([]byte, error)
Request(method, path string, pipeline string, params map[string]string, body interface{}) (int, []byte, error)
GetVersion() string
}
type Loader struct {
config TemplateConfig
client ESClient
beatInfo beat.Info
}
func NewLoader(cfg *common.Config, client ESClient, beatInfo beat.Info) (*Loader, error) {
config := DefaultConfig
err := cfg.Unpack(&config)
if err != nil {
return nil, err
}
return &Loader{
config: config,
client: client,
beatInfo: beatInfo,
}, nil
}
// Load checks if the index mapping template should be loaded
// In case the template is not already loaded or overwriting is enabled, the
// template is written to index
func (l *Loader) Load() error {
tmpl, err := New(l.beatInfo.Version, l.beatInfo.IndexPrefix, l.client.GetVersion(), l.config)
if err != nil {
return fmt.Errorf("error creating template instance: %v", err)
}
// Check if template already exist or should be overwritten
exists := l.CheckTemplate(tmpl.GetName())
if !exists || l.config.Overwrite {
logp.Info("Loading template for Elasticsearch version: %s", l.client.GetVersion())
if l.config.Overwrite {
logp.Info("Existing template will be overwritten, as overwrite is enabled.")
}
fieldsPath := paths.Resolve(paths.Config, l.config.Fields)
output, err := tmpl.Load(fieldsPath)
if err != nil {
return fmt.Errorf("error creating template from file %s: %v", fieldsPath, err)
}
err = l.LoadTemplate(tmpl.GetName(), output)
if err != nil {
return fmt.Errorf("could not load template: %v", err)
}
} else {
logp.Info("Template already exists and will not be overwritten.")
}
return nil
}
// LoadTemplate loads a template into Elasticsearch overwriting the existing
// template if it exists. If you wish to not overwrite an existing template
// then use CheckTemplate prior to calling this method.
func (l *Loader) LoadTemplate(templateName string, template map[string]interface{}) error {
logp.Debug("template", "Try loading template with name: %s", templateName)
path := "/_template/" + templateName
body, err := l.client.LoadJSON(path, template)
if err != nil {
return fmt.Errorf("couldn't load template: %v. Response body: %s", err, body)
}
logp.Info("Elasticsearch template with name '%s' loaded", templateName)
return nil
}
// CheckTemplate checks if a given template already exist. It returns true if
// and only if Elasticsearch returns with HTTP status code 200.
func (l *Loader) CheckTemplate(templateName string) bool {
status, _, _ := l.client.Request("HEAD", "/_template/"+templateName, "", nil, nil)
if status != 200 {
return false
}
return true
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。