Skip to content

Commit

Permalink
Fix sensu_handler and others from recreating each run
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnf committed Aug 16, 2014
1 parent 89dde9e commit 408e967
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 63 deletions.
44 changes: 21 additions & 23 deletions lib/puppet/provider/sensu_client_config/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -47,71 +45,71 @@ def destroy
end

def exists?
@conf.has_key?('client')
conf.has_key?('client')
end

def check_args
['name', 'address', 'subscriptions', 'safe_mode', 'bind']
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

Expand Down
18 changes: 8 additions & 10 deletions lib/puppet/provider/sensu_client_subscription/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,23 +23,23 @@ def config_file
end

def create
@conf['client'] = {'subscriptions' => [ resource[:name] ] }
conf['client'] = {'subscriptions' => [ resource[:name] ] }
end

def destroy
@conf = nil
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

58 changes: 28 additions & 30 deletions lib/puppet/provider/sensu_handler/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 408e967

Please sign in to comment.