Skip to content

Commit 520f5bf

Browse files
Remove templar use and replace with regex (#169)
Remove templar use and replace with regex Signed-off-by: Stewart Francis <[email protected]>
1 parent 40c93eb commit 520f5bf

File tree

7 files changed

+47
-134
lines changed

7 files changed

+47
-134
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Remove usage of templar, and replace with simple pattern matching (https://github.com/ansible-collections/ibm_zos_cics/pull/169).

plugins/action/csd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def run(self, tmp=None, task_vars=None):
1919
task_vars=task_vars,
2020
)
2121

22-
def _process_module_args(self, module_args, _templar, ds_name, task_vars, cics_data_sets_required):
23-
super(ActionModule, self)._process_module_args(module_args, _templar, ds_name, task_vars, cics_data_sets_required)
22+
def _process_module_args(self, module_args, ds_name, cics_data_sets_required):
23+
super(ActionModule, self)._process_module_args(module_args, ds_name, cics_data_sets_required)
2424
if module_args.get(INPUT_LOCATION):
2525
module_args[INPUT_LOCATION] = module_args[INPUT_LOCATION].lower()
2626
if module_args[INPUT_LOCATION] == LOCAL and module_args.get(INPUT_SOURCE):

plugins/action/region_jcl.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def run(self, tmp=None, task_vars=None):
3737
}
3838

