Class: LogStash::ElasticsearchClient::RubyClient

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

Instance Method Summary collapse

Methods included from Util::ManticoreSSLConfigHelper

#manticore_ssl_options_from_config

Constructor Details

#initialize(settings, logger) ⇒ RubyClient

Returns a new instance of RubyClient.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logstash/elasticsearch_client.rb', line 26

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

  ssl_options = manticore_ssl_options_from_config('elasticsearch', settings)

  @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)


47
48
49
50
51
52
53
54
55
# File 'lib/logstash/elasticsearch_client.rb', line 47

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



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/logstash/elasticsearch_client.rb', line 61

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



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/elasticsearch_client.rb', line 77

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



57
58
59
# File 'lib/logstash/elasticsearch_client.rb', line 57

def host_settings
  @client_args[:hosts]
end

#put(path, content) ⇒ Object



73
74
75
# File 'lib/logstash/elasticsearch_client.rb', line 73

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