Add new cidr2ip function
Also, a small typo bug is corrected in cidr2iface. Change-Id: I2e41ea8e6b7d80333b18cffab31b277497511ba2
This commit is contained in:
parent
6a64101cd0
commit
d13536855f
@ -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
|
# 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
|
# not use this file except in compliance with the License. You may obtain
|
||||||
@ -17,7 +17,10 @@ require 'socket'
|
|||||||
|
|
||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:cidr2iface, :type => :rvalue, :doc => <<-EOS
|
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
|
EOS
|
||||||
) do |argv|
|
) 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}$/
|
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
|
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
|
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
|
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")
|
raise ("No Matching iface found")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
34
lib/puppet/parser/functions/cidr2ip.rb
Normal file
34
lib/puppet/parser/functions/cidr2ip.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user