Skip to content

Commit b6a19d0

Browse files
authored
Correct variable tokens at discover time (#720)
* Issue #708 - Remove illegal characters from variable name during discovery * Issue #708 - Corrected test * Issue #708 - Use cleaned resource name to find a user match
1 parent a205a5b commit b6a19d0

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

core/src/main/python/wlsdeploy/tool/util/variable_injector_functions.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
_fake_name_replacement = re.compile('.' + _fake_name_marker)
1818
_white_space_replacement = re.compile('\s')
1919

20+
# bad characters for a property name - anything that isn't a good character
21+
_bad_chars_replacement = re.compile('[^\\w.-]')
22+
2023

2124
def managed_server_list(model):
2225
"""
@@ -76,11 +79,20 @@ def format_variable_name(location, attribute, aliases):
7679
if node is not None and len(node) > 0:
7780
short_name += node + '.'
7881
short_name += attribute
82+
return clean_property_name(short_name)
83+
7984

80-
short_name = short_name.replace('/', '.')
81-
short_name = _white_space_replacement.sub('-', short_name)
82-
short_name = _fake_name_replacement.sub('', short_name)
83-
return short_name
85+
def clean_property_name(name):
86+
"""
87+
Remove or replace invalid characters in the variable name for use as a property name.
88+
:param name: the name to be cleaned
89+
:return: the revised name
90+
"""
91+
name = name.replace('/', '.')
92+
name = _white_space_replacement.sub('-', name)
93+
name = _bad_chars_replacement.sub('-', name)
94+
name = _fake_name_replacement.sub('', name)
95+
return name
8496

8597

8698
def __traverse_location(iterate_location, attribute, name_list, aliases, last_folder=None, last_folder_short=None):

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ def find_user_name(property_name, model_dictionary):
230230
name = matches[0]
231231
resources = dictionary_utils.get_dictionary_element(model_dictionary, RESOURCES)
232232
system_resources = dictionary_utils.get_dictionary_element(resources, JDBC_SYSTEM_RESOURCE)
233-
datasource = dictionary_utils.get_dictionary_element(system_resources, name)
233+
234+
# the property name has already been "cleaned", so clean resource name to find a match
235+
datasource = {}
236+
for resource_name in system_resources:
237+
resource_property_name = variable_injector_functions.clean_property_name(resource_name)
238+
if name == resource_property_name:
239+
datasource = dictionary_utils.get_dictionary_element(system_resources, resource_name)
240+
234241
jdbc_resources = dictionary_utils.get_dictionary_element(datasource, JDBC_RESOURCE)
235242
driver_params = dictionary_utils.get_dictionary_element(jdbc_resources, JDBC_DRIVER_PARAMS)
236243
properties = dictionary_utils.get_dictionary_element(driver_params, PROPERTIES)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Copyright (c) 2020, Oracle Corporation and/or its affiliates. All rights reserved.
3+
The Universal Permissive License (UPL), Version 1.0
4+
"""
5+
6+
import unittest
7+
8+
from wlsdeploy.aliases.aliases import Aliases
9+
from wlsdeploy.aliases.location_context import LocationContext
10+
from wlsdeploy.aliases.model_constants import MACHINE
11+
from wlsdeploy.tool.util import variable_injector_functions
12+
from wlsdeploy.util import variables
13+
from wlsdeploy.util.model_context import ModelContext
14+
15+
16+
class VariableInjectorFunctionsTests(unittest.TestCase):
17+
"""
18+
Test the variable injector functions methods.
19+
"""
20+
wls_version = '12.2.1.3'
21+
attribute_name = 'Notes'
22+
23+
def setUp(self):
24+
model_context = ModelContext("test", {})
25+
self.aliases = Aliases(model_context)
26+
self.location = LocationContext().append_location(MACHINE)
27+
self.name_token = self.aliases.get_name_token(self.location)
28+
self.short_name = self.aliases.get_folder_short_name(self.location)
29+
30+
def testFormatVariableName(self):
31+
"""
32+
Verify that names from location are converted to valid variable names.
33+
"""
34+
# spaces in model name
35+
self._check_name('My Machine', 'My-Machine')
36+
37+
# parenthesis can be in model name
38+
self._check_name('ohMy-(datasource)', 'ohMy--datasource-')
39+
40+
# versioned library names may be model names
41+
self._check_name('Lib.oracle.wsm.idmrest.sharedlib#1.0@12.2.1.3.Target',
42+
'Lib.oracle.wsm.idmrest.sharedlib-1.0-12.2.1.3.Target')
43+
44+
def _check_name(self, name, expected_key):
45+
"""
46+
Verify that the specified name is converted to match the expected key.
47+
A machine location is created with the supplied name and converted to a variable name.
48+
An expected value is constructed using the expected key and known parameters.
49+
:param name: the name to be converted
50+
:param expected_key: the expected variable key
51+
"""
52+
self.location.add_name_token(self.name_token, name)
53+
variable_name = variable_injector_functions.format_variable_name(self.location, self.attribute_name,
54+
self.aliases)
55+
56+
# verify that a property built with this value will parse correctly
57+
property_text = '@@PROP:%s@@' % variable_name
58+
matches = variables._property_pattern.findall(property_text)
59+
self.assertEqual(1, len(matches), "Property %s should parse correctly" % property_text)
60+
61+
expected_name = "%s.%s.%s" % (self.short_name, expected_key, self.attribute_name)
62+
self.assertEqual(expected_name, variable_name)
63+
64+
65+
if __name__ == '__main__':
66+
unittest.main()

0 commit comments

Comments
 (0)