From fd5ae1f7a84f1cb6e9b038f164c5dc81e80488c0 Mon Sep 17 00:00:00 2001 From: Eugene Piven Date: Mon, 25 Dec 2017 08:57:13 +0300 Subject: [PATCH] Added ensure_* functions --- lib/puppet/functions/ensure_directory.rb | 37 +++++++++++++++++++++ lib/puppet/functions/ensure_file.rb | 37 +++++++++++++++++++++ lib/puppet/functions/ensure_link.rb | 41 ++++++++++++++++++++++++ lib/puppet/functions/ensure_present.rb | 20 ++++++++++++ spec/functions/ensure_directory_spec.rb | 26 +++++++++++++++ spec/functions/ensure_file_spec.rb | 26 +++++++++++++++ spec/functions/ensure_link_spec.rb | 26 +++++++++++++++ spec/functions/ensure_present_spec.rb | 26 +++++++++++++++ 8 files changed, 239 insertions(+) create mode 100644 lib/puppet/functions/ensure_directory.rb create mode 100644 lib/puppet/functions/ensure_file.rb create mode 100644 lib/puppet/functions/ensure_link.rb create mode 100644 lib/puppet/functions/ensure_present.rb create mode 100644 spec/functions/ensure_directory_spec.rb create mode 100644 spec/functions/ensure_file_spec.rb create mode 100644 spec/functions/ensure_link_spec.rb create mode 100644 spec/functions/ensure_present_spec.rb diff --git a/lib/puppet/functions/ensure_directory.rb b/lib/puppet/functions/ensure_directory.rb new file mode 100644 index 000000000..f6c311196 --- /dev/null +++ b/lib/puppet/functions/ensure_directory.rb @@ -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 + 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 diff --git a/lib/puppet/functions/ensure_file.rb b/lib/puppet/functions/ensure_file.rb new file mode 100644 index 000000000..c6b85adc7 --- /dev/null +++ b/lib/puppet/functions/ensure_file.rb @@ -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 diff --git a/lib/puppet/functions/ensure_link.rb b/lib/puppet/functions/ensure_link.rb new file mode 100644 index 000000000..512c1d460 --- /dev/null +++ b/lib/puppet/functions/ensure_link.rb @@ -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 diff --git a/lib/puppet/functions/ensure_present.rb b/lib/puppet/functions/ensure_present.rb new file mode 100644 index 000000000..7bfab01de --- /dev/null +++ b/lib/puppet/functions/ensure_present.rb @@ -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 diff --git a/spec/functions/ensure_directory_spec.rb b/spec/functions/ensure_directory_spec.rb new file mode 100644 index 000000000..03c0aa0d5 --- /dev/null +++ b/spec/functions/ensure_directory_spec.rb @@ -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 diff --git a/spec/functions/ensure_file_spec.rb b/spec/functions/ensure_file_spec.rb new file mode 100644 index 000000000..734b2421a --- /dev/null +++ b/spec/functions/ensure_file_spec.rb @@ -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 diff --git a/spec/functions/ensure_link_spec.rb b/spec/functions/ensure_link_spec.rb new file mode 100644 index 000000000..4c5038f33 --- /dev/null +++ b/spec/functions/ensure_link_spec.rb @@ -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 diff --git a/spec/functions/ensure_present_spec.rb b/spec/functions/ensure_present_spec.rb new file mode 100644 index 000000000..0363886fe --- /dev/null +++ b/spec/functions/ensure_present_spec.rb @@ -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