Skip to content

Commit

Permalink
Merge pull request #169 from fe80/feature/ensure_remove
Browse files Browse the repository at this point in the history
make sure the ensure absent works with package install
  • Loading branch information
bastelfreak authored May 7, 2021
2 parents 6d0449c + f5d9c80 commit d9da773
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 16 deletions.
19 changes: 16 additions & 3 deletions manifests/aggregator.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
#
# [*options*]
# List. Plugin options for use in the aggregator template.

#
# [*plugin_type*]
# String. Define the telegraf plugin type to use (default is $name)
#
# [*ensure*]
# Set if the ensure params of the config file. If telegraf::ensure is absent the value is automatically absent
#
define telegraf::aggregator (
String $plugin_type = $name,
Optional[Array] $options = undef,
String $plugin_type = $name,
Optional[Array] $options = undef,
Enum['present', 'absent'] $ensure = 'present',
) {
include telegraf

$_ensure = $telegraf::ensure ? {
'absent' => 'absent',
default => $ensure,
}

file { "${telegraf::config_folder}/${name}.conf":
ensure => $_ensure,
content => inline_template("<%= require 'toml-rb'; TomlRB.dump({'aggregators'=>{'${plugin_type}'=>@options}}) %>"),
require => Class['telegraf::config'],
notify => Class['telegraf::service'],
Expand Down
10 changes: 8 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
assert_private()

file { $telegraf::config_file:
ensure => file,
ensure => $telegraf::ensure_file,
content => template('telegraf/telegraf.conf.erb'),
owner => $telegraf::config_file_owner,
group => $telegraf::config_file_group,
mode => $telegraf::config_file_mode,
}

$_dir = $telegraf::ensure ? {
'absent' => { ensure => 'absent', force => true },
default => { ensure => 'directory' }
}

file { $telegraf::config_folder:
ensure => directory,
owner => $telegraf::config_file_owner,
group => $telegraf::config_file_group,
mode => $telegraf::config_folder_mode,
purge => $telegraf::purge_config_fragments,
recurse => true,
* => $_dir,
}
}
10 changes: 10 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@
$service_hasstatus = $telegraf::params::service_hasstatus
$service_restart = $telegraf::params::service_restart

$ensure_file = $ensure ? {
'absent' => 'absent',
default => 'file',
}

$ensure_status = $ensure ? {
'absent' => 'absent',
default => 'present',
}

contain telegraf::install
contain telegraf::config
contain telegraf::service
Expand Down
19 changes: 16 additions & 3 deletions manifests/input.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
#
# [*options*]
# List. Plugin options for use in the input template.

#
# [*plugin_type*]
# String. Define the telegraf plugin type to use (default is $name)
#
# [*ensure*]
# Set if the ensure params of the config file. If telegraf::ensure is absent the value is automatically absent
#
define telegraf::input (
String $plugin_type = $name,
Array $options = [],
String $plugin_type = $name,
Array $options = [],
Enum['present', 'absent'] $ensure = 'present',
) {
include telegraf

$_ensure = $telegraf::ensure ? {
'absent' => 'absent',
default => $ensure,
}

file { "${telegraf::config_folder}/${name}.conf":
ensure => $_ensure,
content => inline_template("<%= require 'toml-rb'; TomlRB.dump({'inputs'=>{'${plugin_type}'=>@options}}) %>"),
require => Class['telegraf::config'],
notify => Class['telegraf::service'],
Expand Down
9 changes: 8 additions & 1 deletion manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
$release = $facts['os']['lsb']['codename']
}
apt::source { 'influxdata':
ensure => $telegraf::ensure_status,
comment => 'Mirror for InfluxData packages',
location => "${telegraf::repo_location}${distro}",
release => $release,
Expand All @@ -110,6 +111,7 @@
$_baseurl = "https://repos.influxdata.com/rhel/\$releasever/\$basearch/${telegraf::repo_type}"
}
yumrepo { 'influxdata':
ensure => $telegraf::ensure_status,
name => 'influxdata',
descr => "InfluxData Repository - ${facts['os']['name']} \$releasever",
enabled => 1,
Expand Down Expand Up @@ -178,7 +180,12 @@
}
} else {
if ! $telegraf::manage_archive {
ensure_packages([$telegraf::package_name], { ensure => $telegraf::ensure, install_options => $telegraf::install_options })
ensure_packages([$telegraf::package_name],
{
ensure => $telegraf::ensure,
install_options => $telegraf::install_options,
}
)
}
}
}
19 changes: 16 additions & 3 deletions manifests/output.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
#
# [*options*]
# List. Plugin options for use in the output template.

