-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
1 parent
85368fa
commit ce21f86
Showing
3 changed files
with
107 additions
and
15 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,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 |
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_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 |