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
# File 'lib/logstash/elasticsearch_client.rb', line 25

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

  ssl_options = {}

  if @settings["var.elasticsearch.ssl.enabled"] == "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"]
  password = @settings["var.elasticsearch.password"]
  if username
    @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)


50
51
52
53
54
55
56
57
58
# File 'lib/logstash/elasticsearch_client.rb', line 50

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



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logstash/elasticsearch_client.rb', line 64

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

#head(path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/logstash/elasticsearch_client.rb', line 80

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



60
61
62
# File 'lib/logstash/elasticsearch_client.rb', line 60

def host_settings
  @client_args[:hosts]
end

#put(path, content) ⇒ Object



76
77
78
# File 'lib/logstash/elasticsearch_client.rb', line 76

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