Skip to content
Snippets Groups Projects
Commit 1292ecfe authored by Thomas Bellman's avatar Thomas Bellman
Browse files

New function cartesian() for calculating cartesian products.

This function helps in defining iptables, where one may want
to create rules for e.g. several IP addresses, ports and/or
protocols, but otherwise similar.

This was originally commit 946039108a47 in nsc-puppet-utils.
parent 97159bb5
No related branches found
No related tags found
No related merge requests found
module Puppet::Parser::Functions
newfunction(:cartesian, :type => :rvalue, :doc => "\
Return the cartesian product of the arrays passed as arguments.
This can be used to create multiple variants of a string. It
works similar to the {} construct in some shells.
Example::
$in = [ 'eth0', 'eth3' ]
$out = [ 'eth2', 'eth7' ]
$cmd = cartesian('iptables -i ', $in, ' -o ', $out, ' -j REJECT')
$cmd now holds the list::
[ 'iptables -i eth0 -o eth2 -j REJECT',
'iptables -i eth0 -o eth7 -j REJECT',
'iptables -i eth3 -o eth2 -j REJECT',
'iptables -i eth3 -o eth7 -j REJECT' ]
A simple string as an argument is equivalent to a one element
long list of that string.
If any of the arguments is an empty list, the return value will
be an empty list.
") \
do |args|
result = [ "" ]
args.each do |a|
if not a.is_a?(Array)
a = [a]
end
newres = []
result.each do |p1|
a.each do |p2|
newres << p1+p2
end
end
result = newres
end
return result
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment