From 117c9ff7820a6ca7c8d17892b1b7a7b08e2a37d1 Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 18 Jul 2023 14:44:14 +0200 Subject: [PATCH 1/5] Allow native types to handle a list of tags make it possible to concatenate fragments identified by different tags into a file; allow parameter "tag" of type "concat_file" to be a string or array of strings therefore additionally handle nonexistent tags (as being optional in both native types) correctly --- lib/puppet/type/concat_file.rb | 20 +++++++++++++++----- lib/puppet/type/concat_fragment.rb | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/puppet/type/concat_file.rb b/lib/puppet/type/concat_file.rb index ddde4f01..71dabc13 100644 --- a/lib/puppet/type/concat_file.rb +++ b/lib/puppet/type/concat_file.rb @@ -8,20 +8,30 @@ Puppet::Type.newtype(:concat_file) do @doc = <<-DOC @summary - Generates a file with content from fragments sharing a common unique tag. + Generates a file with content from fragments sharing a single or a list of common unique tag(s). @example Concat_fragment <<| tag == 'unique_tag' |>> + Concat_fragment <<| tag == 'other_tag' |>> concat_file { '/tmp/file': tag => 'unique_tag', # Optional. Default to undef path => '/tmp/file', # Optional. If given it overrides the resource name owner => 'root', # Optional. Default to undef group => 'root', # Optional. Default to undef - mode => '0644' # Optional. Default to undef - order => 'numeric' # Optional, Default to 'numeric' + mode => '0644', # Optional. Default to undef + order => 'numeric', # Optional, Default to 'numeric' ensure_newline => false # Optional, Defaults to false } + concat_file { '/tmp/file2': + tag => ['unique_tag', 'other_tag'], + path => '/tmp/file2', + owner => 'root', + group => 'root', + mode => '0644', + order => 'numeric', + ensure_newline => false + } DOC ensurable do @@ -40,7 +50,7 @@ def exists? end newparam(:tag) do - desc 'Required. Specifies a unique tag reference to collect all concat_fragments with the same tag.' + desc 'Specifies a single or a list of unique tag reference(s) to collect all concat_fragments with the same tag(s).' end newparam(:path, namevar: true) do @@ -194,7 +204,7 @@ def fragments next unless resource.is_a?(Puppet::Type.type(:concat_fragment)) if resource[:target] == self[:path] || resource[:target] == title || - (resource[:tag] && resource[:tag] == self[:tag]) + ((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || self[:tag].include?(resource[:tag]))) resource end }.compact diff --git a/lib/puppet/type/concat_fragment.rb b/lib/puppet/type/concat_fragment.rb index 1261ee6d..3fd41d0b 100644 --- a/lib/puppet/type/concat_fragment.rb +++ b/lib/puppet/type/concat_fragment.rb @@ -75,7 +75,7 @@ next unless resource.is_a?(Puppet::Type.type(:concat_file)) resource[:path] == self[:target] || resource.title == self[:target] || - (resource[:tag] && resource[:tag] == self[:tag]) + ((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || resource[:tag].include?(self[:tag]))) end if found.empty? From 909606b8ef994db00bddb7cbef32b0c358e88f32 Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 18 Jul 2023 17:41:11 +0200 Subject: [PATCH 2/5] Add parameter "tag" to concat::fragment allow a user defined tag for a fragment; keep previous default behavior if not supplied --- manifests/fragment.pp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/manifests/fragment.pp b/manifests/fragment.pp index c73ec0ce..7b0203a1 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -16,10 +16,14 @@ # @param target # Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent concat resource. # +# @param tag +# Specifies a custom tag to use for the fragment. +# define concat::fragment ( String $target, Optional[Variant[Sensitive[String], String, Deferred]] $content = undef, Optional[Variant[String, Array]] $source = undef, + Optional[String[1]] $tag = undef, Variant[String, Integer] $order = '10', ) { $resource = 'Concat::Fragment' @@ -34,7 +38,11 @@ fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.") } - $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') + if $tag =~ Undef { + $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') + } else { + $safe_target_name = $tag + } concat_fragment { $name: target => $target, From d4d8399d0bffa4923985a77ae6ac19ceafdcd2d1 Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 18 Jul 2023 17:48:25 +0200 Subject: [PATCH 3/5] Add parameter "tag" to concat allow a user defined tag or list of tags for fragments to build the concat_file out of --- manifests/init.pp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index ef03f2e4..cccbe924 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -82,6 +82,9 @@ # @param create_empty_file # Specifies whether to create an empty file if no fragments are defined. Defaults to true. # +# @param tag +# Specifies a custom tag or list of custom tags for gatherinng the fragments to combine. +# define concat ( Enum['present', 'absent'] $ensure = 'present', Stdlib::Absolutepath $path = $name, @@ -103,6 +106,7 @@ Boolean $force = false, Boolean $create_empty_file = true, Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain', + Optional[Variant[String[1], Array[String[1], 1]]] $tag = undef, ) { $safe_name = regsubst($name, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'G') $default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n" @@ -122,9 +126,15 @@ } } + if $tag =~ Undef { + $safe_names = $safe_name + } else { + $safe_names = flatten($safe_name, $tag) + } + if $ensure == 'present' { concat_file { $name: - tag => $safe_name, + tag => $safe_names, path => $path, owner => $owner, group => $group, @@ -156,7 +166,7 @@ } else { concat_file { $name: ensure => $ensure, - tag => $safe_name, + tag => $safe_names, path => $path, backup => $backup, } From 95ad14d0040ee905d098c45f97e275515b4318de Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 1 Aug 2023 10:54:48 +0200 Subject: [PATCH 4/5] Rename tagging parameter for defined types concat and concat::fragment avoid potential problems and warnings by overlapping with the metaparameter "tag" --- manifests/fragment.pp | 8 ++++---- manifests/init.pp | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/manifests/fragment.pp b/manifests/fragment.pp index 7b0203a1..aa85c2cc 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -16,14 +16,14 @@ # @param target # Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent concat resource. # -# @param tag +# @param tagging # Specifies a custom tag to use for the fragment. # define concat::fragment ( String $target, Optional[Variant[Sensitive[String], String, Deferred]] $content = undef, Optional[Variant[String, Array]] $source = undef, - Optional[String[1]] $tag = undef, + Optional[String[1]] $tagging = undef, Variant[String, Integer] $order = '10', ) { $resource = 'Concat::Fragment' @@ -38,10 +38,10 @@ fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.") } - if $tag =~ Undef { + if $tagging =~ Undef { $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') } else { - $safe_target_name = $tag + $safe_target_name = $tagging } concat_fragment { $name: diff --git a/manifests/init.pp b/manifests/init.pp index cccbe924..b1c7ae46 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -82,8 +82,8 @@ # @param create_empty_file # Specifies whether to create an empty file if no fragments are defined. Defaults to true. # -# @param tag -# Specifies a custom tag or list of custom tags for gatherinng the fragments to combine. +# @param tagging +# Specifies a custom tag or list of custom tags for gathering the fragments to combine. # define concat ( Enum['present', 'absent'] $ensure = 'present', @@ -105,8 +105,8 @@ Optional[String] $seluser = undef, Boolean $force = false, Boolean $create_empty_file = true, - Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain', - Optional[Variant[String[1], Array[String[1], 1]]] $tag = undef, + Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain', + Optional[Variant[String[1], Array[String[1], 1]]] $tagging = undef, ) { $safe_name = regsubst($name, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'G') $default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n" @@ -126,10 +126,10 @@ } } - if $tag =~ Undef { + if $tagging =~ Undef { $safe_names = $safe_name } else { - $safe_names = flatten($safe_name, $tag) + $safe_names = flatten($safe_name, $tagging) } if $ensure == 'present' { From f5dea19ae9cb47d455243c36652b7904d18e978c Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 4 Jun 2024 12:49:44 +0200 Subject: [PATCH 5/5] Fix condition syntax --- manifests/fragment.pp | 6 +++--- manifests/init.pp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/manifests/fragment.pp b/manifests/fragment.pp index aa85c2cc..e438c5a6 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -38,10 +38,10 @@ fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.") } - if $tagging =~ Undef { - $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') - } else { + if $tagging { $safe_target_name = $tagging + } else { + $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') } concat_fragment { $name: diff --git a/manifests/init.pp b/manifests/init.pp index b1c7ae46..f8d96968 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -126,10 +126,10 @@ } } - if $tagging =~ Undef { - $safe_names = $safe_name - } else { + if $tagging { $safe_names = flatten($safe_name, $tagging) + } else { + $safe_names = $safe_name } if $ensure == 'present' {