Skip to content

Allow user defined tag or list of tags #790

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/puppet/type/concat_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/type/concat_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 9 additions & 1 deletion manifests/fragment.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 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]] $tagging = undef,
Variant[String, Integer] $order = '10',
) {
$resource = 'Concat::Fragment'
Expand All @@ -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 $tagging {
$safe_target_name = $tagging
} else {
$safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM')
}

concat_fragment { $name:
target => $target,
Expand Down
16 changes: 13 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
# @param create_empty_file
# Specifies whether to create an empty file if no fragments are defined. Defaults to true.
#
# @param tagging
# Specifies a custom tag or list of custom tags for gathering the fragments to combine.
#
define concat (
Enum['present', 'absent'] $ensure = 'present',
Stdlib::Absolutepath $path = $name,
Expand All @@ -102,7 +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',
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"
Expand All @@ -122,9 +126,15 @@
}
}

if $tagging {
$safe_names = flatten($safe_name, $tagging)
} else {
$safe_names = $safe_name
}

if $ensure == 'present' {
concat_file { $name:
tag => $safe_name,
tag => $safe_names,
path => $path,
owner => $owner,
group => $group,
Expand Down Expand Up @@ -156,7 +166,7 @@
} else {
concat_file { $name:
ensure => $ensure,
tag => $safe_name,
tag => $safe_names,
path => $path,
backup => $backup,
}
Expand Down