Skip to content

Commit

Permalink
Add cidr_to_range function
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Oct 4, 2020
1 parent 85368fa commit ce21f86
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
66 changes: 51 additions & 15 deletions REFERENCE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Reference

<!-- DO NOT EDIT: This document was generated by Puppet Strings -->

## Table of Contents

**Functions**
### 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_range`](#extlibcidr_to_range): Converts an CIDR address of the form 192.168.0.1/24 into a range of IP address excluding the network and broadcast addresses.
* [`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 All @@ -22,7 +24,7 @@

## Functions

### extlib::cache_data
### `extlib::cache_data`

Type: Ruby 4.x API

Expand Down Expand Up @@ -92,7 +94,41 @@ Data type: `Any`

The data for when there is no cache yet

### extlib::default_content
### `extlib::cidr_to_range`

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_range('127.0.0.1/8')
```

#### `extlib::cidr_to_range(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[Array[Stdlib::IP::Address::V4::Nosubnet],Array[Stdlib::IP::Address::V6::Nosubnet]]` IPv6 or IPv4 ip range without net/broadcast

##### Examples

###### calling the function

```puppet
extlib::cidr_to_range('127.0.0.1/8')
```

##### `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 Expand Up @@ -142,7 +178,7 @@ Data type: `Optional[String]`

The path to an .erb or .epp template file or `undef`.

### extlib::dir_split
### `extlib::dir_split`

Type: Puppet Language

Expand Down Expand Up @@ -178,7 +214,7 @@ Data type: `Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]`

- either an absolute path or a array of absolute paths.

### extlib::dump_args
### `extlib::dump_args`

Type: Ruby 4.x API

Expand All @@ -204,7 +240,7 @@ Data type: `Any`

The data you want to dump as pretty JSON.

### extlib::echo
### `extlib::echo`

Type: Ruby 4.x API

Expand Down Expand Up @@ -286,7 +322,7 @@ Data type: `Optional[String]`

An optional comment to prepend to the debug output.

### extlib::file_separator
### `extlib::file_separator`

Type: Puppet Language

Expand Down Expand Up @@ -314,7 +350,7 @@ Returns: `String` - The os specific path separator.
extlib::file_separator() => '/'
```

### extlib::has_module
### `extlib::has_module`

Type: Ruby 4.x API

Expand Down Expand Up @@ -349,7 +385,7 @@ Data type: `Pattern[/\A\w+[-\/]\w+\z/]`
The full name of the module you want to know exists or not.
Namespace and modulename can be separated with either `-` or `/`.

### extlib::ip_to_cron
### `extlib::ip_to_cron`

Type: Ruby 4.x API

Expand Down Expand Up @@ -385,7 +421,7 @@ Data type: `Optional[Integer[1]]`

The number of seconds to use as the run interval

### extlib::mkdir_p
### `extlib::mkdir_p`

Type: Puppet Language

Expand Down Expand Up @@ -427,7 +463,7 @@ Data type: `Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]`

- the path(s) to create

### extlib::path_join
### `extlib::path_join`

Type: Puppet Language

Expand Down Expand Up @@ -477,7 +513,7 @@ Data type: `Array[String]`

Joins two or more directories by file separator.

### extlib::random_password
### `extlib::random_password`

Type: Ruby 4.x API

Expand Down Expand Up @@ -511,7 +547,7 @@ Data type: `Integer[1]`

The length of random password you want generated.

### extlib::read_url
### `extlib::read_url`

Type: Ruby 4.x API

Expand Down Expand Up @@ -557,7 +593,7 @@ Data type: `Stdlib::HTTPUrl`

The URL to read from

### extlib::resources_deep_merge
### `extlib::resources_deep_merge`

Type: Ruby 4.x API

Expand Down Expand Up @@ -709,7 +745,7 @@ Data type: `Hash`

The hash of defaults to merge.

### extlib::sort_by_version
### `extlib::sort_by_version`

Type: Ruby 4.x API

Expand Down
21 changes: 21 additions & 0 deletions lib/puppet/functions/extlib/cidr_to_range.rb
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
35 changes: 35 additions & 0 deletions spec/functions/extlib/cidr_to_range.rb
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

0 comments on commit ce21f86

Please sign in to comment.