Puppet Function: nets2cidr
- Defined in:
- lib/puppet/parser/functions/nets2cidr.rb
- Function type:
- Ruby 3.x API
Overview
Take an input Array
of networks and returns an equivalent
Array
in CIDR notation.
It can also accept a String
separated by spaces, commas, or
semicolons.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/puppet/parser/functions/nets2cidr.rb', line 2 newfunction(:nets2cidr, :type => :rvalue, :doc => <<-EOM) do |args| require File.(File.dirname(__FILE__) + '/../../../puppetx/simp/simplib.rb') Take an input `Array` of networks and returns an equivalent `Array` in CIDR notation. It can also accept a `String` separated by spaces, commas, or semicolons. @param networks [Variant[Array[String], String]] @return [Variant[Array[String], String]] EOM networks = Array(args.dup).flatten retval = Array.new # Try to be smart about pulling the string apart. networks = networks.map{|x| if !x.is_a?(Array) then x = x.split(/\s|,|;/).delete_if{ |y| y.empty? } end }.flatten networks.each do |lnet| # Skip any hostnames that we find. if PuppetX::SIMP::Simplib.hostname?(lnet) then retval << lnet next end begin ipaddr = IPAddr.new(lnet) rescue raise Puppet::ParseError,"nets2cidr: #{lnet} is not a valid IP address!" end # Just add it if it doesn't have a specified netmask. if lnet =~ /\// then retval << "#{ipaddr.to_s}/#{IPAddr.new(ipaddr.inspect.split('/').last.chop).to_i.to_s(2).count('1')}" else retval << lnet end end retval end |