From a8e3f10340bc9921db7a10100c072d4ae8386a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Fri, 13 Jul 2018 09:36:01 +0200 Subject: [PATCH] Make hosts / plugins / types always optional The previous configuration did not allowed to create a host with only plugins or types which is quite common, just like the plugin allows to not pass hosts, plugins or types. For consistency, never require any "child" Struct. While here, extend and reorder the tests. --- .../classes/collectd_plugin_threshold_spec.rb | 27 ++++++++++++++++++- templates/plugin/threshold.conf.epp | 6 ++--- templates/plugin/threshold/host.epp | 4 +-- templates/plugin/threshold/plugin.epp | 2 +- types/threshold/host.pp | 4 +-- types/threshold/plugin.pp | 2 +- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/spec/classes/collectd_plugin_threshold_spec.rb b/spec/classes/collectd_plugin_threshold_spec.rb index 89deed2e1..395c923c8 100644 --- a/spec/classes/collectd_plugin_threshold_spec.rb +++ b/spec/classes/collectd_plugin_threshold_spec.rb @@ -7,7 +7,7 @@ facts end - let :params do + let :custom_params do { 'types' => [ { @@ -77,6 +77,31 @@ content: %r{LoadPlugin threshold} ) end + end + + context ':ensure => present and empty configurations' do + let :params do + { + 'hosts' => [ + { + 'name' => 'example.com', + } + ], + 'plugins' => [ + { + 'name' => 'interface', + } + ], + } + end + + it { is_expected.to compile } + end + + context ':ensure => present and custom parameters' do + let :params do + custom_params + end it { is_expected.to contain_file('threshold.load').with(content: %r{}) } it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMin 0\.0}) } diff --git a/templates/plugin/threshold.conf.epp b/templates/plugin/threshold.conf.epp index 844ef602c..d4f8cb97b 100644 --- a/templates/plugin/threshold.conf.epp +++ b/templates/plugin/threshold.conf.epp @@ -1,11 +1,11 @@ - <%- $collectd::plugin::threshold::hosts.each |$host| { -%> + <%- $collectd::plugin::threshold::hosts.lest || { [] }.each |$host| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/host.epp', { host => $host })) -%> <%- } -%> - <%- $collectd::plugin::threshold::plugins.each |$plugin| { -%> + <%- $collectd::plugin::threshold::plugins.lest || { [] }.each |$plugin| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $collectd::plugin::threshold::types.each |$type| { -%> + <%- $collectd::plugin::threshold::types.lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/host.epp b/templates/plugin/threshold/host.epp index 4571da01b..1ba4cd80b 100644 --- a/templates/plugin/threshold/host.epp +++ b/templates/plugin/threshold/host.epp @@ -1,8 +1,8 @@ "> - <%- $host['plugins'].each |$plugin| { -%> + <%- $host['plugins'].lest || { [] }.each |$plugin| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $host['types'].each |$type| { -%> + <%- $host['types'].lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/plugin.epp b/templates/plugin/threshold/plugin.epp index c2b8e0134..ce5f9df9d 100644 --- a/templates/plugin/threshold/plugin.epp +++ b/templates/plugin/threshold/plugin.epp @@ -2,7 +2,7 @@ <%- if $plugin['instance'] != undef { -%> Instance "<%= $plugin['instance'] %>" <%- } -%> - <%- $plugin['types'].each |$type| { -%> + <%- $plugin['types'].lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/types/threshold/host.pp b/types/threshold/host.pp index 5ed2e17c4..2902722d8 100644 --- a/types/threshold/host.pp +++ b/types/threshold/host.pp @@ -1,5 +1,5 @@ type Collectd::Threshold::Host = Struct[{ name => String[1], - plugins => Array[Collectd::Threshold::Plugin], - types => Array[Collectd::Threshold::Type], + plugins => Optional[Array[Collectd::Threshold::Plugin]], + types => Optional[Array[Collectd::Threshold::Type]], }] diff --git a/types/threshold/plugin.pp b/types/threshold/plugin.pp index 4876909ba..e31501f71 100644 --- a/types/threshold/plugin.pp +++ b/types/threshold/plugin.pp @@ -1,5 +1,5 @@ type Collectd::Threshold::Plugin = Struct[{ name => String[1], instance => Optional[String[1]], - types => Array[Collectd::Threshold::Type], + types => Optional[Array[Collectd::Threshold::Type]], }]