Skip to content

Commit

Permalink
Merge pull request #1 from lavagetto/master
Browse files Browse the repository at this point in the history
Use etcd-ruby for reqests instead of plain http.
  • Loading branch information
garethr committed Dec 21, 2013
2 parents dbb640c + 3945a7b commit e1ecba4
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions lib/hiera/backend/etcd_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ module Backend
class Etcd_backend

def initialize
require 'net/http'
require 'net/https'
require 'etcd'
require 'json'
@config = Config[:http]
@http = Net::HTTP.new(@config[:host], @config[:port])
@http.read_timeout = @config[:http_read_timeout] || 10
@http.open_timeout = @config[:http_connect_timeout] || 10
@client = Etcd.client(:host => @config[:host], :port => @config[:port])
end

def lookup(key, scope, order_override, resolution_type)
Expand All @@ -22,48 +19,56 @@ def lookup(key, scope, order_override, resolution_type)
paths.insert(0, order_override) if order_override

paths.each do |path|
Hiera.debug("[hiera-etcd]: Lookup #{@config[:host]}:#{@config[:port]}#{path}/#{key}")
httpreq = Net::HTTP::Get.new("#{path}/#{key}")

# If we can't connect to etcd at all we throw an exception and
# block the run
Hiera.debug("[hiera-etcd]: Lookup #{path}/#{key} on #{@config[:host]}:#{@config[:port]}")
begin
httpres = @http.request(httpreq)
rescue Exception => e
Hiera.warn("[hiera-etcd]: Net::HTTP threw exception #{e.message}")
raise Exception, e.message
result = @client.get("#{path}/#{key}")
rescue
Hiera.debug("[hiera-etcd]: bad request key")
next
end
answer = self.parse_result(result.value, resolution_type, scope)
next unless answer
break
end
answer
end

# If we don't find any results in etcd we continue on to the
# next path
unless httpres.kind_of?(Net::HTTPSuccess)
Hiera.debug("[hiera-etcd]: #{httpres.code} HTTP response from #{@config[:host]}:#{@config[:port]}#{path}/#{key}")
next
end

# On to the next path if we don't have a response
next unless httpres.body
# Parse result from standard etcd JSON response
result = JSON.parse(httpres.body)['value']
parsed_result = Backend.parse_answer(result, scope)
def parse_result(res, type, scope)
answer = nil
case type
when :array
answer ||= []
begin
data = Backend.parse_answer(JSON[res], scope)
rescue
Hiera.warn("[hiera-etcd]: '#{res}' is not in json format, and array lookup is requested")
return answer
end

# Format response as specified type, either array, hash or text
case resolution_type
when :array
answer ||= []
answer << parsed_result
when :hash
answer ||= {}
answer = parsed_result.merge answer
if data.is_a?(Array)
answer = data.clone
else
answer = parsed_result
break
Hiera.warn("Data is in json format, but this is not an array")
end
when :hash
answer ||= {}
begin
data = Backend.parse_answer(JSON[res], scope)
rescue
Hiera.warn("[hiera-etcd]: '#{res}' is not in json format, and hash lookup is requested")
return answer
end
if data.is_a?(Hash)
answer = data.clone
else
Hiera.warn("Data is in json format, but this is not an hash")
end
else
answer = Backend.parse_answer(res, scope)
end
answer
end

end
end
end

0 comments on commit e1ecba4

Please sign in to comment.