Skip to content

Commit 4543054

Browse files
authored
Merge pull request #1448 from alexjfisher/issue1447
Fix `has_ip_address` and `has_ip_network` functions
2 parents 52740b7 + 2a25b6d commit 4543054

10 files changed

+131
-188
lines changed

REFERENCE.md

+56-53
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ environment.
6363
* [`grep`](#grep): This function searches through an array and returns any elements that match
6464
the provided regular expression.
6565
* [`has_interface_with`](#has_interface_with): DEPRECATED. Use the namespaced function [`stdlib::has_interface_with`](#stdlibhas_interface_with) instead.
66-
* [`has_interface_with`](#has_interface_with): Returns boolean based on kind and value.
67-
* [`has_ip_address`](#has_ip_address): Returns true if the client has the requested IP address on some interface.
68-
* [`has_ip_network`](#has_ip_network): Returns true if the client has an IP address within the requested network.
66+
* [`has_ip_address`](#has_ip_address): DEPRECATED. Use the namespaced function [`stdlib::has_ip_address`](#stdlibhas_ip_address) instead.
67+
* [`has_ip_network`](#has_ip_network): DEPRECATED. Use the namespaced function [`stdlib::has_ip_network`](#stdlibhas_ip_network) instead.
6968
* [`intersection`](#intersection): This function returns an array of the intersection of two.
7069
* [`is_a`](#is_a): Boolean check to determine whether a variable is of a given data type.
7170
This is equivalent to the `=~` type checks.
@@ -126,6 +125,8 @@ optional seed for repeatable randomness.
126125
* [`stdlib::fqdn_rotate`](#stdlib--fqdn_rotate): Rotates an array or string a random number of times, combining the `fqdn` fact and an optional seed for repeatable randomness.
127126
* [`stdlib::has_function`](#stdlib--has_function): Returns whether the Puppet runtime has access to a given function.
128127
* [`stdlib::has_interface_with`](#stdlib--has_interface_with): Returns boolean based on network interfaces present and their attribute values.
128+
* [`stdlib::has_ip_address`](#stdlib--has_ip_address): Returns true if the client has the requested IPv4 address on some interface.
129+
* [`stdlib::has_ip_network`](#stdlib--has_ip_network): Returns true if the client has the requested IPv4 network on some interface.
129130
* [`stdlib::ip_in_range`](#stdlib--ip_in_range): Returns true if the ipaddress is within the given CIDRs
130131
* [`stdlib::merge`](#stdlib--merge): Merges two or more hashes together or hashes resulting from iteration, and returns
131132
the resulting hash.
@@ -2052,75 +2053,41 @@ Data type: `Any`
20522053

20532054

20542055

2055-
### <a name="has_interface_with"></a>`has_interface_with`
2056-
2057-
Type: Ruby 3.x API
2058-
2059-
Valid kinds are `macaddress`, `netmask`, `ipaddress` and `network`.
2060-
2061-
#### Examples
2062-
2063-
##### **Usage**
2064-
2065-
```puppet
2066-
has_interface_with("macaddress", "x:x:x:x:x:x") # Returns `false`
2067-
has_interface_with("ipaddress", "127.0.0.1") # Returns `true`
2068-
```
2069-
2070-
##### If no "kind" is given, then the presence of the interface is checked:
2071-
2072-
```puppet
2073-
has_interface_with("lo") # Returns `true`
2074-
```
2056+
### <a name="has_ip_address"></a>`has_ip_address`
20752057

2076-
#### `has_interface_with()`
2058+
Type: Ruby 4.x API
20772059

2078-
Valid kinds are `macaddress`, `netmask`, `ipaddress` and `network`.
2060+
DEPRECATED. Use the namespaced function [`stdlib::has_ip_address`](#stdlibhas_ip_address) instead.
20792061

2080-
Returns: `Any` boolean values `true` or `false`
2062+
#### `has_ip_address(Any *$args)`
20812063

2082-
##### Examples
2064+
The has_ip_address function.
20832065

2084-
###### **Usage**
2085-
2086-
```puppet
2087-
has_interface_with("macaddress", "x:x:x:x:x:x") # Returns `false`
2088-
has_interface_with("ipaddress", "127.0.0.1") # Returns `true`
2089-
```
2066+
Returns: `Any`
20902067

2091-
###### If no "kind" is given, then the presence of the interface is checked:
2068+
##### `*args`
20922069

2093-
```puppet
2094-
has_interface_with("lo") # Returns `true`
2095-
```
2070+
Data type: `Any`
20962071

2097-
### <a name="has_ip_address"></a>`has_ip_address`
20982072

2099-
Type: Ruby 3.x API
21002073

2101-
This function iterates through the 'interfaces' fact and checks the
2102-
'ipaddress_IFACE' facts, performing a simple string comparison.
2074+
### <a name="has_ip_network"></a>`has_ip_network`
21032075

2104-
#### `has_ip_address()`
2076+
Type: Ruby 4.x API
21052077

2106-
This function iterates through the 'interfaces' fact and checks the
2107-
'ipaddress_IFACE' facts, performing a simple string comparison.
2078+
DEPRECATED. Use the namespaced function [`stdlib::has_ip_network`](#stdlibhas_ip_network) instead.
21082079

2109-
Returns: `Boolean` `true` or `false`
2080+
#### `has_ip_network(Any *$args)`
21102081

2111-
### <a name="has_ip_network"></a>`has_ip_network`
2082+
The has_ip_network function.
21122083

2113-
Type: Ruby 3.x API
2084+
Returns: `Any`
21142085

2115-
This function iterates through the 'interfaces' fact and checks the
2116-
'network_IFACE' facts, performing a simple string comparision.
2086+
##### `*args`
21172087

2118-
#### `has_ip_network()`
2088+
Data type: `Any`
21192089

2120-
This function iterates through the 'interfaces' fact and checks the
2121-
'network_IFACE' facts, performing a simple string comparision.
21222090

2123-
Returns: `Any` Boolean value, `true` if the client has an IP address within the requested network.
21242091

21252092
### <a name="intersection"></a>`intersection`
21262093

@@ -3575,6 +3542,42 @@ Data type: `String[1]`
35753542

35763543
The value of the attribute
35773544

3545+
### <a name="stdlib--has_ip_address"></a>`stdlib::has_ip_address`
3546+
3547+
Type: Puppet Language
3548+
3549+
Returns true if the client has the requested IPv4 address on some interface.
3550+
3551+
#### `stdlib::has_ip_address(Stdlib::IP::Address::V4::Nosubnet $ip_address)`
3552+
3553+
The stdlib::has_ip_address function.
3554+
3555+
Returns: `Boolean` Returns `true` if the requested IP address exists on any interface.
3556+
3557+
##### `ip_address`
3558+
3559+
Data type: `Stdlib::IP::Address::V4::Nosubnet`
3560+
3561+
The IPv4 address you want to check the existence of
3562+
3563+
### <a name="stdlib--has_ip_network"></a>`stdlib::has_ip_network`
3564+
3565+
Type: Puppet Language
3566+
3567+
Returns true if the client has the requested IPv4 network on some interface.
3568+
3569+
#### `stdlib::has_ip_network(Stdlib::IP::Address::V4::Nosubnet $ip_network)`
3570+
3571+
The stdlib::has_ip_network function.
3572+
3573+
Returns: `Boolean` Returns `true` if the requested IP network exists on any interface.
3574+
3575+
##### `ip_network`
3576+
3577+
Data type: `Stdlib::IP::Address::V4::Nosubnet`
3578+
3579+
The IPv4 network you want to check the existence of
3580+
35783581
### <a name="stdlib--ip_in_range"></a>`stdlib::ip_in_range`
35793582

35803583
Type: Ruby 4.x API

functions/has_ip_address.pp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @summary Returns true if the client has the requested IPv4 address on some interface.
2+
#
3+
# @param ip_address
4+
# The IPv4 address you want to check the existence of
5+
# @return [Boolean] Returns `true` if the requested IP address exists on any interface.
6+
function stdlib::has_ip_address(
7+
Stdlib::IP::Address::V4::Nosubnet $ip_address,
8+
) >> Boolean {
9+
stdlib::has_interface_with('ipaddress', $ip_address)
10+
}

functions/has_ip_network.pp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @summary Returns true if the client has the requested IPv4 network on some interface.
2+
#
3+
# @param ip_network
4+
# The IPv4 network you want to check the existence of
5+
# @return [Boolean] Returns `true` if the requested IP network exists on any interface.
6+
function stdlib::has_ip_network(
7+
Stdlib::IP::Address::V4::Nosubnet $ip_network,
8+
) >> Boolean {
9+
stdlib::has_interface_with('network', $ip_network)
10+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::has_ip_address`](#stdlibhas_ip_address) instead.
6+
Puppet::Functions.create_function(:has_ip_address) do
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
9+
end
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'has_ip_address', 'This function is deprecated, please use stdlib::has_ip_address instead.', false)
12+
call_function('stdlib::has_ip_address', *args)
13+
end
14+
end
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::has_ip_network`](#stdlibhas_ip_network) instead.
6+
Puppet::Functions.create_function(:has_ip_network) do
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
9+
end
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'has_ip_network', 'This function is deprecated, please use stdlib::has_ip_network instead.', false)
12+
call_function('stdlib::has_ip_network', *args)
13+
end
14+
end

lib/puppet/parser/functions/has_interface_with.rb

-68
This file was deleted.

lib/puppet/parser/functions/has_ip_address.rb

-27
This file was deleted.

lib/puppet/parser/functions/has_ip_network.rb

-27
This file was deleted.

spec/functions/has_ip_address_spec.rb

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44

55
describe 'has_ip_address' do
66
it { is_expected.not_to be_nil }
7-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
8-
it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
7+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::has_ip_address' expects 1 argument, got none}) }
8+
it { is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{'stdlib::has_ip_address' expects 1 argument, got 2}) }
99

1010
context 'when on Linux Systems' do
1111
let(:facts) do
1212
{
13-
interfaces: 'eth0,lo',
14-
ipaddress: '10.0.0.1',
15-
ipaddress_lo: '127.0.0.1',
16-
ipaddress_eth0: '10.0.0.1'
13+
networking: {
14+
'interfaces' => {
15+
'eth0' => {
16+
'ip' => '10.0.0.1',
17+
},
18+
'lo' => {
19+
'ip' => '127.0.0.1',
20+
},
21+
},
22+
'ip' => '10.0.0.1',
23+
},
1724
}
1825
end
1926

2027
it { is_expected.to run.with_params('127.0.0.1').and_return(true) }
2128
it { is_expected.to run.with_params('10.0.0.1').and_return(true) }
2229
it { is_expected.to run.with_params('8.8.8.8').and_return(false) }
23-
it { is_expected.to run.with_params('invalid').and_return(false) }
30+
it { is_expected.to run.with_params('invalid').and_raise_error(ArgumentError, %r{parameter 'ip_address' expects a match}) }
2431
end
2532
end

0 commit comments

Comments
 (0)