Skip to content

Commit 12f2211

Browse files
committed
New params_lookup function. Moved url_parse from stdlib42
1 parent 5a14a00 commit 12f2211

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# params_lookup.rb
3+
#
4+
# This function lookups for a variable value in various locations
5+
# following this order
6+
# - Hiera backend, if present
7+
# - ::varname (if option abs_lookup is true)
8+
# - ::modulename_varname
9+
# - ::modulename::params::varname
10+
# - The value set with the "default" option
11+
#
12+
# It's based on a suggestion of Dan Bode on how to better manage
13+
# Example42 NextGen modules params lookups.
14+
# Code has been written with the decisive help of Brice Figureau,
15+
# Peter Meier and Ohad Levy during the Fosdem 2012 days (thanks guys)
16+
#
17+
# Alessandro Franceschi al@lab42.it
18+
#
19+
module Puppet::Parser::Functions
20+
newfunction(:params_lookup, :type => :rvalue, :doc => <<-EOS
21+
This fuction looks for the given variable name in a set of different sources:
22+
- Hiera, if available ('varname' if option abs_lookup is true)
23+
- Hiera, if available ('modulename_varname')
24+
- ::varname (if option abs_lookup is true)
25+
- ::modulename_varname
26+
- ::modulename::params::varname
27+
- The value set with the "default" option
28+
EOS
29+
) do |arguments|
30+
31+
raise(Puppet::ParseError, "params_lookup(): Define at least the variable name " +
32+
"given (#{arguments.size} for 1)") if arguments.size < 1
33+
34+
value = :undef
35+
var_name = arguments[0]
36+
options = { 'default' => :undef, 'abs_lookup' => false }.merge(arguments[1] || {})
37+
module_name = parent_module_name
38+
if Puppet::Parser::Functions.function('hiera')
39+
value = function_hiera("#{var_name}",:undef) if options["abs_lookup"]
40+
value = function_hiera("#{module_name}_#{var_name}",:undef)
41+
end
42+
if value == :undef
43+
value = undef_as(:undef, lookupvar("::#{var_name}")) if options["abs_lookup"]
44+
value = undef_as(:undef, lookupvar("::#{module_name}_#{var_name}"))
45+
value = undef_as(:undef, lookupvar("::#{module_name}::params::#{var_name}")) if value == :undef
46+
value = undef_as(:undef, options["default"]) if value == :undef
47+
end
48+
49+
return value
50+
end
51+
end
52+
53+
# vim: set ts=2 sw=2 et :
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'uri'
2+
3+
Puppet::Parser::Functions::newfunction(:url_parse, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
4+
Returns information about an url
5+
6+
This function expects two arguments, an URL and the part of the url you want to retrieve.
7+
8+
Example:
9+
$source_filename = url_parse($source_url,path)
10+
11+
Given an url like: https://my_user:my_pass@www.example.com:8080/path/to/file.php?id=1&ret=0
12+
You obtain the following results according to the second argument:
13+
scheme : https
14+
userinfo : my_user:my_pass
15+
user : my_user
16+
password : my_pass
17+
host : www.example.com
18+
port : 8080
19+
path : /path/to/file.php
20+
query : id=1&ret=0
21+
filename : file.php
22+
filetype : php
23+
filedir : file
24+
25+
26+
ENDHEREDOC
27+
raise ArgumentError, ("url_parse(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2
28+
url=URI.parse args[0]
29+
case args[1]
30+
when 'scheme' then url.scheme
31+
when 'userinfo' then url.userinfo
32+
when 'user' then url.user
33+
when 'password' then url.password
34+
when 'host' then url.host
35+
when 'port' then url.port
36+
when 'path' then url.path
37+
when 'query' then url.query
38+
when 'filename' then File.basename url.path
39+
when 'filetype' then File.extname url.path
40+
when 'filedir' then (File.basename url.path).chomp(File.extname(url.path))
41+
else url
42+
end
43+
end
44+

0 commit comments

Comments
 (0)