diff --git a/REFERENCE.md b/REFERENCE.md index 57c667a..a46e92b 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -7,6 +7,7 @@ ### Functions * [`extlib::cache_data`](#extlibcache_data): Retrieves data from a cache file, or creates it with supplied data if the file doesn't exist +* [`extlib::cidr_to_range`](#extlibcidr_to_range): Converts an CIDR address of the form 192.168.0.1/24 into a range of IP address excluding the network and broadcast addresses. * [`extlib::default_content`](#extlibdefault_content): Takes an optional content and an optional template name and returns the contents of a file. * [`extlib::dir_split`](#extlibdir_split): Splits the given directory or directories into individual paths. * [`extlib::dump_args`](#extlibdump_args): Prints the args to STDOUT in Pretty JSON format. @@ -93,6 +94,40 @@ Data type: `Any` The data for when there is no cache yet +### `extlib::cidr_to_range` + +Type: Ruby 4.x API + +Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned + +#### Examples + +##### calling the function + +```puppet +extlib::cidr_to_range('127.0.0.1/8') +``` + +#### `extlib::cidr_to_range(Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR] $ip)` + +Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned + +Returns: `Variant[Array[Stdlib::IP::Address::V4::Nosubnet],Array[Stdlib::IP::Address::V6::Nosubnet]]` IPv6 or IPv4 ip range without net/broadcast + +##### Examples + +###### calling the function + +```puppet +extlib::cidr_to_range('127.0.0.1/8') +``` + +##### `ip` + +Data type: `Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]` + +IPv6 or IPv4 address in CIDR notation + ### `extlib::default_content` Type: Ruby 4.x API diff --git a/lib/puppet/functions/extlib/cidr_to_range.rb b/lib/puppet/functions/extlib/cidr_to_range.rb new file mode 100644 index 0000000..b89a74e --- /dev/null +++ b/lib/puppet/functions/extlib/cidr_to_range.rb @@ -0,0 +1,21 @@ +# Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned +# +# @summary Converts an CIDR address of the form 192.168.0.1/24 into a range of IP address excluding the network and broadcast addresses. +# +Puppet::Functions.create_function(:'extlib::cidr_to_range') do + # @param ip IPv6 or IPv4 address in CIDR notation + # @return IPv6 or IPv4 ip range without net/broadcast + # @example calling the function + # extlib::cidr_to_range('127.0.0.1/8') + dispatch :cidr_to_range do + param 'Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]', :ip + return_type 'Variant[Array[Stdlib::IP::Address::V4::Nosubnet],Array[Stdlib::IP::Address::V6::Nosubnet]]' + end + + def cidr_to_range(ip) + ips = IPAddr.new(ip).to_range.map(&:to_s) + ips.shift + ips.pop + ips + end +end diff --git a/spec/functions/extlib/cidr_to_range.rb b/spec/functions/extlib/cidr_to_range.rb new file mode 100644 index 0000000..aa3f6f0 --- /dev/null +++ b/spec/functions/extlib/cidr_to_range.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'extlib::cidr_to_range' do + it 'exists' do + is_expected.not_to be_nil + end + + context 'when called with no parameters' do + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + end + + context 'when called with a Integer' do + it { is_expected.to run.with_params(42).and_raise_error(ArgumentError) } + end + + context 'when called with a String thats not an ip address' do + it { is_expected.to run.with_params('42').and_raise_error(ArgumentError) } + end + + context 'when called with an IP Address that is not in the CIDR notation' do + it { is_expected.to run.with_params('127.0.0.1').and_raise_error(ArgumentError) } + end + + context 'when called with an IP Address that is not in the CIDR notation' do + it { is_expected.to run.with_params('fe80::800:27ff:fe00:0').and_raise_error(ArgumentError) } + end + + context 'when called with an IPv4 CIDR' do + it { is_expected.to run.with_params('127.0.0.0/30').and_return(['127.0.0.1', '127.0.0.2']) } + end + + context 'when called with an IPv6 CIDR' do + it { is_expected.to run.with_params('fe80::5054:ff:fe47:4a37/126').and_return(['fe80::5054:ff:fe47:4a35', 'fe80::5054:ff:fe47:4a36']) } + end +end