#
# [*plugin_type*]
# String. Define the telegraf plugin type to use (default is $name)
#
# [*ensure*]
# Set if the ensure params of the config file. If telegraf::ensure is absent the value is automatically absent
#
define telegraf::output (
String $plugin_type = $name,
Optional[Array] $options = undef,
String $plugin_type = $name,
Optional[Array] $options = undef,
Enum['present', 'absent'] $ensure = 'present',
) {
include telegraf

$_ensure = $telegraf::ensure ? {
'absent' => 'absent',
default => $ensure,
}

file { "${telegraf::config_folder}/${name}.conf":
ensure => $_ensure,
content => inline_template("<%= require 'toml-rb'; TomlRB.dump({'outputs'=>{'${plugin_type}'=>@options}}) %>"),
require => Class['telegraf::config'],
notify => Class['telegraf::service'],
Expand Down
19 changes: 16 additions & 3 deletions manifests/processor.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
#
# [*options*]
# List. Plugin options for use in the processor template.

#
# [*plugin_type*]
# String. Define the telegraf plugin type to use (default is $name)
#
# [*ensure*]
# Set if the ensure params of the config file. If telegraf::ensure is absent the value is automatically absent
#
define telegraf::processor (
String $plugin_type = $name,
Optional[Array] $options = undef,
String $plugin_type = $name,
Optional[Array] $options = undef,
Enum['present', 'absent'] $ensure = 'present',
) {
include telegraf

$_ensure = $telegraf::ensure ? {
'absent' => 'absent',
default => $ensure,
}

file { "${telegraf::config_folder}/${name}.conf":
ensure => $_ensure,
content => inline_template("<%= require 'toml-rb'; TomlRB.dump({'processors'=>{'${plugin_type}'=>@options}}) %>"),
require => Class['telegraf::config'],
notify => Class['telegraf::service'],
Expand Down
2 changes: 1 addition & 1 deletion manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class telegraf::service {
assert_private()

if $telegraf::manage_service {
if $telegraf::manage_service and $telegraf::ensure != 'absent' {
service { 'telegraf':
ensure => $telegraf::service_ensure,
hasstatus => $telegraf::service_hasstatus,
Expand Down
44 changes: 44 additions & 0 deletions spec/classes/telegraf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
facts
end

context 'default include'
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('telegraf::config') }
it { is_expected.to contain_class('telegraf::install') }
Expand Down Expand Up @@ -250,6 +251,49 @@
end
end
end

describe 'with ensure absent' do
let(:pre_condition) do
[
'class {"telegraf": ensure => absent}',
]
end

it do
case facts[:osfamily]
when 'Debian'
is_expected.to contain_package('telegraf').with(ensure: 'absent')
is_expected.to contain_apt__source('influxdata').with(
ensure: 'absent',
)
when 'RedHat'
is_expected.to contain_package('telegraf').with(ensure: 'absent')
is_expected.to contain_yumrepo('influxdata').with(ensure: 'absent')

when 'windows'
is_expected.to contain_package('telegraf').with(ensure: 'absent')
end
_dir = case facts[:osfamily]
when 'Darwin'
'/usr/local/etc/telegraf'
when 'windows'
'C:/Program Files/telegraf'
else
'/etc/telegraf'
end

is_expected.to contain_file(_dir + '/telegraf.conf').with(
ensure: 'absent',
)

is_expected.to contain_file(_dir + '/telegraf.d').with(
ensure: 'absent',
force: true,
)

is_expected.not_to contain_service('telegraf')
end
end
end
end
end
53 changes: 53 additions & 0 deletions spec/defines/aggregator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,59 @@
end
end
end

context 'with ensure absent' do
let(:title) {'my_basicstats'}
let(:params) do
{
ensure: 'absent',
}
end

it do
_dir = case facts[:osfamily]
when 'Darwin'
'/usr/local/etc/telegraf/telegraf.d'
when 'windows'
'C:/Program Files/telegraf/telegraf.d'
else
'/etc/telegraf/telegraf.d'
end

is_expected.to contain_file(_dir + '/my_basicstats.conf').with(
ensure: 'absent',
)
end
end

context 'with class ensure absent' do
let(:pre_condition) do
[
'class {"telegraf": ensure => absent}',
]
end
let(:title) {'my_basicstats'}
let(:params) do
{
ensure: 'present',
}
end

it do
_dir = case facts[:osfamily]
when 'Darwin'
'/usr/local/etc/telegraf/telegraf.d'
when 'windows'
'C:/Program Files/telegraf/telegraf.d'
else
'/etc/telegraf/telegraf.d'
end

is_expected.to contain_file(_dir + '/my_basicstats.conf').with(
ensure: 'absent',
)
end
end
end
end
end
54 changes: 54 additions & 0 deletions spec/defines/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,60 @@
end
end
end

context 'with ensure absent' do
let(:title) {'my_basicstats'}
let(:params) do
{
ensure: 'absent',
}
end

it do
_dir = case facts[:osfamily]
when 'Darwin'
'/usr/local/etc/telegraf/telegraf.d'
when 'windows'
'C:/Program Files/telegraf/telegraf.d'
else
'/etc/telegraf/telegraf.d'
end

is_expected.to contain_file(_dir + '/my_basicstats.conf').with(
ensure: 'absent',
)
end
end

context 'with class ensure absent' do
let(:pre_condition) do
[
'class {"telegraf": ensure => absent}',
]
end
let(:title) {'my_basicstats'}
let(:params) do
{
ensure: 'present',
}
end


it do
_dir = case facts[:osfamily]
when 'Darwin'
'/usr/local/etc/telegraf/telegraf.d'
when 'windows'
'C:/Program Files/telegraf/telegraf.d'
else
'/etc/telegraf/telegraf.d'
end

is_expected.to contain_file(_dir + '/my_basicstats.conf').with(
ensure: 'absent',
)
end
end
end
end
end
Loading

0 comments on commit d9da773

Please sign in to comment.