Skip to content

Commit 806ef1b

Browse files
authored
Merge pull request #790 from Lightning-/feature_taglist
Allow user defined tag or list of tags
2 parents a61c7c5 + bee8011 commit 806ef1b

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

lib/puppet/type/concat_file.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@
88
Puppet::Type.newtype(:concat_file) do
99
@doc = <<-DOC
1010
@summary
11-
Generates a file with content from fragments sharing a common unique tag.
11+
Generates a file with content from fragments sharing a single or a list of common unique tag(s).
1212
1313
@example
1414
Concat_fragment <<| tag == 'unique_tag' |>>
15+
Concat_fragment <<| tag == 'other_tag' |>>
1516
1617
concat_file { '/tmp/file':
1718
tag => 'unique_tag', # Optional. Default to undef
1819
path => '/tmp/file', # Optional. If given it overrides the resource name
1920
owner => 'root', # Optional. Default to undef
2021
group => 'root', # Optional. Default to undef
21-
mode => '0644' # Optional. Default to undef
22-
order => 'numeric' # Optional, Default to 'numeric'
22+
mode => '0644', # Optional. Default to undef
23+
order => 'numeric', # Optional, Default to 'numeric'
2324
ensure_newline => false # Optional, Defaults to false
2425
}
26+
concat_file { '/tmp/file2':
27+
tag => ['unique_tag', 'other_tag'],
28+
path => '/tmp/file2',
29+
owner => 'root',
30+
group => 'root',
31+
mode => '0644',
32+
order => 'numeric',
33+
ensure_newline => false
34+
}
2535
DOC
2636

2737
ensurable do
@@ -40,7 +50,7 @@ def exists?
4050
end
4151

4252
newparam(:tag) do
43-
desc 'Required. Specifies a unique tag reference to collect all concat_fragments with the same tag.'
53+
desc 'Specifies a single or a list of unique tag reference(s) to collect all concat_fragments with the same tag(s).'
4454
end
4555

4656
newparam(:path, namevar: true) do
@@ -194,7 +204,7 @@ def fragments
194204
next unless resource.is_a?(Puppet::Type.type(:concat_fragment))
195205

196206
if resource[:target] == self[:path] || resource[:target] == title ||
197-
(resource[:tag] && resource[:tag] == self[:tag])
207+
((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || self[:tag].is_a?(Array) && self[:tag].include?(resource[:tag])))
198208
resource
199209
end
200210
}.compact

lib/puppet/type/concat_fragment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
next unless resource.is_a?(Puppet::Type.type(:concat_file))
7676

7777
resource[:path] == self[:target] || resource.title == self[:target] ||
78-
(resource[:tag] && resource[:tag] == self[:tag])
78+
((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || resource[:tag].is_a?(Array) && resource[:tag].include?(self[:tag])))
7979
end
8080

8181
if found.empty?

manifests/fragment.pp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
# @param target
1717
# Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent concat resource.
1818
#
19+
# @param tagging
20+
# Specifies a custom tag to use for the fragment.
21+
#
1922
define concat::fragment (
2023
String $target,
2124
Optional[Variant[Sensitive[String], String, Deferred]] $content = undef,
2225
Optional[Variant[String, Array]] $source = undef,
26+
Optional[String[1]] $tagging = undef,
2327
Variant[String, Integer] $order = '10',
2428
) {
2529
$resource = 'Concat::Fragment'
@@ -34,7 +38,11 @@
3438
fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.")
3539
}
3640

37-
$safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM')
41+
if $tagging {
42+
$safe_target_name = $tagging
43+
} else {
44+
$safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM')
45+
}
3846

3947
concat_fragment { $name:
4048
target => $target,

manifests/init.pp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
# @param create_empty_file
8383
# Specifies whether to create an empty file if no fragments are defined. Defaults to true.
8484
#
85+
# @param tagging
86+
# Specifies a custom tag or list of custom tags for gathering the fragments to combine.
87+
#
8588
define concat (
8689
Enum['present', 'absent'] $ensure = 'present',
8790
Stdlib::Absolutepath $path = $name,
@@ -102,7 +105,8 @@
102105
Optional[String] $seluser = undef,
103106
Boolean $force = false,
104107
Boolean $create_empty_file = true,
105-
Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain',
108+
Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain',
109+
Optional[Variant[String[1], Array[String[1], 1]]] $tagging = undef,
106110
) {
107111
$safe_name = regsubst($name, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'G')
108112
$default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n"
@@ -122,9 +126,15 @@
122126
}
123127
}
124128

129+
if $tagging {
130+
$safe_names = flatten($safe_name, $tagging)
131+
} else {
132+
$safe_names = $safe_name
133+
}
134+
125135
if $ensure == 'present' {
126136
concat_file { $name:
127-
tag => $safe_name,
137+
tag => $safe_names,
128138
path => $path,
129139
owner => $owner,
130140
group => $group,
@@ -156,7 +166,7 @@
156166
} else {
157167
concat_file { $name:
158168
ensure => $ensure,
159-
tag => $safe_name,
169+
tag => $safe_names,
160170
path => $path,
161171
backup => $backup,
162172
}

0 commit comments

Comments
 (0)