From 408e9672ce82deb0b22112633c8bcff4c3500264 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Sat, 16 Aug 2014 19:08:35 +1000 Subject: [PATCH] Fix sensu_handler and others from recreating each run Make it more like sensu_check which has a conf method. Looks like these were fixed at some earlier stage. The problem is that resource[:base_path] doesn't exist when the initializer is called. --- .../provider/sensu_client_config/json.rb | 44 +++++++------- .../sensu_client_subscription/json.rb | 18 +++--- lib/puppet/provider/sensu_handler/json.rb | 58 +++++++++---------- 3 files changed, 57 insertions(+), 63 deletions(-) diff --git a/lib/puppet/provider/sensu_client_config/json.rb b/lib/puppet/provider/sensu_client_config/json.rb index 22cbe1f17a..46bc17d3fa 100644 --- a/lib/puppet/provider/sensu_client_config/json.rb +++ b/lib/puppet/provider/sensu_client_config/json.rb @@ -12,19 +12,17 @@ confine :feature => :json include Puppet_X::Sensu::Totype - def initialize(*args) - super - + def conf begin - @conf = JSON.parse(File.read(config_file)) + @conf ||= JSON.parse(File.read(config_file)) rescue - @conf = {} + @conf ||= {} end end def flush File.open(config_file, 'w') do |f| - f.puts JSON.pretty_generate(@conf) + f.puts JSON.pretty_generate(conf) end end @@ -33,7 +31,7 @@ def config_file end def create - @conf['client'] = {} + conf['client'] = {} self.client_name = resource[:client_name] self.address = resource[:address] self.bind = resource[:bind] @@ -47,7 +45,7 @@ def destroy end def exists? - @conf.has_key?('client') + conf.has_key?('client') end def check_args @@ -55,63 +53,63 @@ def check_args end def client_name - @conf['client']['name'] + conf['client']['name'] end def client_name=(value) - @conf['client']['name'] = value + conf['client']['name'] = value end def address - @conf['client']['address'] + conf['client']['address'] end def address=(value) - @conf['client']['address'] = value + conf['client']['address'] = value end def bind - @conf['client']['bind'] + conf['client']['bind'] end def bind=(value) - @conf['client']['bind'] = value + conf['client']['bind'] = value end def subscriptions - @conf['client']['subscriptions'] || [] + conf['client']['subscriptions'] || [] end def subscriptions=(value) - @conf['client']['subscriptions'] = value + conf['client']['subscriptions'] = value end def custom - @conf['client'].reject { |k,v| check_args.include?(k) } + conf['client'].reject { |k,v| check_args.include?(k) } end def custom=(value) - @conf['client'].delete_if { |k,v| not check_args.include?(k) } - @conf['client'].merge!(to_type(value)) + conf['client'].delete_if { |k,v| not check_args.include?(k) } + conf['client'].merge!(to_type(value)) end def safe_mode - case @conf['client']['safe_mode'] + case conf['client']['safe_mode'] when true :true when false :false else - @conf['client']['safe_mode'] + conf['client']['safe_mode'] end end def safe_mode=(value) case value when true, 'true', 'True', :true, 1 - @conf['client']['safe_mode'] = true + conf['client']['safe_mode'] = true else - @conf['client']['safe_mode'] = false + conf['client']['safe_mode'] = false end end diff --git a/lib/puppet/provider/sensu_client_subscription/json.rb b/lib/puppet/provider/sensu_client_subscription/json.rb index 0b11813385..69a7e1002e 100644 --- a/lib/puppet/provider/sensu_client_subscription/json.rb +++ b/lib/puppet/provider/sensu_client_subscription/json.rb @@ -4,19 +4,17 @@ Puppet::Type.type(:sensu_client_subscription).provide(:json) do confine :feature => :json - def initialize(*args) - super - + def conf begin - @conf = JSON.parse(File.read(config_file)) + @conf ||= JSON.parse(File.read(config_file)) rescue - @conf = {} + @conf ||= {} end end def flush File.open(config_file, 'w') do |f| - f.puts JSON.pretty_generate(@conf) + f.puts JSON.pretty_generate(conf) end end @@ -25,7 +23,7 @@ def config_file end def create - @conf['client'] = {'subscriptions' => [ resource[:name] ] } + conf['client'] = {'subscriptions' => [ resource[:name] ] } end def destroy @@ -33,15 +31,15 @@ def destroy end def subscriptions - @conf['client']['subscriptions'] + conf['client']['subscriptions'] end def subscriptions=(value) - @conf['client']['subscriptions'] = value + conf['client']['subscriptions'] = value end def exists? - @conf.has_key?('client') && @conf['client'].has_key?('subscriptions') + conf.has_key?('client') && conf['client'].has_key?('subscriptions') end end diff --git a/lib/puppet/provider/sensu_handler/json.rb b/lib/puppet/provider/sensu_handler/json.rb index ffb6563136..9b0d57b945 100644 --- a/lib/puppet/provider/sensu_handler/json.rb +++ b/lib/puppet/provider/sensu_handler/json.rb @@ -4,25 +4,23 @@ Puppet::Type.type(:sensu_handler).provide(:json) do confine :feature => :json - def initialize(*args) - super - + def conf begin - @conf = JSON.parse(File.read(config_file)) + @conf ||= JSON.parse(File.read(config_file)) rescue - @conf = {} + @conf ||= {} end end def flush File.open(config_file, 'w') do |f| - f.puts JSON.pretty_generate(@conf) + f.puts JSON.pretty_generate(conf) end end def create - @conf['handlers'] = {} - @conf['handlers'][resource[:name]] = {} + conf['handlers'] = {} + conf['handlers'][resource[:name]] = {} self.command = resource[:command] self.type = resource[:type] # Optional arguments @@ -44,85 +42,85 @@ def destroy end def exists? - @conf.has_key?('handlers') and @conf['handlers'].has_key?(resource[:name]) + conf.has_key?('handlers') and conf['handlers'].has_key?(resource[:name]) end def command - @conf['handlers'][resource[:name]]['command'] + conf['handlers'][resource[:name]]['command'] end def command=(value) - @conf['handlers'][resource[:name]]['command'] = value + conf['handlers'][resource[:name]]['command'] = value end def config - @conf[resource[:name]] + conf[resource[:name]] end def config=(value) - @conf[resource[:name]] = value + conf[resource[:name]] = value end def exchange - @conf['handlers'][resource[:name]]['exchange'] + conf['handlers'][resource[:name]]['exchange'] end def exchange=(value) - @conf['handlers'][resource[:name]]['exchange'] = value + conf['handlers'][resource[:name]]['exchange'] = value end def socket - @conf['handlers'][resource[:name]]['socket'] + conf['handlers'][resource[:name]]['socket'] end def socket=(value) - @conf['handlers'][resource[:name]]['socket'] = value - if @conf['handlers'][resource[:name]]['socket'].has_key?('port') - @conf['handlers'][resource[:name]]['socket']['port'] = @conf['handlers'][resource[:name]]['socket']['port'].to_i + conf['handlers'][resource[:name]]['socket'] = value + if conf['handlers'][resource[:name]]['socket'].has_key?('port') + conf['handlers'][resource[:name]]['socket']['port'] = conf['handlers'][resource[:name]]['socket']['port'].to_i end end def handlers - @conf['handlers'][resource[:name]]['handlers'] + conf['handlers'][resource[:name]]['handlers'] end def handlers=(value) - @conf['handlers'][resource[:name]]['handlers'] = value + conf['handlers'][resource[:name]]['handlers'] = value end def mutator - @conf['handlers'][resource[:name]]['mutator'] + conf['handlers'][resource[:name]]['mutator'] end def mutator=(value) - @conf['handlers'][resource[:name]]['mutator'] = value + conf['handlers'][resource[:name]]['mutator'] = value end def filters - @conf['filters'][resource[:name]]['filters'] + conf['filters'][resource[:name]]['filters'] end def filters=(value) if value.is_a?(Array) - @conf['handlers'][resource[:name]]['filters'] = value + conf['handlers'][resource[:name]]['filters'] = value else - @conf['handlers'][resource[:name]]['filters'] = [ value ] + conf['handlers'][resource[:name]]['filters'] = [ value ] end end def severities - @conf['handlers'][resource[:name]]['severities'] + conf['handlers'][resource[:name]]['severities'] end def severities=(value) - @conf['handlers'][resource[:name]]['severities'] = value + conf['handlers'][resource[:name]]['severities'] = value end def type - @conf['handlers'][resource[:name]]['type'] + conf['handlers'][resource[:name]]['type'] end def type=(value) - @conf['handlers'][resource[:name]]['type'] = value + conf['handlers'][resource[:name]]['type'] = value end end