-
Notifications
You must be signed in to change notification settings - Fork 579
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.