Skip to content

Commit

Permalink
Make hosts / plugins / types always optional
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smortex committed Jul 13, 2018
1 parent 10220d7 commit a8e3f10
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
27 changes: 26 additions & 1 deletion spec/classes/collectd_plugin_threshold_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
facts
end

let :params do
let :custom_params do
{
'types' => [
{
Expand Down Expand Up @@ -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{<Type "foo">}) }
it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMin 0\.0}) }
Expand Down
6 changes: 3 additions & 3 deletions templates/plugin/threshold.conf.epp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Plugin "threshold">
<%- $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 })) -%>
<%- } -%>
</Plugin>
4 changes: 2 additions & 2 deletions templates/plugin/threshold/host.epp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Host "<%= $host['name'] %>">
<%- $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 })) -%>
<%- } -%>
</Host>
2 changes: 1 addition & 1 deletion templates/plugin/threshold/plugin.epp
Original file line number Diff line number Diff line change
Expand Up @@ -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 })) -%>
<%- } -%>
</Plugin>
4 changes: 2 additions & 2 deletions types/threshold/host.pp
Original file line number Diff line number Diff line change
@@ -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]],
}]
2 changes: 1 addition & 1 deletion types/threshold/plugin.pp
Original file line number Diff line number Diff line change
@@ -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]],
}]

0 comments on commit a8e3f10

Please sign in to comment.