Skip to content

Commit a089759

Browse files
authored
Allow adding and removing attributes in Context (#502) (#503)
* Allow adding and removing attributes in Context (#502) * Fix white space lint errors * Correct indentation * Update commons-daemon-native version to match Tomcat bundled version
1 parent 187ee08 commit a089759

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

manifests/config/context.pp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
#
33
# @param catalina_base
44
# Specifies the root of the Tomcat installation.
5+
# @param additional_attributes
6+
# Specifies any additional attributes to add to the Context. Should be a hash of the format 'attribute' => 'value'. Optional
7+
# @param attributes_to_remove
8+
# Specifies an array of attributes to remove from the element. Valid options: an array of strings. `[]`.
59
# @param show_diff
610
# Specifies display differences when augeas changes files, defaulting to true. Valid options: true or false.
711
#
812
define tomcat::config::context (
9-
Optional[Stdlib::Absolutepath] $catalina_base = undef,
10-
Boolean $show_diff = true,
13+
Optional[Stdlib::Absolutepath] $catalina_base = undef,
14+
Hash $additional_attributes = {},
15+
Array $attributes_to_remove = [],
16+
Boolean $show_diff = true,
1117
) {
1218
include tomcat
1319
$_catalina_base = pick($catalina_base, $tomcat::catalina_home)
@@ -17,9 +23,35 @@
1723
fail('Server configurations require Augeas >= 1.0.0')
1824
}
1925

26+
$base_path = 'Context'
27+
28+
if ! empty($additional_attributes) {
29+
$set_additional_attributes = suffix(prefix(join_keys_to_values($additional_attributes, " '"), "set ${base_path}/#attribute/"), "'")
30+
31+
# Extra augeas to add atttibutes if there are currently no attrributes in <Context> element
32+
augeas { "context-add_attribute_${_catalina_base}":
33+
incl => "${catalina_base}/conf/context.xml",
34+
lens => 'Xml.lns',
35+
context => "/files/${catalina_base}/conf/context.xml",
36+
changes => ['ins #attribute before Context/#text[1]'] + $set_additional_attributes,
37+
onlyif => 'match Context/#attribute size == 0',
38+
}
39+
} else {
40+
$set_additional_attributes = undef
41+
}
42+
if ! empty(any2array($attributes_to_remove)) {
43+
$rm_attributes_to_remove = prefix(any2array($attributes_to_remove), "rm ${base_path}/#attribute/")
44+
} else {
45+
$rm_attributes_to_remove = undef
46+
}
47+
2048
$_watched_resource = 'set Context/WatchedResource/#text "WEB-INF/web.xml"'
2149

22-
$changes = delete_undef_values([$_watched_resource])
50+
$changes = delete_undef_values(flatten([
51+
$_watched_resource,
52+
$set_additional_attributes,
53+
$rm_attributes_to_remove,
54+
]))
2355

2456
if ! empty($changes) {
2557
augeas { "context-${_catalina_base}":

spec/defines/config/context_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@
3232
}
3333
end
3434

35+
context 'Add Attribute' do
36+
let :params do
37+
{
38+
catalina_base: '/opt/apache-tomcat/test',
39+
additional_attributes: {
40+
'crossContext' => 'true',
41+
},
42+
attributes_to_remove: [
43+
'foobar',
44+
],
45+
}
46+
end
47+
48+
changes = [
49+
'set Context/WatchedResource/#text "WEB-INF/web.xml"',
50+
'set Context/#attribute/crossContext \'true\'',
51+
'rm Context/#attribute/foobar',
52+
]
53+
it {
54+
is_expected.to contain_augeas('context-/opt/apache-tomcat/test').with(
55+
'lens' => 'Xml.lns',
56+
'incl' => '/opt/apache-tomcat/test/conf/context.xml',
57+
'changes' => changes,
58+
)
59+
}
60+
end
3561
describe 'failing tests' do
3662
context 'old augeas' do
3763
let :facts do

0 commit comments

Comments
 (0)