Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Commit

Permalink
#203 Add netplan defines
Browse files Browse the repository at this point in the history
  • Loading branch information
alvagante committed Aug 28, 2019
1 parent 257544b commit 3d7e8fa
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
48 changes: 48 additions & 0 deletions manifests/netplan.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Define network::netplan
#
# Define to manage a netplan configuration file
#
define network::netplan (
String $config_file_name = "50-${title}-yaml",
Enum['present','absent'] $ensure = 'present',
String $renderer = 'networkd',
Numeric $version = 2,

Stdlib::Absolutepath $config_dir_path = '/etc/netplan',

Hash $ethernets = {},
Hash $wifis = {},
Hash $bridges = {},
Hash $bonds = {},
Hash $tunnels = {},
Hash $vlans = {},

Optional[String] $file_content = undef,
Optional[String] $file_source = undef,

) {

$netplan_data = {
'network' => {
'version' => $version,
'renderer' => $renderer,
'ethernets' => $ethernets,
'wifis' => $wifis,
'bridges' => $bridges,
'bonds' => $bonds,
'tunnels' => $tunnels,
'vlans' => $vlans,
}
}

$real_file_content = $file_source ? {
undef => pick($file_content,to_yaml($netplan_data)),
default => undef,
}

file { "${config_dir_path}/${config_file_name}":
ensure => $ensure,
content => $real_file_content,
source => $file_source,
}
}
75 changes: 75 additions & 0 deletions manifests/netplan/interface.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Define network::netplan::interface
#
# Define to manage an interface via netplan
#
define network::netplan::interface (
Enum['present','absent'] $ensure = 'present',

String $interface_name = $title,
String $config_file_name = "50-${title}-yaml",
String $interface_type = 'ethernet',
Hash $interface_options = {},

Stdlib::Absolutepath $config_dir_path = '/etc/netplan',

String $renderer = 'networkd',
Numeric $version = 2,

Boolean $dhcp4 = false,
Boolean $dhcp6 = false,

Optional[Stdlib::MAC] $macaddress = undef,
Variant[Undef,Array] $addresses = undef,
Variant[Undef,Array] $routes = undef,
Optional[Stdlib::IP::Address::V4] $gateway4 = undef,
Optional[Stdlib::IP::Address::V6] $gateway6 = undef,
Optional[Array] $nameservers_addresses = undef,
Optional[Array] $nameservers_search = undef,

Optional[String] $file_content = undef,
Optional[String] $file_source = undef,

) {

$match_values = $macaddress ? {
undef => undef,
default => {
match => {
macaddress => $macaddress,
}
}
}

$default_values = {
dhcp4 => $dhcp4,
dhcp6 => $dhcp6,
addresses => $addresses,
gateway4 => $gateway4,
gateway6 => $gateway6,
nameservers => {
addresses => $nameservers_addresses,
search => $nameservers_search,
},
routes => $routes,
}

$netplan_data = {
'network' => {
'version' => $version,
"${interface_type}s" => {
$interface_name => delete_undef_values($default_values + $match_values + $interface_options),
}
}
}

$real_file_content = $file_source ? {
undef => pick($file_content,to_yaml($netplan_data)),
default => undef,
}
file { "${config_dir_path}/${config_file_name}":
ensure => $ensure,
content => $real_file_content,
source => $file_source,
}

}
16 changes: 16 additions & 0 deletions spec/defines/netplan/interface_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'network::netplan::interface' do
let(:title) { 'namevar' }
let(:params) do
{}
end

on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

it { is_expected.to compile }
end
end
end
16 changes: 16 additions & 0 deletions spec/defines/netplan_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'network::netplan' do
let(:title) { 'namevar' }
let(:params) do
{}
end

on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

it { is_expected.to compile }
end
end
end

0 comments on commit 3d7e8fa

Please sign in to comment.