Skip to content

Commit

Permalink
Updates docs for puppet functions (voxpupuli#208)
Browse files Browse the repository at this point in the history
* redisget() doc update
* getvar_emptystring() doc update
  • Loading branch information
petems committed May 19, 2017
1 parent b071293 commit ad7a42c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
34 changes: 19 additions & 15 deletions lib/puppet/parser/functions/getvar_emptystring.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
module Puppet::Parser::Functions

newfunction(:getvar_emptystring, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
Lookup a variable in a remote namespace.
Return an empty string rather than undef/nil if it could not be found
For example:
$foo = getvar('site::data::foo')
# Equivalent to $foo = $site::data::foo
This is useful if the namespace itself is stored in a string:
$datalocation = 'site::data'
$bar = getvar("${datalocation}::bar")
# Equivalent to $bar = $site::data::bar
ENDHEREDOC
newfunction(:getvar_emptystring, :type => :rvalue, :doc => <<-DOC
Return a variable in a remote namespace, but returns an empty
string if the variable is undefined or can't be found.
This is useful when trying to lookup a value that might be undefined
on first run, like a fact for an application that will be installed as
part of a Puppet run, but you don't want the reference to an undefined variable
to cause an error when strict variables are enabled.
@param variable [String] The variable to lookup
@return [String] The value of the variable if found
@return [String] An empty string if the variable is not found eg. `''`
@example Checking a variable exists (Equivalent to $foo = $site::data::foo)
$foo = getvar('site::data::foo')
@example Calling the function. (Equivalent to $bar = $site::data::bar)
$datalocation = 'site::data'
$bar = getvar("${datalocation}::bar")
@example Combining with the pick() function
$redis_version_real = pick(getvar_emptystring('redis_server_version'), '3.2.1')
DOC
) do |args|

unless args.length == 1
raise Puppet::ParseError, ("getvar_emptystring(): wrong number of arguments (#{args.length}; must be 1)")
Expand Down
16 changes: 11 additions & 5 deletions lib/puppet/parser/functions/redisget.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
require 'redis'

module Puppet::Parser::Functions
newfunction(:redisget, :type => :rvalue, :doc => <<-EOS
Returns the value of the key being looked up or nil if the key does not
exist. Takes two arguments, the first being a string value of the key to be
looked up and the second is the URL to the Redis service.
EOS
newfunction(:redisget, :type => :rvalue, :doc => <<-DOC
Returns the value of the key being looked up or nil if the key does not
exist. Takes two arguments, the first being a string value of the key to be
looked up and the second is the URL to the Redis service.
@param redis_key [String] The key to look up in redis.
@param redis_uri [String] The endpoint of the Redis instance.
@return [String] The value of the key from redis
@return [String] An empty string eg. `''`
@example Calling the function.
$version = redisget('version.myapp', 'redis://redis.example.com:6379')
DOC
) do |args|

raise(Puppet::ParseError, "redisget(): Wrong number of arguments given (#{args.size} for 2)") if args.size != 2
Expand Down

0 comments on commit ad7a42c

Please sign in to comment.