From d13536855f50e9614aaec02c3001374e1e786ef0 Mon Sep 17 00:00:00 2001 From: Alejandro Andreu Date: Fri, 3 Feb 2017 10:11:18 +0100 Subject: [PATCH] Add new cidr2ip function Also, a small typo bug is corrected in cidr2iface. Change-Id: I2e41ea8e6b7d80333b18cffab31b277497511ba2 --- lib/puppet/parser/functions/cidr2iface.rb | 9 ++++-- lib/puppet/parser/functions/cidr2ip.rb | 34 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 lib/puppet/parser/functions/cidr2ip.rb diff --git a/lib/puppet/parser/functions/cidr2iface.rb b/lib/puppet/parser/functions/cidr2iface.rb index 81e22c1..1328156 100644 --- a/lib/puppet/parser/functions/cidr2iface.rb +++ b/lib/puppet/parser/functions/cidr2iface.rb @@ -1,4 +1,4 @@ -# Copyright 2015 Midokura SARL, Inc. +# Copyright 2017 Midokura SARL, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -17,7 +17,10 @@ require 'socket' module Puppet::Parser::Functions newfunction(:cidr2iface, :type => :rvalue, :doc => <<-EOS - This function returns a iface name or will raise an error if no iface matches + This function returns an interface name or will raise an error if no iface + is configured with an IP address inside the specified CIDR. + + If multiples interfaces match, the first one is returned. EOS ) do |argv| unless argv[0] =~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/[0-9]{,2}$/ @@ -25,7 +28,7 @@ module Puppet::Parser::Functions end ifaces = Socket.getifaddrs.map { |i| {ip: i.addr.ip_address,name: i.name,cidr: NetAddr::CIDR::create("#{i.addr.ip_address} #{i.netmask.ip_address}").to_s } if i.addr.ipv4? }.compact matching_iface = ifaces.select{ |i| i[:cidr] == argv[0]}.first - return matching_iface[:name] unless matching_ifaces.nil? + return matching_iface[:name] unless matching_iface.nil? raise ("No Matching iface found") end end diff --git a/lib/puppet/parser/functions/cidr2ip.rb b/lib/puppet/parser/functions/cidr2ip.rb new file mode 100644 index 0000000..6e25bb8 --- /dev/null +++ b/lib/puppet/parser/functions/cidr2ip.rb @@ -0,0 +1,34 @@ +# Copyright 2017 Midokura SARL, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +require 'netaddr' +require 'socket' + +module Puppet::Parser::Functions + newfunction(:cidr2ip, :type => :rvalue, :doc => <<-EOS + This function returns an ip address or will raise an error if no iface + is configured with an IP address inside the specified CIDR. + + If multiples interfaces match, the first one is returned. + EOS + ) do |argv| + unless argv[0] =~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/[0-9]{,2}$/ + raise ArgumentError, "#{argv[0]} is not a valid CIDR" + end + ifaces = Socket.getifaddrs.map { |i| {ip: i.addr.ip_address,name: i.name,cidr: NetAddr::CIDR::create("#{i.addr.ip_address} #{i.netmask.ip_address}").to_s } if i.addr.ipv4? }.compact + matching_iface = ifaces.select{ |i| i[:cidr] == argv[0]}.first + return matching_iface[:ip] unless matching_iface.nil? + raise ("No Matching iface found") + end +end