-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modernise
has_interface_with
function
Convert the function to the modern function API as a namespaced function and use the `networking` fact instead of legacy facts. A non-namespaced shim is also created (but marked deprecated), to preserve compatibility.
- Loading branch information
1 parent
39f1e80
commit 1dd922d
Showing
4 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary DEPRECATED. Use the namespaced function [`stdlib::has_interface_with`](#stdlibhas_interface_with) instead. | ||
Puppet::Functions.create_function(:has_interface_with) do | ||
dispatch :deprecation_gen do | ||
repeated_param 'Any', :args | ||
end | ||
def deprecation_gen(*args) | ||
call_function('deprecation', 'has_interface_with', 'This method is deprecated, please use stdlib::has_interface_with instead.') | ||
call_function('stdlib::has_interface_with', *args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary Returns boolean based on network interfaces present and their attribute values. | ||
# | ||
# Can be called with one, or two arguments. | ||
Puppet::Functions.create_function(:'stdlib::has_interface_with') do | ||
# @param interface | ||
# The name of an interface | ||
# @return [Boolean] Returns `true` if `interface` exists and `false` otherwise | ||
# @example When called with a single argument, the presence of the interface is checked | ||
# stdlib::has_interface_with('lo') # Returns `true` | ||
dispatch :has_interface do | ||
param 'String[1]', :interface | ||
return_type 'Boolean' | ||
end | ||
|
||
# @param kind | ||
# A supported interface attribute | ||
# @param value | ||
# The value of the attribute | ||
# @return [Boolean] Returns `true` if any of the interfaces in the `networking` fact has a `kind` attribute with the value `value`. Otherwise returns `false` | ||
# @example Checking if an interface exists with a given mac address | ||
# stdlib::has_interface_with('macaddress', 'x:x:x:x:x:x') # Returns `false` | ||
# @example Checking if an interface exists with a given IP address | ||
# stdlib::has_interface_with('ipaddress', '127.0.0.1') # Returns `true` | ||
dispatch :has_interface_with do | ||
param "Enum['macaddress','netmask','ipaddress','network','ip','mac']", :kind | ||
param 'String[1]', :value | ||
return_type 'Boolean' | ||
end | ||
|
||
def has_interface(interface) # rubocop:disable Naming/PredicateName | ||
interfaces.key? interface | ||
end | ||
|
||
def has_interface_with(kind, value) # rubocop:disable Naming/PredicateName | ||
# For compatibility with older version of function that used the legacy facts, alias `ip` with `ipaddress` and `mac` with `macaddress` | ||
kind = 'ip' if kind == 'ipaddress' | ||
kind = 'mac' if kind == 'macaddress' | ||
|
||
interface = interfaces.find { |_interface, params| params[kind] == value } | ||
!interface.nil? | ||
end | ||
|
||
def interfaces | ||
closure_scope['facts']['networking']['interfaces'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters