Class: LookupHttp

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LookupHttp

Returns a new instance of LookupHttp.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lookup_http.rb', line 3

def initialize(opts={})
  require 'net/http'
  require 'net/https'
  @config = opts

  @debug_log = @config[:debug_log]
  @http = Net::HTTP.new(@config[:host], @config[:port])
  @http.read_timeout = @config[:http_read_timeout] || 10
  @http.open_timeout = @config[:http_connect_timeout] || 10

  if @config[:use_ssl]
    @http.use_ssl = true

    if @config[:ssl_verify] == false
      @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end

    if @config[:ssl_cert]
      store = OpenSSL::X509::Store.new
      store.add_cert(OpenSSL::X509::Certificate.new(File.read(@config[:ssl_ca_cert])))
      @http.cert_store = store

      @http.key = OpenSSL::PKey::RSA.new(File.read(@config[:ssl_cert]))
      @http.cert = OpenSSL::X509::Certificate.new(File.read(@config[:ssl_key]))
    end
  else
    @http.use_ssl = false
  end
end

Instance Method Details

#get_parsed(path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lookup_http.rb', line 67

def get_parsed(path)
  httpreq = Net::HTTP::Get.new(path)

  if @config[:use_auth]
    httpreq.basic_auth @config[:auth_user], @config[:auth_pass]
  end

  if @config[:headers]
    @config[:headers].each do |name,content|
      httpreq.add_field name.to_s, content
    end
  end

  begin
    httpres = @http.request(httpreq)
  rescue Exception => e
    Hiera.warn("[lookup_http]: Net::HTTP threw exception #{e.message}")
    raise Exception, e.message unless @config[:failure] == 'graceful'
    return
  end

  unless httpres.kind_of?(Net::HTTPSuccess)
    log_debug("[lookup_http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
    log_debug("HTTP response code was #{httpres.code}")
    unless httpres.code == '404' && @config[:ignore_404]
      raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
    end
    return
  end

  parse_response httpres.body
end

#log_debug(msg) ⇒ Object

This allows us to pass Hiera.debug or Jerakia.log.debug to the lookup_http class we should find a better way to handle logging withing LookupHttp



102
103
104
105
106
# File 'lib/lookup_http.rb', line 102

def log_debug(msg)
  if @debug_log
    eval "#{@debug_log} '#{msg}'"
  end
end

#parse_json(answer) ⇒ Object

Handlers Here we define specific handlers to parse the output of the http request and return its structured representation. Currently we support YAML and JSON



55
56
57
58
59
# File 'lib/lookup_http.rb', line 55

def parse_json(answer)
  require 'rubygems'
  require 'json'
  JSON.parse(answer)
end

#parse_response(answer) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lookup_http.rb', line 35

def parse_response(answer)
  return unless answer

  format = @config[:output] || 'plain'
  log_debug("[lookup_http]: Query returned data, parsing response as #{format}")

  case format
  when 'json'
    parse_json answer
  when 'yaml'
    parse_yaml answer
  else
    answer
  end
end

#parse_yaml(answer) ⇒ Object



61
62
63
64
# File 'lib/lookup_http.rb', line 61

def parse_yaml(answer)
  require 'yaml'
  YAML.load(answer)
end