aboutsummaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/functions/extract_id.rb14
-rw-r--r--lib/puppet/parser/functions/list_to_zookeeper_hash.rb24
-rw-r--r--lib/puppet/parser/functions/validate_ip_address.rb53
3 files changed, 91 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/extract_id.rb b/lib/puppet/parser/functions/extract_id.rb
new file mode 100644
index 0000000..61734ab
--- /dev/null
+++ b/lib/puppet/parser/functions/extract_id.rb
@@ -0,0 +1,14 @@
+# Custom function to extract the index from a list.
+# The list are a list of hostname, and the index is the n'th
+# position of the host in list
+module Puppet::Parser::Functions
+ newfunction(:extract_id, :type => :rvalue) do |argv|
+ hosts = argv[0]
+ if hosts.class != Array
+ hosts = [hosts]
+ end
+ hostname = argv[1]
+ hash = Hash[hosts.map.with_index.to_a]
+ return hash[hostname].to_i + 1
+ end
+end
diff --git a/lib/puppet/parser/functions/list_to_zookeeper_hash.rb b/lib/puppet/parser/functions/list_to_zookeeper_hash.rb
new file mode 100644
index 0000000..814326e
--- /dev/null
+++ b/lib/puppet/parser/functions/list_to_zookeeper_hash.rb
@@ -0,0 +1,24 @@
+# Custom function to convert a list of ips to a map
+# like {'ip' => xxx.xxx.xxx.xxx }. This function is needed
+# because a not-so-good design of the puppet-midonet module
+# and we hope to deprecate it soon.
+
+module Puppet::Parser::Functions
+ newfunction(:list_to_zookeeper_hash, :type => :rvalue, :doc => <<-EOS
+ This function returns Zookeper configuration list of hash
+ EOS
+ ) do |argv|
+ zk_list = argv[0]
+ if zk_list.class != Array
+ zk_list = [zk_list]
+ end
+ result = Array.new
+ zk_list.each do |zk_ip|
+ zk_map = Hash.new
+ zk_map['ip'] = zk_ip
+ zk_map['port'] = 2181
+ result.push(zk_map)
+ end
+ return result
+ end
+end
diff --git a/lib/puppet/parser/functions/validate_ip_address.rb b/lib/puppet/parser/functions/validate_ip_address.rb
new file mode 100644
index 0000000..ebdf727
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ip_address.rb
@@ -0,0 +1,53 @@
+# TODO(devvesa): Remove the validation function once puppetlabs-stdlib maintainers
+# accept the pull request: https://github.com/puppetlabs/puppetlabs-stdlib/pull/546
+# This project should not maintain it.
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ip_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IP addresses,
+ regardless they are IPv4 or IPv6
+ Fail compilation if any value fails this check.
+ The following values will pass:
+ $my_ip = "1.2.3.4"
+ validate_ip_address($my_ip)
+ validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+
+ $my_ip = "3ffe:505:2"
+ validate_ip_address(1)
+ validate_ip_address($my_ip)
+ validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+ $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
+ validate_ip_address($some_array)
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ip_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv4? or IPAddr.new(arg).ipv6?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."
+ end
+ end
+
+ end
+
+end