Class: LogStash::Outputs::OpenSearch::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/opensearch/template_manager.rb

Class Method Summary collapse

Class Method Details

.default_template_path(major_version, ecs_compatibility = :disabled, legacy_template = true) ⇒ Object



48
49
50
51
52
53
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 48

def self.default_template_path(major_version, ecs_compatibility=:disabled, legacy_template=true)
  template_version = major_version
  suffix = legacy_template ? "" : "_index"
  default_template_name = "templates/ecs-#{ecs_compatibility}/#{template_version}x#{suffix}.json"
  ::File.expand_path(default_template_name, ::File.dirname(__FILE__))
end

.install(client, template_name, template, template_overwrite) ⇒ Object



36
37
38
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 36

def self.install(client, template_name, template, template_overwrite)
  client.template_install(template_name, template, template_overwrite)
end

.install_template(plugin) ⇒ Object

To be mixed into the opensearch plugin base



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 13

def self.install_template(plugin)
  return unless plugin.manage_template
  if plugin.template
    plugin.logger.info("Using mapping template from", :path => plugin.template)
    template = read_template_file(plugin.template)
  else
    plugin.logger.info("Using a default mapping template", :version => plugin.maximum_seen_major_version,
                                                           :ecs_compatibility => plugin.ecs_compatibility)
    template = load_default_template(plugin.maximum_seen_major_version, plugin.ecs_compatibility, plugin.client.legacy_template?)
  end

  plugin.logger.debug("Attempting to install template", template: template)
  install(plugin.client, template_name(plugin), template, plugin.template_overwrite)
end

.load_default_template(major_version, ecs_compatibility, legacy_template) ⇒ Object



29
30
31
32
33
34
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 29

def self.load_default_template(major_version, ecs_compatibility, legacy_template)
  template_path = default_template_path(major_version, ecs_compatibility, legacy_template)
  read_template_file(template_path)
rescue => e
  fail "Failed to load default template for OpenSearch v#{major_version} with ECS #{ecs_compatibility}; caused by: #{e.inspect}"
end

.read_template_file(template_path) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 55

def self.read_template_file(template_path)
  raise ArgumentError, "Template file '#{template_path}' could not be found" unless ::File.exists?(template_path)
  template_data = ::IO.read(template_path)
  LogStash::Json.load(template_data)
end

.template_name(plugin) ⇒ Object



44
45
46
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 44

def self.template_name(plugin)
  plugin.template_name
end

.template_settings(plugin, template) ⇒ Object



40
41
42
# File 'lib/logstash/outputs/opensearch/template_manager.rb', line 40

def self.template_settings(plugin, template)
  template['settings']
end