Update stdlib and concat to 6.1.0 both
[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     @summary
7       Returns boolean based on kind and value.
8
9     @return
10       boolean values `true` or `false`
11
12     Valid kinds are `macaddress`, `netmask`, `ipaddress` and `network`.
13
14     @example **Usage**
15       has_interface_with("macaddress", "x:x:x:x:x:x") # Returns `false`
16       has_interface_with("ipaddress", "127.0.0.1") # Returns `true`
17
18     @example If no "kind" is given, then the presence of the interface is checked:
19       has_interface_with("lo") # Returns `true`
20     DOC
21              ) do |args|
22
23     raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments given (#{args.size} for 1 or 2)") if args.empty? || 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     begin
42       catch :undefined_variable do
43         factval = lookupvar(kind)
44       end
45     rescue Puppet::ParseError # rubocop:disable Lint/HandleExceptions : Eat the exception if strict_variables = true is set
46     end
47     if factval == value
48       return true
49     end
50
51     result = false
52     interfaces.each do |iface|
53       iface.downcase!
54       factval = nil
55       begin
56         # Bug with 3.7.1 - 3.7.3 when using future parser throws :undefined_variable
57         # https://tickets.puppetlabs.com/browse/PUP-3597
58         catch :undefined_variable do
59           factval = lookupvar("#{kind}_#{iface}")
60         end
61       rescue Puppet::ParseError # rubocop:disable Lint/HandleExceptions : Eat the exception if strict_variables = true is set
62       end
63       if value == factval
64         result = true
65         break
66       end
67     end
68
69     result
70   end
71 end