File: cidr_to_netmask.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 (21 lines) | stat: -rw-r--r-- 830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

# Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned
#
# @summary Converts an CIDR address of the form 192.168.0.1/24 into its netmask.
#
Puppet::Functions.create_function(:'extlib::cidr_to_netmask') do
  # @param ip IPv6 or IPv4 address in CIDR notation
  # @return IPv6 or IPv4 netmask address
  # @example calling the function
  #   extlib::cidr_to_netmask('127.0.0.1/8')
  dispatch :cidr_to_netmask do
    param 'Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]', :ip
    return_type 'Variant[Stdlib::IP::Address::V4::Nosubnet,Stdlib::IP::Address::V6::Nosubnet]'
  end

  def cidr_to_netmask(ip)
    # IPAddr has no getter for the subnetmask, but inspect() returns it
    IPAddr.new(ip).inspect.gsub(%r{.*/(.*)>}, '\1')
  end
end