File: netmask_to_cidr.rb

package info (click to toggle)
puppet-module-extlib 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 1,035; sh: 15; makefile: 10
file content (20 lines) | stat: -rw-r--r-- 708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# frozen_string_literal: true

# @summary Converts an octet netmask address of the form 255.255.255.0 into its CIDR variant.
#          Thus making it directly usable with the values from facter.
#
Puppet::Functions.create_function(:'extlib::netmask_to_cidr') do
  # @param netmask IPv6 or IPv4 netmask in octet notation
  # @return CIDR / prefix length
  # @example calling the function
  #   extlib::netmask_to_cidr('255.0.0.0')
  dispatch :netmask_to_cidr do
    param 'Stdlib::IP::Address::Nosubnet', :netmask
    return_type 'Integer[0, 128]'
  end

  def netmask_to_cidr(netmask)
    dummy_addr = IPAddr.new(netmask).ipv6? ? '::' : '0.0.0.0'
    IPAddr.new("#{dummy_addr}/#{netmask}").prefix
  end
end