diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index de7da05de5b..c0a07bcd80e 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -35,5 +35,5 @@ The list below covers the major changes between 7.0.0-rc2 and master only. by `make` and `mage`. Example: `export PYTHON_EXE=python2.7`. {pull}11212[11212] - Prometheus helper for metricbeat contains now `Namespace` field for `prometheus.MetricsMappings` {pull}11424[11424] - Update Jinja2 version to 2.10.1. {pull}11817[11817] -- Reduce idxmgmt.Supporter interface and rework export commands to reuse logic. {pull}11777[11777], {pull}12065[12065] +- Reduce idxmgmt.Supporter interface and rework export commands to reuse logic. {pull}11777[11777], {pull}12065[12065], {pull}12067[12067] - Update urllib3 version to 1.24.2 {pull}11930[11930] diff --git a/libbeat/cmd/export/export.go b/libbeat/cmd/export/export.go index d7acacf9753..07ec27edf08 100644 --- a/libbeat/cmd/export/export.go +++ b/libbeat/cmd/export/export.go @@ -71,10 +71,7 @@ func newFileClient(dir string, ver string) (*fileClient, error) { if err != nil { return nil, err } - err = os.MkdirAll(path, os.ModePerm) - if err != nil { - return nil, err - } + fmt.Println(fmt.Sprintf("Writing to directory %s", path)) return &fileClient{ver: *common.MustNewVersion(ver), dir: path}, nil } @@ -82,7 +79,7 @@ func (c *stdoutClient) GetVersion() common.Version { return c.ver } -func (c *stdoutClient) Write(_ string, body string) error { +func (c *stdoutClient) Write(_ string, _ string, body string) error { _, err := c.f.WriteString(body) return err } @@ -91,8 +88,12 @@ func (c *fileClient) GetVersion() common.Version { return c.ver } -func (c *fileClient) Write(name string, body string) error { - f, err := os.Create(filepath.Join(c.dir, fmt.Sprintf("%s.json", name))) +func (c *fileClient) Write(component string, name string, body string) error { + path := filepath.Join(c.dir, component) + if err := os.MkdirAll(path, os.ModePerm); err != nil { + return err + } + f, err := os.Create(filepath.Join(path, fmt.Sprintf("%s.json", name))) defer f.Close() if err != nil { return err diff --git a/libbeat/idxmgmt/client_handler.go b/libbeat/idxmgmt/client_handler.go index c4665ce0ecb..9feba65ebd7 100644 --- a/libbeat/idxmgmt/client_handler.go +++ b/libbeat/idxmgmt/client_handler.go @@ -45,7 +45,7 @@ type ESClient interface { // prepare a policy and write alias. type FileClient interface { GetVersion() common.Version - Write(name string, body string) error + Write(component string, name string, body string) error } // NewClientHandler initializes and returns a new instance of ClientHandler diff --git a/libbeat/idxmgmt/ilm/client_handler.go b/libbeat/idxmgmt/ilm/client_handler.go index 1cc8456c21d..99daa4aaae3 100644 --- a/libbeat/idxmgmt/ilm/client_handler.go +++ b/libbeat/idxmgmt/ilm/client_handler.go @@ -63,7 +63,7 @@ type FileClientHandler struct { // prepare a policy and write alias. type FileClient interface { GetVersion() common.Version - Write(name string, body string) error + Write(component string, name string, body string) error } const ( @@ -243,9 +243,8 @@ func (h *FileClientHandler) CheckILMEnabled(mode Mode) (bool, error) { // CreateILMPolicy writes given policy to the configured file. func (h *FileClientHandler) CreateILMPolicy(policy Policy) error { - p := common.MapStr{policy.Name: policy.Body} - str := fmt.Sprintf("%s\n", p.StringToPrint()) - if err := h.client.Write(policy.Name, str); err != nil { + str := fmt.Sprintf("%s\n", policy.Body.StringToPrint()) + if err := h.client.Write("policy", policy.Name, str); err != nil { return fmt.Errorf("error printing policy : %v", err) } return nil diff --git a/libbeat/idxmgmt/ilm/client_handler_integration_test.go b/libbeat/idxmgmt/ilm/client_handler_integration_test.go index 8d328cbca2f..960436b4a05 100644 --- a/libbeat/idxmgmt/ilm/client_handler_integration_test.go +++ b/libbeat/idxmgmt/ilm/client_handler_integration_test.go @@ -256,18 +256,19 @@ func TestFileClientHandler_CreateILMPolicy(t *testing.T) { c := newMockClient("") h := ilm.NewFileClientHandler(c) name := "test-policy" - body := map[string]interface{}{"foo": "bar"} + body := common.MapStr{"foo": "bar"} h.CreateILMPolicy(ilm.Policy{Name: name, Body: body}) assert.Equal(t, name, c.name) + assert.Equal(t, "policy", c.component) var out common.MapStr json.Unmarshal([]byte(c.body), &out) - assert.Equal(t, common.MapStr{name: body}, out) + assert.Equal(t, body, out) } type mockClient struct { - v common.Version - name, body string + v common.Version + component, name, body string } func newMockClient(v string) *mockClient { @@ -281,7 +282,7 @@ func (c *mockClient) GetVersion() common.Version { return c.v } -func (c *mockClient) Write(name string, body string) error { - c.name, c.body = name, body +func (c *mockClient) Write(component string, name string, body string) error { + c.component, c.name, c.body = component, name, body return nil } diff --git a/libbeat/idxmgmt/ilm/ilm.go b/libbeat/idxmgmt/ilm/ilm.go index 3ecc85d8c40..ee2c203289b 100644 --- a/libbeat/idxmgmt/ilm/ilm.go +++ b/libbeat/idxmgmt/ilm/ilm.go @@ -59,7 +59,7 @@ type Manager interface { // See: [Policy phases and actions documentation](https://www.elastic.co/guide/en/elasticsearch/reference/master/ilm-policy-definition.html). type Policy struct { Name string - Body map[string]interface{} + Body common.MapStr } // Alias describes the alias to be created in Elasticsearch. diff --git a/libbeat/idxmgmt/ilm/ilm_test.go b/libbeat/idxmgmt/ilm/ilm_test.go index 9a860e04f64..2d457010050 100644 --- a/libbeat/idxmgmt/ilm/ilm_test.go +++ b/libbeat/idxmgmt/ilm/ilm_test.go @@ -76,12 +76,10 @@ func TestDefaultSupport_Init(t *testing.T) { t.Run("load external policy", func(t *testing.T) { s, err := DefaultSupport(nil, info, common.MustNewConfigFrom( - map[string]interface{}{ - "policy_file": "testfiles/custom.json", - }, + common.MapStr{"policy_file": "testfiles/custom.json"}, )) require.NoError(t, err) - assert.Equal(t, map[string]interface{}{"hello": "world"}, s.Policy().Body) + assert.Equal(t, common.MapStr{"hello": "world"}, s.Policy().Body) }) } diff --git a/libbeat/template/load.go b/libbeat/template/load.go index d9fe54897a6..2d8fdbf6607 100644 --- a/libbeat/template/load.go +++ b/libbeat/template/load.go @@ -55,7 +55,7 @@ type FileLoader struct { // FileClient defines the minimal interface required for the FileLoader type FileClient interface { GetVersion() common.Version - Write(name string, body string) error + Write(component string, name string, body string) error } // NewESLoader creates a new template loader for ES @@ -145,9 +145,8 @@ func (l *FileLoader) Load(config TemplateConfig, info beat.Info, fields []byte, return err } - p := common.MapStr{tmpl.name: body} - str := fmt.Sprintf("%s\n", p.StringToPrint()) - if err := l.client.Write(tmpl.name, str); err != nil { + str := fmt.Sprintf("%s\n", body.StringToPrint()) + if err := l.client.Write("template", tmpl.name, str); err != nil { return fmt.Errorf("error printing template: %v", err) } return nil diff --git a/libbeat/template/load_test.go b/libbeat/template/load_test.go index 22566229ab9..07f0ca7ad5b 100644 --- a/libbeat/template/load_test.go +++ b/libbeat/template/load_test.go @@ -63,14 +63,14 @@ func TestFileLoader_Load(t *testing.T) { require.NoError(t, err) body, err := buildBody(tmpl, test.cfg, test.fields) require.NoError(t, err) - assert.Equal(t, common.MapStr{test.name: body}.StringToPrint()+"\n", fc.body) + assert.Equal(t, body.StringToPrint()+"\n", fc.body) }) } } type fileClient struct { - ver common.Version - body string + ver common.Version + kind, name, body string } func newFileClient(ver string) (*fileClient, error) { @@ -88,7 +88,7 @@ func (c *fileClient) GetVersion() common.Version { return c.ver } -func (c *fileClient) Write(name string, body string) error { - c.body = body +func (c *fileClient) Write(component string, name string, body string) error { + c.kind, c.name, c.body = component, name, body return nil } diff --git a/libbeat/tests/system/test_ilm.py b/libbeat/tests/system/test_ilm.py index c016fba7129..2dcc11edd6f 100644 --- a/libbeat/tests/system/test_ilm.py +++ b/libbeat/tests/system/test_ilm.py @@ -286,9 +286,8 @@ def setUp(self): self.policy_name = self.beat_name + "-9.9.9" self.cmd = "ilm-policy" - def assert_log_contains_policy(self, policy): + def assert_log_contains_policy(self): assert self.log_contains('ILM policy successfully loaded.') - assert self.log_contains(policy) assert self.log_contains('"max_age": "30d"') assert self.log_contains('"max_size": "50gb"') @@ -304,7 +303,7 @@ def test_default(self): config=self.config) assert exit_code == 0 - self.assert_log_contains_policy(self.policy_name) + self.assert_log_contains_policy() self.assert_log_contains_write_alias() def test_load_disabled(self): @@ -316,7 +315,7 @@ def test_load_disabled(self): config=self.config) assert exit_code == 0 - self.assert_log_contains_policy(self.policy_name) + self.assert_log_contains_policy() self.assert_log_contains_write_alias() def test_changed_policy_name(self): @@ -330,5 +329,5 @@ def test_changed_policy_name(self): config=self.config) assert exit_code == 0 - self.assert_log_contains_policy(policy_name) + self.assert_log_contains_policy() self.assert_log_contains_write_alias() diff --git a/metricbeat/tests/system/test_template.py b/metricbeat/tests/system/test_template.py index 335c435119c..5899796524d 100644 --- a/metricbeat/tests/system/test_template.py +++ b/metricbeat/tests/system/test_template.py @@ -41,9 +41,7 @@ def test_export_template(self): break t = json.loads(template_content) - keys = [k for k, v in t.iteritems() if k.startswith("metricbeat")] - assert len(keys) == 1 - properties = t[keys[0]]["mappings"]["properties"] + properties = t["mappings"]["properties"] # Check libbeat fields assert properties["@timestamp"] == {"type": "date"}