Skip to content

Commit

Permalink
add conf param to nm class
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoblitt committed Jul 7, 2023
1 parent 3b504af commit 02f5d9f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
5 changes: 4 additions & 1 deletion data/common.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
--- {}
---
nm::conf:
main: {}
logging: {}
8 changes: 8 additions & 0 deletions examples/nm_conf.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class { 'nm':
conf => {
'main' => {
'dns' => 'none',
'no-auto-default' => '*',
},
},
}
45 changes: 23 additions & 22 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# @summary
# Manage NetworkManager
#
# @param conf
# If a String:
# Verbatim content of `NetworkManager.conf`.
#
# If a Hash:
# Hash of data to serialize to `NetworkManager.conf`.
#
# See: https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html
#
# @param connections
# Hash of nm::connection resources to create
#
class nm (
Optional[Variant[String[1], Hash[String, Hash]]] $conf = undef,
Optional[Hash[String, Hash]] $connections = undef,
) {
$conf_dir= '/etc/NetworkManager/conf.d'
$conn_dir = '/etc/NetworkManager/system-connections'
$conf_dir= '/etc/NetworkManager'
$conf_d_dir= "${conf_dir}/conf.d"
$conn_dir = "${conf_dir}/system-connections"

require nm::install
require nm::service
Expand All @@ -25,36 +36,26 @@
}

# remove any conflicting nm drop-in config files
# XXX NetworkManager.conf needs to be handled
file { $conf_dir:
file { $conf_d_dir:
ensure => 'directory',
purge => true,
recurse => true,
force => true,
}

file { "${conf_dir}/ignore-unknown-interfaces.conf":
ensure => 'file',
mode => '0644',
# lint:ignore:strict_indent
content => @("CONF"),
[main]
# do not create connections for unmanaged interfaces
no-auto-default=*
| CONF
# lint:endignore
# write NetworkManager.conf
$ini_config = { 'quote_char' => undef }

$_real_conf= $conf? {
String => $conf,
Hash => extlib::to_ini($conf, $ini_config),
default => undef,
}

file { "${conf_dir}/resolv_conf.conf":
file { "${conf_dir}/NetworkManager.conf":
ensure => 'file',
mode => '0644',
# lint:ignore:strict_indent
content => @("CONF"),
[main]
# do not write to /etc/resolv.conf
dns=none
| CONF
# lint:endignore
content => $_real_conf,
}

# remove unmanaged .nmconnection files
Expand Down
38 changes: 38 additions & 0 deletions spec/acceptance/nm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,47 @@
it { is_expected.to be_installed }
end

describe file('/etc/NetworkManager/NetworkManager.conf') do
it { is_expected.to be_file }
it { is_expected.to be_owned_by 'root' }
it { is_expected.to be_grouped_into 'root' }
it { is_expected.to be_mode '644' } # serverspec does not like a leading 0

its(:content) do
is_expected.to match <<~CONTENT
# THIS FILE IS CONTROLLED BY PUPPET
[main]
[logging]
CONTENT
end
end

describe service('NetworkManager') do
it { is_expected.to be_enabled }
it { is_expected.to be_running }
end
end

context 'with conf parameter' do
include_examples 'the example', 'nm_conf.pp'

describe file('/etc/NetworkManager/NetworkManager.conf') do
it { is_expected.to be_file }
it { is_expected.to be_owned_by 'root' }
it { is_expected.to be_grouped_into 'root' }
it { is_expected.to be_mode '644' } # serverspec does not like a leading 0

its(:content) do
is_expected.to match <<~CONTENT
# THIS FILE IS CONTROLLED BY PUPPET
[main]
dns=none
no-auto-default=*
CONTENT
end
end
end
end
7 changes: 7 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
)
end

it do
is_expected.to contain_file('/etc/NetworkManager/NetworkManager.conf').with(
ensure: 'file',
mode: '0644',

Check failure on line 36 in spec/classes/init_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call. (https://rubystyle.guide#no-trailing-params-comma)
)
end

it do
is_expected.to contain_file('/etc/NetworkManager/system-connections').with(
ensure: 'directory',
Expand Down

0 comments on commit 02f5d9f

Please sign in to comment.