require 'patron' require 'json' require 'base64' class Registry def getKeyValue (key) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' # patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end #sess.enable_debug resp = sess.get("/#{key}") # puts resp.body if resp.status != 200 abort("problem #{resp.status_line}") end result = JSON.parse(resp.body) return Base64.decode64(result[0]["Value"]) end def getValue (root, key) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' # patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end #sess.enable_debug resp = sess.get("/#{root}/#{key}") # puts resp.body if resp.status != 200 abort("problem #{resp.status_line}") end result = JSON.parse(resp.body) return Base64.decode64(result[0]["Value"]) end def exists (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.get("/#{root}") puts resp.body if resp.status == 200 abort("problem #{resp.status_line}") end end def deleteDirectory (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.delete("/#{root}?&recurse=true") puts resp.body if resp.status != 200 abort("problem #{resp.status_line}") end end def registerDirectory (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.put("/#{root}?dir=true", "") puts resp.body if resp.status > 202 abort("problem #{resp.status_line}") end end def register (root, key, value) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end #sess.enable_debug resp = sess.put("/#{root}/#{key}", "#{value}") puts resp.body if resp.status > 202 abort("problem #{resp.status_line}") end end end