Class: LogStash::ElasticsearchClient::RubyClient

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/elasticsearch_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings, logger) ⇒ RubyClient

Returns a new instance of RubyClient.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/elasticsearch_client.rb', line 25

def initialize(settings, logger)
  @settings = settings
  @logger = logger
  @client_args = client_args

  ssl_options = {}

  # boolean settings may be strings if set through the cli
  # or booleans if set through the yaml file, so we use .to_s
  if @settings["var.elasticsearch.ssl.enabled"].to_s == "true"
    ssl_options[:verify] = @settings.fetch("var.elasticsearch.ssl.verification_mode", true)
    ssl_options[:ca_file] = @settings.fetch("var.elasticsearch.ssl.certificate_authority", nil)
    ssl_options[:client_cert] = @settings.fetch("var.elasticsearch.ssl.certificate", nil)
    ssl_options[:client_key] = @settings.fetch("var.elasticsearch.ssl.key", nil)
  end

  @client_args[:ssl] = ssl_options

  username = @settings["var.elasticsearch.username"]
  if username
    password = @settings["var.elasticsearch.password"]
    if password.is_a?(LogStash::Util::Password)
      password = password.value
    end
    @client_args[:transport_options] = { :headers => { "Authorization" => 'Basic ' + Base64.encode64( "#{username}:#{password}" ).chomp } }
  end

  @client = Elasticsearch::Client.new(@client_args)
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/logstash/elasticsearch_client.rb', line 55

def can_connect?
  begin
    head(SecureRandom.hex(32).prepend('_'))
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
    true
  rescue Manticore::SocketException
    false
  end
end

#delete(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/logstash/elasticsearch_client.rb', line 69

def delete(path)
  begin
    normalize_response(@client.perform_request('DELETE', path, {}, nil))
  rescue Exception => e
    if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
      Response.new(404, "", {})
    else
      raise e
    end
  end
end

#head(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/logstash/elasticsearch_client.rb', line 85

def head(path)
  begin
    normalize_response(@client.perform_request('HEAD', path, {}, nil))
  rescue Exception => e
    if is_404_error?(e)
      Response.new(404, "", {})
    else
      raise e
    end
  end
end

#host_settingsObject



65
66
67
# File 'lib/logstash/elasticsearch_client.rb', line 65

def host_settings
  @client_args[:hosts]
end

#put(path, content) ⇒ Object



81
82
83
# File 'lib/logstash/elasticsearch_client.rb', line 81

def put(path, content)
  normalize_response(@client.perform_request('PUT', path, {}, content))
end