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 | begin
|
---|
42 | catch :undefined_variable do
|
---|
43 | factval = lookupvar(kind)
|
---|
44 | end
|
---|
45 | rescue Puppet::ParseError # 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 # 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
|
---|