Skip to content

Commit

Permalink
Merge 0ccdddd into 7db724e
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Oct 4, 2020
2 parents 7db724e + 0ccdddd commit fa2aea4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions REFERENCE.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions 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
35 changes: 35 additions & 0 deletions 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

0 comments on commit fa2aea4

Please sign in to comment.