Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert printing template and policy name on export. #12067

Merged
merged 8 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
- Reduce idxmgmt.Supporter interface and rework export commands to reuse logic. {pull}11777[11777], {pull}12067[12067]
- Update urllib3 version to 1.24.2 {pull}11930[11930]
15 changes: 8 additions & 7 deletions libbeat/cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ 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
}

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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions libbeat/idxmgmt/ilm/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions libbeat/idxmgmt/ilm/client_handler_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/ilm/ilm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions libbeat/idxmgmt/ilm/ilm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
7 changes: 3 additions & 4 deletions libbeat/template/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions libbeat/template/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
4 changes: 1 addition & 3 deletions metricbeat/tests/system/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down