]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/has_interface_with.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / has_interface_with.rb
1 #
2 # has_interface_with
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:has_interface_with, :type => :rvalue, :doc => <<-EOS
7 Returns boolean based on kind and value:
8   * macaddress
9   * netmask
10   * ipaddress
11   * network
12
13 has_interface_with("macaddress", "x:x:x:x:x:x")
14 has_interface_with("ipaddress", "127.0.0.1")    => true
15 etc.
16
17 If no "kind" is given, then the presence of the interface is checked:
18 has_interface_with("lo")                        => true
19     EOS
20   ) do |args|
21
22     raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " +
23           "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
24
25     interfaces = lookupvar('interfaces')
26
27     # If we do not have any interfaces, then there are no requested attributes
28     return false if (interfaces == :undefined || interfaces.nil?)
29
30     interfaces = interfaces.split(',')
31
32     if args.size == 1
33       return interfaces.member?(args[0])
34     end
35
36     kind, value = args
37
38     # Bug with 3.7.1 - 3.7.3  when using future parser throws :undefined_variable
39     # https://tickets.puppetlabs.com/browse/PUP-3597
40     factval = nil
41     catch :undefined_variable do
42       factval = lookupvar(kind)
43     end
44     if factval == value
45       return true
46     end
47
48     result = false
49     interfaces.each do |iface|
50       iface.downcase!
51       factval = nil
52       begin
53         # Bug with 3.7.1 - 3.7.3 when using future parser throws :undefined_variable
54         # https://tickets.puppetlabs.com/browse/PUP-3597
55         catch :undefined_variable do
56           factval = lookupvar("#{kind}_#{iface}")
57         end
58       rescue Puppet::ParseError # Eat the exception if strict_variables = true is set
59       end
60       if value == factval
61         result = true
62         break
63       end
64     end
65
66     result
67   end
68 end