aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDan Prince <dprince@redhat.com>2015-05-29 10:27:01 -0400
committerDan Prince <dprince@redhat.com>2015-06-03 10:51:29 -0400
commit10ac6980f93ca33e279359c29c3cc19151b79f32 (patch)
treea909f97bf3c675f1d720ff5a0ad0feaa305d76e6 /lib
parent8b945b5e0ff93af2ec1c6540d6177739ab846c99 (diff)
Add interface_for_ip function
This patch adds a custom Puppet function called interface_for_ip This function will be used within the TripleO puppet implementation to help obtain the correct interface for a given IP address. Change-Id: I0979f69a49052fda888277fa64ebeadc038bc778
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/functions/interface_for_ip.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/interface_for_ip.rb b/lib/puppet/parser/functions/interface_for_ip.rb
new file mode 100644
index 0000000..1c67120
--- /dev/null
+++ b/lib/puppet/parser/functions/interface_for_ip.rb
@@ -0,0 +1,33 @@
+require 'ipaddr'
+
+# Custom function to lookup the interface which matches the subnet
+# of the provided IP address.
+# The function iterates over all the interfaces and chooses the
+# first locally assigned interface which matches the IP.
+module Puppet::Parser::Functions
+ newfunction(:interface_for_ip, :type => :rvalue, :doc => "Find the bind IP address for the provided subnet.") do |arg|
+ if arg[0].class == String
+ begin
+ ip_to_find = arg[0]
+ Dir.foreach('/sys/class/net/') do |interface|
+ next if interface == '.' or interface == '..'
+ iface_no_dash = interface.gsub('-', '_')
+ interface_ip = lookupvar("ipaddress_#{iface_no_dash}")
+ netmask = lookupvar("netmask_#{iface_no_dash}")
+ if not interface_ip.nil? then
+ ip1=IPAddr.new(interface_ip)
+ ip2=IPAddr.new(ip_to_find)
+ if ip1.mask(netmask) == ip2.mask(netmask) then
+ return interface
+ end
+ end
+ end
+ rescue JSON::ParserError
+ raise Puppet::ParseError, "Syntax error: #{arg[0]} is invalid"
+ end
+ else
+ raise Puppet::ParseError, "Syntax error: #{arg[0]} is not a String"
+ end
+ return ''
+ end
+end