Module: Fluent::ElasticsearchIndexTemplate

Included in:
ElasticsearchOutput
Defined in:
lib/fluent/plugin/elasticsearch_index_template.rb

Instance Method Summary collapse

Instance Method Details

#get_template(template_file) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 3

def get_template(template_file)
  if !File.exists?(template_file)
    raise "If you specify a template_name you must specify a valid template file (checked '#{template_file}')!"
  end
  file_contents = IO.read(template_file).gsub(/\n/,'')
  JSON.parse(file_contents)
end

#template_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 11

def template_exists?(name)
  client.indices.get_template(:name => name)
  return true
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  return false
end

#template_install(name, template_file) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 22

def template_install(name, template_file)
  if !template_exists?(name)
    template_put(name, get_template(template_file))
    log.info("Template configured, but no template installed. Installed '#{name}' from #{template_file}.")
  else
    log.info("Template configured and already installed.")
  end
end

#template_put(name, template) ⇒ Object



18
19
20
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 18

def template_put(name, template)
  client.indices.put_template(:name => name, :body => template)
end

#templates_hash_install(templates) ⇒ Object



31
32
33
34
35
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 31

def templates_hash_install (templates)
  templates.each do |key, value|
    template_install(key, value)
  end
end