Skip to content

Commit d27673c

Browse files
authored
Merge pull request #63 from oracle/issue#61-wls-macros-conflict-with-wdt
Issue#61 - Add new @@prop:key@@ syntax for variables, deprecate ${key…
2 parents 69ea73d + c145f1f commit d27673c

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

core/src/main/python/wlsdeploy/util/variables.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
_logger = platform_logger.PlatformLogger('wlsdeploy.variables')
2121
_variable_pattern = re.compile("\\$\\{[\w.-]+\\}")
2222
_file_variable_pattern = re.compile("@@FILE:[\w.\\\/:-]+@@")
23+
_property_pattern = re.compile("@@PROP:[\w.-]+@@")
2324

2425

2526
def load_variables(file_path):
@@ -146,14 +147,28 @@ def _substitute(text, variables):
146147
if tokens:
147148
for token in tokens:
148149
key = token[2:-1]
150+
# for ${key} variables, leave them in place if not defined.
151+
# there are cases where WebLogic allows ${key} values, such as server templates.
152+
# ${key} substitution is deprecated, so log if replacement occurs.
153+
if key in variables:
154+
value = variables[key]
155+
text = text.replace(token, value)
156+
_logger.info('WLSDPLY-01735', token, key, method_name=method_name, class_name=_class_name)
157+
158+
# skip lookups for text with no @@
159+
if '@@' in text:
160+
tokens = _property_pattern.findall(text)
161+
if tokens:
162+
for token in tokens:
163+
key = token[7:-2]
164+
# for @@PROP:key@@ variables, throw an exception if key is not found.
149165
if key not in variables:
150166
ex = exception_helper.create_variable_exception('WLSDPLY-01732', key)
151167
_logger.throwing(ex, class_name=_class_name, method_name=method_name)
152168
raise ex
153169
value = variables[key]
154170
text = text.replace(token, value)
155171

156-
if '@@FILE:' in text:
157172
tokens = _file_variable_pattern.findall(text)
158173
if tokens:
159174
for token in tokens:

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ WLSDPLY-01731=Variables updated after encryption
249249
WLSDPLY-01732=Variable {0} is not found in properties file
250250
WLSDPLY-01733=Variable file {0} cannot be read: {1}
251251
WLSDPLY-01734=No value in variable file {0}
252+
WLSDPLY-01735=Variable substitution for {0} is deprecated, use @@PROP:{1}@@
252253

253254
# wlsdeploy/util/weblogic_helper.py
254255
WLSDPLY-01740=Encryption failed: Unable to locate SerializedSystemIni

core/src/test/python/variables_test.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def testSubstituteYaml(self):
2929
variables.substitute(model, variable_map)
3030
self.assertEqual(model['topology']['Name'], 'xyz123')
3131
self.assertEqual(model['topology']['Server']['s1']['ListenPort'], '1009')
32+
self.assertEqual(model['topology']['Server']['s2']['Cluster'], 'myCluster')
3233
self.assertEqual(True, 'myCluster' in model['topology']['Cluster'])
3334

3435
def testSubstituteJson(self):
@@ -37,12 +38,27 @@ def testSubstituteJson(self):
3738
variables.substitute(model, variable_map)
3839
self.assertEqual(model['topology']['Name'], 'xyz123')
3940
self.assertEqual(model['topology']['Server']['s1']['ListenPort'], '1009')
41+
self.assertEqual(model['topology']['Server']['s2']['Cluster'], 'myCluster')
4042
self.assertEqual(True, 'myCluster' in model['topology']['Cluster'])
4143

4244
def testVariableNotFound(self):
45+
"""
46+
For ${key} substitution, no replacement is done, and no error is reported, if variable not found.
47+
${key} substitution is deprecated.
48+
"""
49+
model = FileToPython(self._resources_dir + '/variables-test.json', self._use_ordering).parse()
50+
model['topology']['Name'] = '${bad.variable}'
51+
variable_map = variables.load_variables(self._variables_file)
52+
variables.substitute(model, variable_map)
53+
self.assertEqual(model['topology']['Name'], '${bad.variable}')
54+
55+
def testPropertyNotFound(self):
56+
"""
57+
For @@PROP:key@@ substitution, an exception is thrown if variable not found.
58+
"""
4359
try:
4460
model = FileToPython(self._resources_dir + '/variables-test.json', self._use_ordering).parse()
45-
model['topology']['Name'] = '${bad.variable}'
61+
model['topology']['Name'] = '@@PROP:bad.variable@@'
4662
variable_map = variables.load_variables(self._variables_file)
4763
variables.substitute(model, variable_map)
4864
except VariableException:

core/src/test/resources/variables-test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"s2": {
2020
"ListenAddress": "127.0.0.1",
2121
"ListenPort": 8101,
22-
"Cluster": "${my.cluster}"
22+
"Cluster": "@@PROP:my.cluster@@"
2323
}
2424
}
2525
}

core/src/test/resources/variables-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ topology:
1414
s2:
1515
ListenAddress: 127.0.0.1
1616
ListenPort: 8101
17-
Cluster: '${my.cluster}'
17+
Cluster: '@@PROP:my.cluster@@'

0 commit comments

Comments
 (0)