Skip to content

Commit

Permalink
(SIMP-9427) Add simplib::params2hash() (#252)
Browse files Browse the repository at this point in the history
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
trevor-vaughan authored May 17, 2021
1 parent 95c9451 commit 1d38395
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 39 deletions.
14 changes: 8 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require:
- rubocop-i18n
AllCops:
DisplayCopNames: true
TargetRubyVersion: '2.4'
TargetRubyVersion: '2.6'
Include:
- "**/*.rb"
Exclude:
Expand All @@ -29,9 +29,9 @@ Lint/RaiseException:
Enabled: true
Lint/StructNewOverride:
Enabled: false
GetText:
I18n/GetText:
Enabled: false
GetText/DecorateString:
I18n/GetText/DecorateString:
Description: We don't want to decorate test output.
Exclude:
- spec/**/*
Expand Down Expand Up @@ -124,11 +124,11 @@ Style/MethodCalledOnDoEndBlock:
Enabled: true
Style/StringMethods:
Enabled: true
GetText/DecorateFunctionMessage:
I18n/GetText/DecorateFunctionMessage:
Enabled: false
GetText/DecorateStringFormattingUsingInterpolation:
I18n/GetText/DecorateStringFormattingUsingInterpolation:
Enabled: false
GetText/DecorateStringFormattingUsingPercent:
I18n/GetText/DecorateStringFormattingUsingPercent:
Enabled: false
Layout/EndOfLine:
Enabled: false
Expand Down Expand Up @@ -158,6 +158,8 @@ RSpec/MessageExpectation:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
RSpec/MultipleMemoizedHelpers:
Enabled: false
RSpec/NestedGroups:
Enabled: false
Style/AsciiComments:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
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

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.project
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
group :test do
gem 'rubocop'
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'rubocop-i18n'
gem 'super_diff'
end

group :system_tests do
Expand Down
25 changes: 25 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* [`simplib::module_metadata::os_supported`](#simplibmodule_metadataos_supported): Returns whether or not the passed module is supported per the module's metadata.json.
* [`simplib::nets2cidr`](#simplibnets2cidr): Take an input list of networks and returns an equivalent `Array` in CIDR notation. * Hostnames are passed through untouched. * Terminates ca
* [`simplib::nets2ddq`](#simplibnets2ddq): Tranforms a list of networks into an equivalent array in dotted quad notation. * IPv4 CIDR networks are converted to dotted quad notation ne
* [`simplib::params2hash`](#simplibparams2hash): 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
* [`simplib::parse_hosts`](#simplibparse_hosts): Convert an `Array` of items that may contain port numbers or protocols into a structured `Hash` of host information. * Works with Hostnames
* [`simplib::passgen`](#simplibpassgen): Generates/retrieves a random password string or its hash for a passed identifier. * Supports 2 modes: * simpkv * Password info is stor
* [`simplib::passgen::gen_password_and_salt`](#simplibpassgengen_password_and_salt): Generates a password and salt * Password length, complexity and complex-only settings are specified by the caller. * Salt length, complexi
Expand Down Expand Up @@ -2474,6 +2475,30 @@ Data type: `String`
String containing the list of networks to
convert; list elements are separated by spaces, commas or semicolons.

### <a name="simplibparams2hash"></a>`simplib::params2hash`

Type: Ruby 4.x API

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

#### `simplib::params2hash(Optional[Array[String[1]]] $prune)`

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

Returns: `Hash` All in-scope parameters

##### `prune`

Data type: `Optional[Array[String[1]]]`

Parameters that you wish to exclude from the output

### <a name="simplibparse_hosts"></a>`simplib::parse_hosts`

Type: Ruby 4.x API
Expand Down
29 changes: 29 additions & 0 deletions lib/puppet/functions/simplib/params2hash.rb
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
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simp-simplib",
"version": "4.7.1",
"version": "4.8.0",
"author": "SIMP Team",
"summary": "A collection of common SIMP functions, facts, and types",
"license": "Apache-2.0",
Expand Down
119 changes: 119 additions & 0 deletions spec/functions/simplib/params2hash_spec.rb
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
Loading

0 comments on commit 1d38395

Please sign in to comment.