From d268b5099503d8416072395bb919b3226361bf7b Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Thu, 3 May 2012 12:56:02 +1000 Subject: [PATCH] Tomdoc the json provider for the sensu_api_config type --- lib/puppet/provider/sensu_api_config/json.rb | 52 +++++++++++++++----- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/puppet/provider/sensu_api_config/json.rb b/lib/puppet/provider/sensu_api_config/json.rb index 634d4c725b..825531b338 100644 --- a/lib/puppet/provider/sensu_api_config/json.rb +++ b/lib/puppet/provider/sensu_api_config/json.rb @@ -1,45 +1,75 @@ require 'json' Puppet::Type.type(:sensu_api_config).provide(:json) do - def initialize(*args) - super - - @conf = JSON.parse(File.read('/etc/sensu/config.json')) + # Internal: Retrieve the current contents of /etc/sensu/config.json. + # + # Returns a Hash representation of the JSON structure in + # /etc/sensu/config.json or an empty Hash if the file can not be read. + def conf + begin + @conf ||= JSON.parse(File.read('/etc/sensu/config.json')) + rescue + @conf ||= {} + end end + # Public: Save changes to the API section of /etc/sensu/config.json to disk. + # + # Returns nothing. def flush File.open('/etc/sensu/config.json', 'w') do |f| f.puts JSON.pretty_generate(@conf) end end + # Public: Create the API configuration section. + # + # Returns nothing. def create - @conf['api'] = {} + conf['api'] = {} self.port = resource[:port] self.host = resource[:host] end + # Public: Remove the API configuration section. + # + # Returns nothing. def destroy - @conf.delete 'api' + conf.delete 'api' end + # Public: Determine if the API configuration section is present. + # + # Returns a Boolean, true if present, false if absent. def exists? - @conf.has_key? 'api' + conf.has_key? 'api' end + # Public: Retrieve the port number that the API is configured to listen on. + # + # Returns the String port number. def port - @conf['api']['port'].to_s + conf['api']['port'].to_s end + # Public: Set the port that the API should listen on. + # + # Returns nothing. def port=(value) - @conf['api']['port'] = value.to_i + conf['api']['port'] = value.to_i end + # Public: Retrieve the hostname that the API is configured to listen on. + # + # Returns the String hostname. def host - @conf['api']['host'] + conf['api']['host'] end + # Public: Set the hostname that the API should listen on. + # + # Returns nothing. def host=(value) - @conf['api']['host'] = value + conf['api']['host'] = value end end