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
# 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"]
  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)


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

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



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

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



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

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



63
64
65
# File 'lib/logstash/elasticsearch_client.rb', line 63

def host_settings
  @client_args[:hosts]
end

#put(path, content) ⇒ Object



79
80
81
# File 'lib/logstash/elasticsearch_client.rb', line 79

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