-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(SIMP-9427) Add simplib::params2hash() (#252)
Added: - Add a `simplib::params2hash()` function to return all of the calling scope's parameters as a Hash SIMP-9427 #comment added simplib::params2hash() to make sssd management easier
- Loading branch information
1 parent
95c9451
commit 1d38395
Showing
8 changed files
with
220 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
* Thu Apr 29 2021 Trevor Vaughan <[email protected]> - 4.8.0 | ||
- Add a `simplib::params2hash()` function to return all of the calling scope's | ||
parameters as a Hash | ||
|
||
* Mon Feb 01 2021 Kendall Moore <[email protected]> - 4.7.1 | ||
- Add net.ipv6.conf.all.disable_ipv6 to simplib_sysctl | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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,29 @@ | ||
# Returns a Hash of the parameters of the calling resource | ||
# | ||
# This is meant to get the parameters of classes and defined types. | ||
# The behavior when calling from other contexts is undefined | ||
Puppet::Functions.create_function(:'simplib::params2hash', Puppet::Functions::InternalFunction) do | ||
|
||
# @param prune | ||
# Parameters that you wish to exclude from the output | ||
# | ||
# @return [Hash] | ||
# All in-scope parameters | ||
dispatch :params2hash do | ||
scope_param() | ||
optional_param 'Array[String[1]]', :prune | ||
end | ||
|
||
def params2hash(scope, prune=[]) | ||
param_hash = scope.resource.to_hash | ||
|
||
prune << :name if (scope.resource.type == 'Class') | ||
|
||
prune.each do |to_prune| | ||
next if to_prune.nil? | ||
param_hash.delete(to_prune.to_sym) | ||
end | ||
|
||
return param_hash | ||
end | ||
end |
This file contains 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
This file contains 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,119 @@ | ||
require 'spec_helper' | ||
|
||
describe 'simplib::params2hash' do | ||
let(:pre_condition) {%{ | ||
class foo( | ||
$param1 = 'foo', | ||
$prune_me = 'bar', | ||
$array = ['one', 'two'], | ||
$hash = { 'key' => 'value' } | ||
){ | ||
$random_var = 'should be ignored' | ||
notify { 'test': message => 'this is a test'} | ||
$_params = simplib::params2hash() | ||
notify { 'class_json': message => $_params.to_json } | ||
} | ||
include 'foo' | ||
define bar ( | ||
$param1 = 'foo', | ||
$prune_me = 'bar', | ||
$array = ['one', 'two'], | ||
$hash = { 'key' => 'value' } | ||
){ | ||
notify { 'test2': message => 'this is another test'} | ||
$_params = simplib::params2hash() | ||
notify { 'define_json': message => $_params.to_json } | ||
} | ||
bar{ 'baz': } | ||
}} | ||
|
||
it { | ||
class_output = JSON.load(catalogue.resource('Notify[class_json]')[:message]) | ||
|
||
expect(class_output).to match( | ||
{ | ||
'param1' => 'foo', | ||
'prune_me' => 'bar', | ||
'array' => ['one', 'two'], | ||
'hash' => {'key' => 'value'} | ||
} | ||
) | ||
|
||
define_output = JSON.load(catalogue.resource('Notify[define_json]')[:message]) | ||
|
||
expect(define_output).to match( | ||
{ | ||
'name' => 'baz', | ||
'param1' => 'foo', | ||
'prune_me' => 'bar', | ||
'array' => ['one', 'two'], | ||
'hash' => {'key' => 'value'} | ||
} | ||
) | ||
} | ||
|
||
context 'when pruning values' do | ||
let(:pre_condition) {%{ | ||
class foo( | ||
$param1 = 'foo', | ||
$prune_me = 'bar', | ||
$array = ['one', 'two'], | ||
$hash = { 'key' => 'value' } | ||
){ | ||
notify { 'test': message => 'this is a test'} | ||
$_params = simplib::params2hash(['prune_me']) | ||
notify { 'class_json': message => $_params.to_json } | ||
} | ||
include 'foo' | ||
define bar ( | ||
$param1 = 'foo', | ||
$prune_me = 'bar', | ||
$array = ['one', 'two'], | ||
$hash = { 'key' => 'value' } | ||
){ | ||
notify { 'test2': message => 'this is another test'} | ||
$_params = simplib::params2hash(['prune_me']) | ||
notify { 'define_json': message => $_params.to_json } | ||
} | ||
bar{ 'baz': } | ||
}} | ||
|
||
it { | ||
class_output = JSON.load(catalogue.resource('Notify[class_json]')[:message]) | ||
|
||
expect(class_output).to match( | ||
{ | ||
'param1' => 'foo', | ||
'array' => ['one', 'two'], | ||
'hash' => {'key' => 'value'} | ||
} | ||
) | ||
|
||
define_output = JSON.load(catalogue.resource('Notify[define_json]')[:message]) | ||
|
||
expect(define_output).to match( | ||
{ | ||
'name' => 'baz', | ||
'param1' => 'foo', | ||
'array' => ['one', 'two'], | ||
'hash' => {'key' => 'value'} | ||
} | ||
) | ||
} | ||
end | ||
end |
Oops, something went wrong.