-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tomdoc the json provider for the sensu_api_config type
- Loading branch information
Showing
1 changed file
with
41 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |