Skip to content

Commit

Permalink
Tomdoc the json provider for the sensu_api_config type
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed May 3, 2012
1 parent 874418c commit d268b50
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions lib/puppet/provider/sensu_api_config/json.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d268b50

Please sign in to comment.