Skip to content

Commit

Permalink
Merge 0389ded into 51f699c
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Oct 5, 2020
2 parents 51f699c + 0389ded commit 4199045
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
35 changes: 35 additions & 0 deletions REFERENCE.md
Expand Up @@ -8,6 +8,7 @@

* [`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_netmask`](#extlibcidr_to_netmask): Converts an CIDR address of the form 192.168.0.1/24 into its netmask.
* [`extlib::cidr_to_network`](#extlibcidr_to_network): Converts a CIDR address of the form 2001:DB8::/32 or 192.0.2.0/24 into their network address (also known as net 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 @@ -128,6 +129,40 @@ Data type: `Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]

IPv6 or IPv4 address in CIDR notation

### `extlib::cidr_to_network`

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_network('2001:DB8::/32')
```

#### `extlib::cidr_to_network(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[Stdlib::IP::Address::V4::Nosubnet,Stdlib::IP::Address::V6::Nosubnet]` IPv6 or IPv4 network/net address

##### Examples

###### calling the function

```puppet
extlib::cidr_to_network('2001:DB8::/32')
```

##### `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
Expand Down
18 changes: 18 additions & 0 deletions lib/puppet/functions/extlib/cidr_to_network.rb
@@ -0,0 +1,18 @@
# Imported by Tim 'bastelfreak' Meusel into voxpupuli/extlib because Yelp/netstdlib got abandoned
#
# @summary Converts a CIDR address of the form 2001:DB8::/32 or 192.0.2.0/24 into their network address (also known as net address)
#
Puppet::Functions.create_function(:'extlib::cidr_to_network') do
# @param ip IPv6 or IPv4 address in CIDR notation
# @return IPv6 or IPv4 network/net address
# @example calling the function
# extlib::cidr_to_network('2001:DB8::/32')
dispatch :cidr_to_network do
param 'Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]', :ip
return_type 'Variant[Stdlib::IP::Address::V4::Nosubnet,Stdlib::IP::Address::V6::Nosubnet]'
end

def cidr_to_network(ip)
IPAddr.new(ip).to_range.first.to_s
end
end
35 changes: 35 additions & 0 deletions spec/functions/extlib/cidr_to_network.rb
@@ -0,0 +1,35 @@
require 'spec_helper'

describe 'extlib::cidr_to_network' 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.0.0.0') }
end

context 'when called with an IPv6 CIDR' do
it { is_expected.to run.with_params('fe80::800:27ff:fe00:0/64').and_return('fe80::') }
end
end

0 comments on commit 4199045

Please sign in to comment.