3939
try:
40-
_process_module_args(self.module_args, self._templar, task_vars)
40+
_process_module_args(self.module_args)
4141
except (KeyError, ValueError) as e:
4242
return_structure.update({
4343
"failed": True,
@@ -55,26 +55,26 @@ def run(self, tmp=None, task_vars=None):
5555
return return_structure
5656

5757

58-
def _process_module_args(module_args, _templar, task_vars):
58+
def _process_module_args(module_args):
5959
for library_key in LIBRARY_KEYS:
6060
_set_top_libraries_key(module_args, library_key)
6161
_validate_list_of_data_set_lengths(module_args[library_key]["top_data_sets"])
6262
_validate_list_of_data_set_lengths(module_args[library_key].get("data_sets", []))
6363

6464
for cics_lib in CICS_DS_KEYS:
65-
_process_libraries_args(module_args, _templar, task_vars, "cics_data_sets", cics_lib)
65+
_process_libraries_args(module_args, "cics_data_sets", cics_lib)
6666

6767
for region_ds in REGION_DS_KEYS:
68-
_process_region_data_set_args(module_args, _templar, region_ds, task_vars)
68+
_process_region_data_set_args(module_args, region_ds)
6969
# Template field in region_data_sets needs to be removed before module execution
7070
if module_args["region_data_sets"].get("template"):
7171
del module_args["region_data_sets"]["template"]
7272

7373
for le_lib in LE_DS_KEYS:
74-
_process_libraries_args(module_args, _templar, task_vars, "le_data_sets", le_lib)
74+
_process_libraries_args(module_args, "le_data_sets", le_lib)
7575

7676
# Optional argument
7777
if module_args.get("cpsm_data_sets"):
7878
for cpsm_lib in CPSM_DS_KEYS:
79-
_process_libraries_args(module_args, _templar, task_vars, "cpsm_data_sets", cpsm_lib)
79+
_process_libraries_args(module_args, "cpsm_data_sets", cpsm_lib)
8080
_process_data_set_unit_args(module_args)

plugins/plugin_utils/_module_action_plugin.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
__metaclass__ = type
99

1010
from ansible.plugins.action import ActionBase
11+
import re
1112

1213
REGION_DS_KEYS = ["dfhgcd", "dfhlcd", "dfhintra", "dfhlrq", "dfhtemp", "dfhauxt", "dfhbuxt", "dfhdmpa", "dfhdmpb", "dfhcsd", "dfhstart"]
1314
CICS_DS_KEYS = ["sdfhload", "sdfhauth", "sdfhlic"]
@@ -33,9 +34,7 @@ def _run(self, ds_name, module_name, cics_data_sets_required, tmp=None, task_var
3334
try:
3435
self._process_module_args(
3536
self.module_args,
36-
self._templar,
3737
ds_name,
38-
task_vars,
3938
cics_data_sets_required,
4039
)
4140
except (KeyError, ValueError) as e:
@@ -56,17 +55,17 @@ def _run(self, ds_name, module_name, cics_data_sets_required, tmp=None, task_var
5655

5756
return return_structure
5857

59-
def _process_module_args(self, module_args, _templar, ds_name, task_vars, cics_data_sets_required):
60-
_process_module_args(module_args, _templar, ds_name, task_vars, cics_data_sets_required)
58+
def _process_module_args(self, module_args, ds_name, cics_data_sets_required):
59+
_process_module_args(module_args, ds_name, cics_data_sets_required)
6160

6261

63-
def _process_module_args(module_args, _templar, ds_name, task_vars, cics_data_sets_required):
64-
_process_region_data_set_args(module_args, _templar, ds_name, task_vars)
62+
def _process_module_args(module_args, ds_name, cics_data_sets_required):
63+
_process_region_data_set_args(module_args, ds_name)
6564
_process_data_set_unit_args(module_args)
6665
_remove_region_data_set_args(module_args, ds_name)
6766

6867
if cics_data_sets_required:
69-
_process_libraries_args(module_args, _templar, task_vars, "cics_data_sets", "sdfhload")
68+
_process_libraries_args(module_args, "cics_data_sets", "sdfhload")
7069
_remove_cics_data_set_args(module_args, "sdfhload")
7170
else:
7271
if module_args.get("cics_data_sets"):
@@ -102,7 +101,7 @@ def _remove_cics_data_set_args(module_args, ds_name):
102101
del module_args["cics_data_sets"][cics_key]
103102

104103

105-
def _process_region_data_set_args(module_args, _templar, ds_name, task_vars):
104+
def _process_region_data_set_args(module_args, ds_name):
106105
if not module_args.get("region_data_sets"):
107106
raise KeyError("Required argument region_data_sets not found")
108107

@@ -111,8 +110,6 @@ def _process_region_data_set_args(module_args, _templar, ds_name, task_vars):
111110
module_args["region_data_sets"].update({
112111
ds_name: {
113112
"dsn": _template_dsn(
114-
_templar=_templar,
115-
task_vars=task_vars,
116113
var_name="data_set_name",
117114
replace_val=ds_name.upper(),
118115
template=module_args["region_data_sets"]["template"],
@@ -140,12 +137,10 @@ def _validate_data_set_length(data_set):
140137
raise ValueError("Data set: {0} is longer than 44 characters.".format(data_set))
141138

142139

143-
def _process_libraries_args(module_args, _templar, task_vars, lib_type, lib_ds_name):
140+
def _process_libraries_args(module_args, lib_type, lib_ds_name):
144141
if not _check_library_override(module_args, lib_type, lib_ds_name):
145142
if _check_template(module_args, lib_type):
146143
module_args[lib_type][lib_ds_name] = _template_dsn(
147-
_templar=_templar,
148-
task_vars=task_vars,
149144
var_name="lib_name",
150145
replace_val=lib_ds_name.upper(),
151146
template=module_args[lib_type]["template"],
@@ -155,14 +150,8 @@ def _process_libraries_args(module_args, _templar, task_vars, lib_type, lib_ds_n
155150
return _validate_data_set_length(module_args[lib_type][lib_ds_name])
156151

157152

158-
def _template_dsn(_templar, task_vars, var_name, replace_val, template):
159-
cpy = task_vars.copy()
160-
cpy.update({var_name: replace_val})
161-
return _templar.copy_with_new_env(
162-
variable_start_string="<<",
163-
variable_end_string=">>",
164-
available_variables=cpy,
165-
).do_template(template, overrides=dict(variable_start_string="<<", variable_end_string=">>"))
153+
def _template_dsn(var_name, replace_val, template):
154+
return re.sub(f"<<\\s*{var_name}\\s*>>", replace_val, template)
166155

167156

168157
def _check_template(module_args, arg_dict):

tests/unit/action/test_data_set.py

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
from __future__ import absolute_import, division, print_function
77
__metaclass__ = type
88

9-
from ansible.parsing.dataloader import DataLoader
10-
from ansible.template import Templar
119
from ansible_collections.ibm.ibm_zos_cics.plugins.plugin_utils._module_action_plugin import (
1210
_process_module_args
1311
)
1412

1513

16-
def get_templar(module_args):
17-
loader = DataLoader()
18-
templar = Templar(loader=loader, variables=module_args)
19-
return templar
20-
21-
2214
def test_data_set_with_template():
2315
args_with_template = {
2416
"region_data_sets": {"template": "data.set.template.<< data_set_name >>"},
@@ -27,10 +19,8 @@ def test_data_set_with_template():
2719
"space_type": "m",
2820
"state": "initial"
2921
}
30-
templar = get_templar(args_with_template)
31-
task_vars = args_with_template
3222

33-
_process_module_args(args_with_template, templar, "dfhlrq", task_vars, False)
23+
_process_module_args(args_with_template, "dfhlrq", False)
3424

3525
assert args_with_template == {
3626
"region_data_sets": {
@@ -51,10 +41,8 @@ def test_data_set_with_override():
5141
"space_type": "m",
5242
"state": "initial"
5343
}
54-
templar = get_templar(args_with_override)
55-
task_vars = args_with_override
5644

57-
_process_module_args(args_with_override, templar, "dfhlrq", task_vars, False)
45+
_process_module_args(args_with_override, "dfhlrq", False)
5846

5947
assert args_with_override == {
6048
"region_data_sets": {
@@ -73,11 +61,9 @@ def test_data_set_with_override_but_no_dsn_key():
7361
"space_type": "m",
7462
"state": "initial"
7563
}
76-
templar = get_templar(args_with_override)
77-
task_vars = args_with_override
7864

7965
try:
80-
_process_module_args(args_with_override, templar, "dfhlrq", task_vars, False)
66+
_process_module_args(args_with_override, "dfhlrq", False)
8167
except KeyError as e:
8268
assert e.args[0] == "No template or data set override found for dfhlrq"
8369
else:
@@ -91,11 +77,9 @@ def test_data_set_with_override_but_no_dsn_value():
9177
"space_type": "m",
9278
"state": "initial"
9379
}
94-
templar = get_templar(args_with_override)
95-
task_vars = args_with_override
9680

9781
try:
98-
_process_module_args(args_with_override, templar, "dfhlrq", task_vars, False)
82+
_process_module_args(args_with_override, "dfhlrq", False)
9983
except KeyError as e:
10084
assert e.args[0] == "No template or data set override found for dfhlrq"
10185
else:
@@ -109,11 +93,9 @@ def test_data_set_without_override_or_template():
10993
"space_type": "m",
11094
"state": "initial"
11195
}
112-
templar = get_templar(args_with_garbage)
113-
task_vars = args_with_garbage
11496

11597
try:
116-
_process_module_args(args_with_garbage, templar, "dfhlrq", task_vars, False)
98+
_process_module_args(args_with_garbage, "dfhlrq", False)
11799
except KeyError as e:
118100
assert e.args[0] == "No template or data set override found for dfhlrq"
119101
else:
@@ -128,10 +110,8 @@ def test_data_set_with_unnecessary_cics_data_sets_arg():
128110
"space_type": "m",
129111
"state": "initial"
130112
}
131-
templar = get_templar(args_with_template)
132-
task_vars = args_with_template
133113

134-
_process_module_args(args_with_template, templar, "dfhlrq", task_vars, False)
114+
_process_module_args(args_with_template, "dfhlrq", False)
135115

136116
assert args_with_template == {
137117
"region_data_sets": {
@@ -152,10 +132,8 @@ def test_data_set_with_le_data_sets_arg():
152132
"space_type": "m",
153133
"state": "initial"
154134
}
155-
templar = get_templar(args_with_template)
156-
task_vars = args_with_template
157135

158-
_process_module_args(args_with_template, templar, "dfhlrq", task_vars, False)
136+
_process_module_args(args_with_template, "dfhlrq", False)
159137

160138
assert args_with_template == {
161139
"region_data_sets": {
@@ -176,10 +154,8 @@ def test_data_set_with_cpsm_data_sets_arg():
176154
"space_type": "m",
177155
"state": "initial"
178156
}
179-
templar = get_templar(args_with_template)
180-
task_vars = args_with_template
181157

182-
_process_module_args(args_with_template, templar, "dfhlrq", task_vars, False)
158+
_process_module_args(args_with_template, "dfhlrq", False)
183159

184160
assert args_with_template == {
185161
"region_data_sets": {
@@ -200,10 +176,8 @@ def test_data_set_with_required_cics_data_sets_templated():
200176
"space_type": "m",
201177
"state": "initial"
202178
}
203-
templar = get_templar(args_with_template)
204-
task_vars = args_with_template
205179

206-
_process_module_args(args_with_template, templar, "dfhgcd", task_vars, True)
180+
_process_module_args(args_with_template, "dfhgcd", True)
207181

208182
assert args_with_template == {
209183
"region_data_sets": {

0 commit comments

Comments
 (0)