Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |