Puppet Function: wmflib::cidr2mask

Defined in:
modules/wmflib/lib/puppet/functions/wmflib/cidr2mask.rb
Function type:
Ruby 4.x API

Summary

Convert a CIDR value to its associated netmask

Overview

wmflib::cidr2mask(String $cidr)str

Examples:

wmflib::cidr2mask("192.168.2.0/24")
=> "255.255.255.0"
wmflib::cidr2mask("2620:0:861:1::/64")
=> "ffff:ffff:ffff:ffff::"

Parameters:

  • cidr (String)

    The cidr to be converted

Returns:

  • (str)

    The String representation of the object



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
# File 'modules/wmflib/lib/puppet/functions/wmflib/cidr2mask.rb', line 6

Puppet::Functions.create_function(:'wmflib::cidr2mask') do
  # @summary
  #   Convert a CIDR value to its associated netmask
  # @example
  #   wmflib::cidr2mask("192.168.2.0/24")
  #   => "255.255.255.0"
  #   wmflib::cidr2mask("2620:0:861:1::/64")
  #   => "ffff:ffff:ffff:ffff::"
  # @param cidr
  #   The cidr to be converted
  #
  # @return [str]
  #   The String representation of the object
  dispatch :cidr2mask do
    param 'String', :cidr
  end

  def cidr2mask(cidr_s)
    # @param cidr_s
    #   The cidr to be converted
    #
    # @return [str]
    #   The String representation of the object
    cidr = IPAddr.new(cidr_s)
    IPAddr.new(cidr.instance_variable_get(:@mask_addr), cidr.family).to_s
  end
end