Class: Jerakia::Datasource::Consul_kv

Inherits:
Instance
  • Object
show all
Defined in:
lib/jerakia/datasource/consul_kv.rb

Instance Attribute Summary

Attributes inherited from Instance

#options, #request, #response

Instance Method Summary collapse

Methods inherited from Instance

#answer, feature, #features?, #initialize, option, set_options, validate_options

Constructor Details

This class inherits a constructor from Jerakia::Datasource::Instance

Instance Method Details

#lookupObject

Entrypoint for Jerakia lookups starts here.


51
52
53
54
55
56
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
# File 'lib/jerakia/datasource/consul_kv.rb', line 51

def lookup
  
  Jerakia.log.debug("[datasource::console_kv] backend performing lookup for #{request.key}")
  paths = options[:searchpath].reject { |p| p.nil? }

  key = request.key
  namespace = request.namespace


  answer do |response|

    break if paths.empty?
    path = paths.shift.split('/').compact


    path << namespace
    path << key unless key.nil?

    diplomat_options = {
      :recurse => options[:recursive],
      :convert_to_hash => options[:to_hash],
      :dc => options[:datacenter],
    }

    begin
     key_path = path.flatten.join('/')
     Jerakia.log.debug("[datasource::consul_kv] Looking up #{key_path} with options #{diplomat_options}")
     result = Diplomat::Kv.get(key_path, diplomat_options)
    rescue Diplomat::KeyNotFound => e
      Jerakia.log.debug("NotFound encountered, skipping to next path entry")
      next
    rescue Faraday::ConnectionFailed => e
      raise Jerakia::Error, "Failed to connect to consul service: #{e.message}"
    end
    response.submit result
  end
end