Skip to content

Commit

Permalink
Add ip_to_reverse function
Browse files Browse the repository at this point in the history
This function calculate the reverse for an IPv4 or IPv6 address

Fixes: #228
  • Loading branch information
bigon committed Jun 26, 2024
1 parent b238ae4 commit 1d69b57
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [`extlib::file_separator`](#extlib--file_separator): Returns the os specific file path separator.
* [`extlib::has_module`](#extlib--has_module): A function that lets you know whether a specific module is on your modulepath.
* [`extlib::ip_to_cron`](#extlib--ip_to_cron): Provides a "random" value to cron based on the last bit of the machine IP address. used to avoid starting a certain cron job at the same time
* [`extlib::ip_to_reverse`](#extlib--ip_to_reverse): Returns the reverse of an IP address
* [`extlib::is_in_cidr`](#extlib--is_in_cidr): Returns a boolean indicating whether an IP address is part of a network CIDR
* [`extlib::last_in_cidr`](#extlib--last_in_cidr): Converts an IPv4 or IPv6 CIDR address of the form 192.0.2.1/24 or 2001:db8::1/64 into the last address in the network
* [`extlib::mkdir_p`](#extlib--mkdir_p): Like the unix command mkdir_p except with puppet code.
Expand Down Expand Up @@ -636,6 +637,42 @@ Data type: `Optional[Integer[1]]`

The number of seconds to use as the run interval

### <a name="extlib--ip_to_reverse"></a>`extlib::ip_to_reverse`

Type: Ruby 4.x API

Not actual query is done to the DNS server, it only calculate the record
that could be added to a DNS zone file.

#### Examples

##### Calling the function

```puppet
extlib::ip_to_reverse('192.0.2.0')
```

#### `extlib::ip_to_reverse(Stdlib::IP::Address::Nosubnet $ip)`

Not actual query is done to the DNS server, it only calculate the record
that could be added to a DNS zone file.

Returns: `String`

##### Examples

###### Calling the function

```puppet
extlib::ip_to_reverse('192.0.2.0')
```

##### `ip`

Data type: `Stdlib::IP::Address::Nosubnet`

IPv4 or IPv6 address

### <a name="extlib--is_in_cidr"></a>`extlib::is_in_cidr`

Type: Ruby 4.x API
Expand Down
23 changes: 23 additions & 0 deletions lib/puppet/functions/extlib/ip_to_reverse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'ipaddr'

# @summary Returns the reverse of an IP address
#
# Not actual query is done to the DNS server, it only calculate the record
# that could be added to a DNS zone file.
Puppet::Functions.create_function(:'extlib::ip_to_reverse') do
# @param ip
# IPv4 or IPv6 address
#
# @example Calling the function
# extlib::ip_to_reverse('192.0.2.0')
dispatch :ip_to_reverse? do
param 'Stdlib::IP::Address::Nosubnet', :ip
return_type 'String'
end

def ip_to_reverse?(ip)
IPAddr(ip).reverse
end
end

0 comments on commit 1d69b57

Please sign in to comment.