From 0ccddddaa4cde112b7ef8ff5dc1f11b24a3dc52c Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sun, 4 Oct 2020 13:04:23 +0200 Subject: [PATCH] Add cidr_to_broadcast function --- REFERENCE.md | 37 +++++++++++++++++++ .../functions/extlib/cidr_to_broadcast.rb | 18 +++++++++ spec/functions/extlib/cidr_to_broadcast.rb | 35 ++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 lib/puppet/functions/extlib/cidr_to_broadcast.rb create mode 100644 spec/functions/extlib/cidr_to_broadcast.rb diff --git a/REFERENCE.md b/REFERENCE.md index 57c667a..a750ee9 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_broadcast`](#extlibcidr_to_broadcast): Converts an IPv4 CIDR address of the form 192.168.0.1/24 into its broadcast address * [`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,42 @@ Data type: `Any` The data for when there is no cache yet +### `extlib::cidr_to_broadcast` + +Type: Ruby 4.x API + +Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned + +* **Note** IPv6 supports only multicast and unicast, so this function only supports IPv4 + +#### Examples + +##### calling the function + +```puppet +extlib::cidr_to_broadcast('127.0.0.1/8') +``` + +#### `extlib::cidr_to_broadcast(Stdlib::IP::Address::V4::CIDR $ip)` + +Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned + +Returns: `Stdlib::IP::Address::V4::Nosubnet` IPv4 broadcast address + +##### Examples + +###### calling the function + +```puppet +extlib::cidr_to_broadcast('127.0.0.1/8') +``` + +##### `ip` + +Data type: `Stdlib::IP::Address::V4::CIDR` + +IPv4 address in CIDR notation + ### `extlib::default_content` Type: Ruby 4.x API diff --git a/lib/puppet/functions/extlib/cidr_to_broadcast.rb b/lib/puppet/functions/extlib/cidr_to_broadcast.rb new file mode 100644 index 0000000..df73c7e --- /dev/null +++ b/lib/puppet/functions/extlib/cidr_to_broadcast.rb @@ -0,0 +1,18 @@ +# Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned +# +# @summary Converts an IPv4 CIDR address of the form 192.168.0.1/24 into its broadcast address +# @note IPv6 supports only multicast and unicast, so this function only supports IPv4 +Puppet::Functions.create_function(:'extlib::cidr_to_broadcast') do + # @param ip IPv4 address in CIDR notation + # @return IPv4 broadcast address + # @example calling the function + # extlib::cidr_to_broadcast('127.0.0.1/8') + dispatch :cidr_to_broadcast do + param 'Stdlib::IP::Address::V4::CIDR', :ip + return_type 'Stdlib::IP::Address::V4::Nosubnet' + end + + def cidr_to_broadcast(ip) + IPAddr.new(ip).to_range.last.to_s + end +end diff --git a/spec/functions/extlib/cidr_to_broadcast.rb b/spec/functions/extlib/cidr_to_broadcast.rb new file mode 100644 index 0000000..32290e3 --- /dev/null +++ b/spec/functions/extlib/cidr_to_broadcast.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'extlib::cidr_to_broadcast' 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.1/8').and_return('127.255.255.255') } + end + + context 'when called with an IPv6 CIDR' do + it { is_expected.to run.with_params('fe80::800:27ff:fe00:0/64').and_raise_error(ArgumentError) } + end +end