Skip to content

Commit 2659b06

Browse files
authored
Merge pull request #491 from tuxmea/context_resources
Allow usage of Context/Resources
2 parents 6dea717 + 78a2204 commit 2659b06

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# @summary Configure Resources elements in $CATALINA_BASE/conf/context.xml
2+
#
3+
# @param ensure
4+
# Specifies whether you are trying to add or remove the Resources element.
5+
# @param resources_name
6+
# The name of the Resources to be created, relative to the `java:comp/env` context. `$name`.
7+
# @param name
8+
# `$resources_name`
9+
# @param resources_type
10+
# The fully qualified Java class name expected by the web application when it performs a lookup for this resources. Required to create the resource.
11+
# @param catalina_base
12+
# Specifies the root of the Tomcat installation.
13+
# @param additional_attributes
14+
# Specifies any additional attributes to add to the Valve. Should be a hash of the format 'attribute' => 'value'. Optional
15+
# @param attributes_to_remove
16+
# Specifies an array of attributes to remove from the element. Valid options: an array of strings. `[]`.
17+
# @param show_diff
18+
# Specifies display differences when augeas changes files, defaulting to true. Valid options: true or false.
19+
#
20+
define tomcat::config::context::resources (
21+
Enum['present','absent'] $ensure = 'present',
22+
$resources_name = $name,
23+
$resources_type = undef,
24+
$catalina_base = $::tomcat::catalina_home,
25+
Hash $additional_attributes = {},
26+
Array $attributes_to_remove = [],
27+
Boolean $show_diff = true,
28+
) {
29+
if versioncmp($::augeasversion, '1.0.0') < 0 {
30+
fail('Server configurations require Augeas >= 1.0.0')
31+
}
32+
33+
if $resources_name {
34+
$_resources_name = $resources_name
35+
} else {
36+
$_resources_name = $name
37+
}
38+
39+
$base_path = "Context/Resources[#attribute/name='${_resources_name}']"
40+
41+
if $ensure == 'absent' {
42+
$changes = "rm ${base_path}"
43+
} else {
44+
# (MODULES-3353) does this need to be quoted?
45+
$set_name = "set ${base_path}/#attribute/name ${_resources_name}"
46+
if $resources_type {
47+
$set_type = "set ${base_path}/#attribute/type ${resources_type}"
48+
} else {
49+
$set_type = undef
50+
}
51+
52+
if ! empty($additional_attributes) {
53+
$set_additional_attributes = suffix(prefix(join_keys_to_values($additional_attributes, " '"), "set ${base_path}/#attribute/"), "'")
54+
} else {
55+
$set_additional_attributes = undef
56+
}
57+
if ! empty(any2array($attributes_to_remove)) {
58+
$rm_attributes_to_remove = prefix(any2array($attributes_to_remove), "rm ${base_path}/#attribute/")
59+
} else {
60+
$rm_attributes_to_remove = undef
61+
}
62+
63+
$changes = delete_undef_values(flatten([
64+
$set_name,
65+
$set_type,
66+
$set_additional_attributes,
67+
$rm_attributes_to_remove,
68+
]))
69+
}
70+
71+
augeas { "context-${catalina_base}-resources-${name}":
72+
lens => 'Xml.lns',
73+
incl => "${catalina_base}/conf/context.xml",
74+
changes => $changes,
75+
show_diff => $show_diff,
76+
}
77+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'tomcat::config::context::resources', type: :define do
6+
let :pre_condition do
7+
'class {"tomcat": }'
8+
end
9+
let :facts do
10+
{
11+
osfamily: 'Debian',
12+
augeasversion: '1.0.0',
13+
}
14+
end
15+
let :title do
16+
'jdbc'
17+
end
18+
19+
context 'Add Resources' do
20+
let :params do
21+
{
22+
catalina_base: '/opt/apache-tomcat/test',
23+
resources_type: 'net.sourceforge.jtds.jdbcx.JtdsDataSource',
24+
additional_attributes: {
25+
'cachingAllowed' => 'true',
26+
'cacheMaxSize' => '100000',
27+
},
28+
attributes_to_remove: [
29+
'foobar',
30+
],
31+
}
32+
end
33+
34+
changes = [
35+
'set Context/Resources[#attribute/name=\'jdbc\']/#attribute/name jdbc',
36+
'set Context/Resources[#attribute/name=\'jdbc\']/#attribute/type net.sourceforge.jtds.jdbcx.JtdsDataSource',
37+
'set Context/Resources[#attribute/name=\'jdbc\']/#attribute/cachingAllowed \'true\'',
38+
'set Context/Resources[#attribute/name=\'jdbc\']/#attribute/cacheMaxSize \'100000\'',
39+
'rm Context/Resources[#attribute/name=\'jdbc\']/#attribute/foobar',
40+
]
41+
it {
42+
is_expected.to contain_augeas('context-/opt/apache-tomcat/test-resources-jdbc').with(
43+
'lens' => 'Xml.lns',
44+
'incl' => '/opt/apache-tomcat/test/conf/context.xml',
45+
'changes' => changes,
46+
)
47+
}
48+
end
49+
context 'Remove Resources' do
50+
let :params do
51+
{
52+
catalina_base: '/opt/apache-tomcat/test',
53+
ensure: 'absent',
54+
}
55+
end
56+
57+
it {
58+
is_expected.to contain_augeas('context-/opt/apache-tomcat/test-resources-jdbc').with(
59+
'lens' => 'Xml.lns',
60+
'incl' => '/opt/apache-tomcat/test/conf/context.xml',
61+
'changes' => ['rm Context/Resources[#attribute/name=\'jdbc\']'],
62+
)
63+
}
64+
end
65+
end

0 commit comments

Comments
 (0)