Skip to content

Added ensure_* functions #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/puppet/functions/ensure_directory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Takes a generic 'ensure' parameter (or its boolean equivalent)
# and converts it to an appropriate value for use with directory
# declaration.
Puppet::Functions.create_function(:ensure_directory) do
# @param ensure_param Pass ensure value here
# @return [String] 'directory' or 'absent'
#
# @example Calling the function with 'present'
# ensure_directory('present') # returns 'directory'
#
# @example Calling the function with boolean parameter
# ensure_directory(true) # returns 'directory'
#
# @example
# class myservice::config (
# Enum[present, absent] $ensure = present,
# ) {
# file { '/etc/myservice':
# ensure => ensure_directory($ensure),
# mode => '0755',
# }
# }
dispatch :ensure do
param 'Variant[String, Boolean]', :ensure_param
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the type to Enum[present, absent] you would not need to check that in an else since you are guaranteed your method gets called with only one of those two values.

end

def ensure(ensure_param)
case ensure_param
when 'present', true then
'directory'
when 'absent', false then
'absent'
else
raise(ArgumentError, "ensure_directory(): invalid argument: '#{ensure_param}'.")
end
end
end
37 changes: 37 additions & 0 deletions lib/puppet/functions/ensure_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Takes a generic 'ensure' parameter (or its boolean equivalent)
# and converts it to an appropriate value for use with file
# declaration.
Puppet::Functions.create_function(:ensure_file) do
# @param ensure_param Pass ensure value here
# @return [String] 'file' or 'absent'
#
# @example Calling the function with 'present'
# ensure_file('present') # returns 'file'
#
# @example Calling the function with boolean parameter:
# ensure_file(true) # returns 'file'
#
# @example Usage context:
# class myservice::config (
# Enum[present, absent] $ensure = present,
# ) {
# file { '/etc/myservice':
# ensure => ensure_file($ensure),
# mode => '0644',
# }
# }
dispatch :ensure do
param 'Variant[String, Boolean]', :ensure_param
end

def ensure(ensure_param)
case ensure_param
when 'present', true then
'file'
when 'absent', false then
'absent'
else
raise(ArgumentError, "ensure_file(): invalid argument: '#{ensure_param}'.")
end
end
end
41 changes: 41 additions & 0 deletions lib/puppet/functions/ensure_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Takes a generic 'ensure' parameter (or its boolean equivalent)
# and converts it to an appropriate value for use with symlink
# declaration.
Puppet::Functions.create_function(:ensure_link) do
# @param ensure_param Pass ensure value here
# @return [String] 'link' or 'absent'
#
# @example Calling the function with 'present'
# ensure_link('present') # returns 'link'
#
# @example Calling the function with boolean parameter:
# ensure_link(true) # returns 'link'
#
# @example Usage context:
# class myservice::config (
# Enum[present, absent] $ensure = present,
# ) {
# file { '/etc/myservice':
# ensure => file,
# }
#
# file { '/etc/myservice-link':
# ensure => ensure_link($ensure),
# target => '/etc/myservice'
# }
# }
dispatch :ensure do
param 'Variant[String, Boolean]', :ensure_param
end

def ensure(ensure_param)
case ensure_param
when 'present', true then
'link'
when 'absent', false then
'absent'
else
raise(ArgumentError, "ensure_link(): invalid argument: '#{ensure_param}'.")
end
end
end
20 changes: 20 additions & 0 deletions lib/puppet/functions/ensure_present.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Converts a boolean to a generic 'ensure' value.
Puppet::Functions.create_function(:ensure_present) do
# @param ensure_param Pass a boolean here
# @return [String] 'present' or 'absent'
#
# @example Calling the function
# ensure_present(true) # returns 'present'
# ensure_present(false) # returns 'absent'
dispatch :ensure do
param 'Boolean', :ensure_param
end

def ensure(ensure_param)
if ensure_param
'present'
else
'absent'
end
end
end
26 changes: 26 additions & 0 deletions spec/functions/ensure_directory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe 'ensure_directory' do
context 'return :directory or :absent with correct parameter' do
it { is_expected.to run.with_params(true).and_return('directory') }
it { is_expected.to run.with_params('present').and_return('directory') }
it { is_expected.to run.with_params(false).and_return('absent') }
it { is_expected.to run.with_params('absent').and_return('absent') }
end

context 'raise an argument error with incorrect parameter' do
[
'',
' ',
'someweirdstuff',
'true',
'false',
{},
[],
[''],
[' '],
].each do |ensure_param|
it { is_expected.to run.with_params(ensure_param).and_raise_error(ArgumentError) }
end
end
end
26 changes: 26 additions & 0 deletions spec/functions/ensure_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe 'ensure_file' do
context 'return :file or :absent with correct parameter' do
it { is_expected.to run.with_params(true).and_return('file') }
it { is_expected.to run.with_params('present').and_return('file') }
it { is_expected.to run.with_params(false).and_return('absent') }
it { is_expected.to run.with_params('absent').and_return('absent') }
end

context 'raise an argument error with incorrect parameter' do
[
'',
' ',
'someweirdstuff',
'true',
'false',
{},
[],
[''],
[' '],
].each do |ensure_param|
it { is_expected.to run.with_params(ensure_param).and_raise_error(ArgumentError) }
end
end
end
26 changes: 26 additions & 0 deletions spec/functions/ensure_link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe 'ensure_link' do
context 'return :link or :absent with correct parameter' do
it { is_expected.to run.with_params(true).and_return('link') }
it { is_expected.to run.with_params('present').and_return('link') }
it { is_expected.to run.with_params(false).and_return('absent') }
it { is_expected.to run.with_params('absent').and_return('absent') }
end

context 'raise an argument error with incorrect parameter' do
[
'',
' ',
'someweirdstuff',
'true',
'false',
{},
[],
[''],
[' '],
].each do |ensure_param|
it { is_expected.to run.with_params(ensure_param).and_raise_error(ArgumentError) }
end
end
end
26 changes: 26 additions & 0 deletions spec/functions/ensure_present_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe 'ensure_present' do
context 'return :present or :absent when passed a Boolean' do
it { is_expected.to run.with_params(true).and_return('present') }
it { is_expected.to run.with_params(false).and_return('absent') }
end

context 'raise an argument error with incorrect parameter' do
[
:present,
:absent,
'',
' ',
'someweirdstuff',
'true',
'false',
{},
[],
[''],
[' '],
].each do |ensure_param|
it { is_expected.to run.with_params(ensure_param).and_raise_error(ArgumentError) }
end
end
end