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