aboutsummaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/functions/netmask_to_cidr.rb
blob: 68ee4cf3d7b7b20a84df673d09ff6bb8e990809f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Custom function to transform netmask from IP notation to
# CIDR format. Input is an IP address, output a CIDR:
# 255.255.255.0 = 24
# The CIDR formated netmask is needed for some
# Contrail configuration files
require 'ipaddr'
module Puppet::Parser::Functions
  newfunction(:netmask_to_cidr, :type => :rvalue) do |args|
    if args[0].class != String
      raise Puppet::ParseError, "Syntax error: #{args[0]} must be a String"
    end
    IPAddr.new(args[0]).to_i.to_s(2).count("1")
  end
end