From 7d63aa7e0997fd23095fa794c5432262c3805d58 Mon Sep 17 00:00:00 2001 From: Lennart Betz Date: Fri, 10 Sep 2021 08:57:09 +0200 Subject: [PATCH] fix #684 Add new feature windowseventlog --- manifests/feature/windowseventlog.pp | 50 ++++++++++++++++++++++++++++ spec/classes/windowseventlog_spec.rb | 44 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 manifests/feature/windowseventlog.pp create mode 100644 spec/classes/windowseventlog_spec.rb diff --git a/manifests/feature/windowseventlog.pp b/manifests/feature/windowseventlog.pp new file mode 100644 index 00000000..77acf22f --- /dev/null +++ b/manifests/feature/windowseventlog.pp @@ -0,0 +1,50 @@ +# @summary +# Configures the Icinga 2 feature windowseventlog. +# +# @param [Enum['absent', 'present']] ensure +# Set to present enables the feature windowseventlog, absent disables it. +# +# @param [Icinga2::LogSeverity] severity +# You can choose the log severity between information, notice, warning or debug. +# +class icinga2::feature::windowseventlog( + Enum['absent', 'present'] $ensure = present, + Icinga2::LogSeverity $severity = 'warning', +) { + + if ! defined(Class['::icinga2']) { + fail('You must include the icinga2 base class before using any icinga2 feature class!') + } + + if $facts['os']['family'] != 'windows' and $ensure != 'absent' { + fail('The feature windowseventlogs is only supported on Windows platforms!') + } + + $conf_dir = $::icinga2::globals::conf_dir + $_notify = $ensure ? { + 'present' => Class['::icinga2::service'], + default => undef, + } + + # compose attributes + $attrs = { + severity => $severity, + } + + # create object + icinga2::object { 'icinga2::object::WindowsEventLogLogger::windowseventlog': + object_name => 'windowseventlog', + object_type => 'WindowsEventLogLogger', + attrs => delete_undef_values($attrs), + attrs_list => keys($attrs), + target => "${conf_dir}/features-available/windowseventlog.conf", + order => 10, + notify => $_notify, + } + + # manage feature + icinga2::feature { 'windowseventlog': + ensure => $ensure, + } +} + diff --git a/spec/classes/windowseventlog_spec.rb b/spec/classes/windowseventlog_spec.rb new file mode 100644 index 00000000..e873be81 --- /dev/null +++ b/spec/classes/windowseventlog_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe('icinga2::feature::windowseventlog', type: :class) do + let(:pre_condition) do + [ + "class { 'icinga2': features => [] }", + ] + end + + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + + case facts[:kernel] + when 'windows' + let(:icinga2_conf_dir) { 'C:/ProgramData/icinga2/etc/icinga2' } + + context 'with defaults' do + it { is_expected.to contain_icinga2__feature('windowseventlog').with({ 'ensure' => 'present' }) } + + it { + is_expected.to contain_icinga2__object('icinga2::object::WindowsEventLogLogger::windowseventlog').with( + { 'target' => "#{icinga2_conf_dir}/features-available/windowseventlog.conf" }, + ).that_notifies('Class[icinga2::service]') + } + end + else + it { is_expected.to raise_error(Puppet::Error, %r{only supported on Windows platforms}) } + end + + context 'with ensure => absent' do + let(:params) do + { + ensure: 'absent', + } + end + + it { is_expected.to contain_icinga2__feature('windowseventlog').with({ 'ensure' => 'absent' }) } + end + end + end +end