Skip to content

Commit cdd9652

Browse files
committed
Cleanup template naming
Also some test cleanup
1 parent e848f24 commit cdd9652

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/logstash/outputs/elasticsearch/template_manager.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ def self.add_ilm_settings_to_template(plugin, template)
3434
template['settings'].update({ 'index.lifecycle.name' => plugin.ilm_policy, 'index.lifecycle.rollover_alias' => plugin.ilm_rollover_alias})
3535
end
3636

37+
# Template name - if template_name set, use it
38+
# if not and ILM is enabled, use the rollover alias
39+
# else use the default value of template_name
3740
def self.template_name(plugin)
38-
(plugin.ilm_enabled? && !plugin.template_name) ? plugin.ilm_rollover_alias : plugin.template_name
41+
plugin.ilm_enabled? && !plugin.original_params.key?('template_name') ? plugin.ilm_rollover_alias : plugin.template_name
3942
end
4043

4144
def self.default_template_path(es_major_version)

spec/integration/outputs/ilm_spec.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,8 @@
399399
let (:ilm_rollover_alias) { "the_cat_in_the_hat" }
400400
let (:index) { ilm_rollover_alias }
401401
let(:expected_index) { index }
402-
let (:template_name) { "custom" }
403402
let (:settings) { super.merge("ilm_policy" => ilm_policy_name,
404403
"template" => template,
405-
"template_name" => template_name,
406404
"ilm_rollover_alias" => ilm_rollover_alias)}
407405

408406

@@ -419,6 +417,8 @@
419417
put_policy(@es,ilm_policy_name, policy)
420418
end
421419

420+
it_behaves_like 'an ILM enabled Logstash'
421+
422422
it 'should create the rollover alias' do
423423
expect(@es.indices.exists_alias(index: ilm_rollover_alias)).to be_falsey
424424
subject.register
@@ -442,12 +442,26 @@
442442
it 'should write the ILM settings into the template' do
443443
subject.register
444444
sleep(1)
445-
expect(@es.indices.get_template(name: template_name)[template_name]["index_patterns"]).to eq(["#{ilm_rollover_alias}-*"])
446-
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['name']).to eq(ilm_policy_name)
447-
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['rollover_alias']).to eq(ilm_rollover_alias)
445+
expect(@es.indices.get_template(name: ilm_rollover_alias)[ilm_rollover_alias]["index_patterns"]).to eq(["#{ilm_rollover_alias}-*"])
446+
expect(@es.indices.get_template(name: ilm_rollover_alias)[ilm_rollover_alias]["settings"]['index']['lifecycle']['name']).to eq(ilm_policy_name)
447+
expect(@es.indices.get_template(name: ilm_rollover_alias)[ilm_rollover_alias]["settings"]['index']['lifecycle']['rollover_alias']).to eq(ilm_rollover_alias)
448+
end
449+
450+
context 'with a different template_name' do
451+
let (:template_name) { "custom_template_name" }
452+
let (:settings) { super.merge('template_name' => template_name)}
453+
454+
it_behaves_like 'an ILM enabled Logstash'
455+
456+
it 'should write the ILM settings into the template' do
457+
subject.register
458+
sleep(1)
459+
expect(@es.indices.get_template(name: template_name)[template_name]["index_patterns"]).to eq(["#{ilm_rollover_alias}-*"])
460+
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['name']).to eq(ilm_policy_name)
461+
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['rollover_alias']).to eq(ilm_rollover_alias)
462+
end
448463
end
449464

450-
it_behaves_like 'an ILM enabled Logstash'
451465
end
452466
end
453467

@@ -467,22 +481,19 @@
467481
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
468482
end
469483

470-
it 'should write the ILM settings into the template' do
484+
it 'should not write the ILM settings into the template' do
471485
subject.register
472486
sleep(1)
473487
expect(@es.indices.get_template(name: "logstash")["logstash"]["index_patterns"]).to eq(["logstash-*"])
474488
expect(@es.indices.get_template(name: "logstash")["logstash"]["settings"]['index']['lifecycle']).to be_nil
475-
476489
end
477490

478-
479491
context 'with an existing policy that will roll over' do
480492
let (:policy) { small_max_doc_policy }
481493
let (:ilm_policy_name) { "3_docs"}
482494
let (:settings) { super.merge("ilm_policy" => ilm_policy_name)}
483495

484-
it 'should index documents normally' do
485-
496+
it 'should not roll over indices' do
486497
subject.register
487498
subject.multi_receive([
488499
LogStash::Event.new("message" => "sample message here"),
@@ -513,6 +524,19 @@
513524
expect(indexes_written.values.first).to eq(6)
514525
end
515526
end
527+
528+
context 'with a custom template name' do
529+
let (:template_name) { "custom_template_name" }
530+
let (:settings) { super.merge('template_name' => template_name)}
531+
532+
it 'should not write the ILM settings into the template' do
533+
subject.register
534+
sleep(1)
535+
expect(@es.indices.get_template(name: template_name)[template_name]["index_patterns"]).to eq(["logstash-*"])
536+
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']).to be_nil
537+
end
538+
end
539+
516540
end
517541
end
518542
end

0 commit comments

Comments
 (0)