begin
  require 'zookeeper'
rescue LoadError
  Chef::Log.warn("Missing gem 'zookeeper'")
end

def get_zk()
  # todo: memoize
  return Zookeeper.new(@new_resource.connect_str)
end


action :create_if_missing do
  zk = get_zk()
  return if zk.stat(:path => @new_resource.path)[:stat].exists
  zk.create(:path => @new_resource.path, :data => @new_resource.data)
end

action :create do
  zk = get_zk()
  if zk.stat(:path => @new_resource.path)[:stat].exists
    zk.set(:path => @new_resource.path, :data => @new_resource.data)
  else
    zk.create(:path => @new_resource.path, :data => @new_resource.data)
  end
end
 
action :delete do
  zk = get_zk()
  zk.delete(:path => @new_resource.path)
end


def get_zk()
  require 'zookeeper'
  # todo: memoize
  return Zookeeper.new(@new_resource.connect_str)
end


action :create_if_missing do
  zk = get_zk()
  return if zk.stat(:path => @new_resource.path)[:stat].exists
  zk.create(:path => @new_resource.path, :data => @new_resource.data)
end

action :create do
  zk = get_zk()
  if zk.stat(:path => @new_resource.path)[:stat].exists
    zk.set(:path => @new_resource.path, :data => @new_resource.data)
  else
    zk.create(:path => @new_resource.path, :data => @new_resource.data)
  end
end
 
action :delete do
  zk = get_zk()
  zk.delete(:path => @new_resource.path)
end

Revert "put require in try/catch"
Revert "put require in try/catch"

This reverts commit 5ce207567b81245d50ac903bea75f9630ca9f733.

since this gets loaded a compile time, we cannot install the gem from a
recipe.
