Update stdlib
[mirror/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 given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
23
24     interfaces = lookupvar('interfaces')
25
26     # If we do not have any interfaces, then there are no requested attributes
27     return false if (interfaces == :undefined || interfaces.nil?)
28
29     interfaces = interfaces.split(',')
30
31     if args.size == 1
32       return interfaces.member?(args[0])
33     end
34
35     kind, value = args
36
37     # Bug with 3.7.1 - 3.7.3  when using future parser throws :undefined_variable
38     # https://tickets.puppetlabs.com/browse/PUP-3597
39     factval = nil
40     begin
41       catch :undefined_variable do
42         factval = lookupvar(kind)
43       end
44     rescue Puppet::ParseError # Eat the exception if strict_variables = true is set
45     end
46     if factval == value
47       return true
48     end
49
50     result = false
51     interfaces.each do |iface|
52       iface.downcase!
53       factval = nil
54       begin
55         # Bug with 3.7.1 - 3.7.3 when using future parser throws :undefined_variable
56         # https://tickets.puppetlabs.com/browse/PUP-3597
57         catch :undefined_variable do
58           factval = lookupvar("#{kind}_#{iface}")
59         end
60       rescue Puppet::ParseError # Eat the exception if strict_variables = true is set
61       end
62       if value == factval
63         result = true
64         break
65       end
66     end
67
68     result
69   end
70 end