Method: Inspec::Resources::Elasticsearch#initialize

Defined in:
lib/resources/elasticsearch.rb

#initialize(opts = {}) ⇒ Elasticsearch

Returns a new instance of Elasticsearch.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/resources/elasticsearch.rb', line 57

def initialize(opts = {})
  return skip_resource 'Package `curl` not avaiable on the host' unless inspec.command('curl').exist?

  @url = opts.fetch(:url, 'http://localhost:9200')

  username   = opts.fetch(:username, nil)
  password   = opts.fetch(:password, nil)
  ssl_verify = opts.fetch(:ssl_verify, true)

  cmd = inspec.command(curl_command_string(username, password, ssl_verify))

  # after implementation of PR #2235, this begin..rescue won't be necessary.
  # The checks in verify_curl_success! can raise their own skip message exception.
  begin
    verify_curl_success!(cmd)
  rescue => e
    return skip_resource e.message
  end

  begin
    content = JSON.parse(cmd.stdout)
    # after implementation of PR #2235, this can be broken out of the begin..rescue
    # clause. The checks in verify_json_payload! can raise their own skip message exception.
    verify_json_payload!(content)
  rescue JSON::ParserError => e
    return skip_resource "Couldn't parse the Elasticsearch response: #{e.message}"
  rescue => e
    return skip_resource e.message
  end

  @nodes = parse_cluster(content)
end