Skip to content

Commit 60d43e7

Browse files
committed
refactor(tofs): avoid using “salt['config.get']” for formula writers
We can hide the call to “salt['config.get']” in the macro by only asking for a “lookup key” where to lookup the list of “source_files”. * docs/TOFS_pattern.rst (Example): document the use of the new optional “lookup” parameter. * template/macros.jinja: add a new optional “lookup” parameter. Lookup “files override” under the “<tplroot>:tofs:sources_files:<lookup key>” and fallback to “source_files” parameter. * template/config/file.sls (template-config-file-file-managed): use the new “lookup” parameter.
1 parent 193bba8 commit 60d43e7

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

docs/TOFS_pattern.rst

+4-6
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,10 @@ We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in t
314314
file.managed:
315315
- name: {{ ntp.config }}
316316
- template: jinja
317-
- source: {{ files_switch(
318-
salt['config.get'](
319-
tplroot ~ ':tofs:source_files:Configure NTP',
320-
['/etc/ntp.conf.jinja']
321-
)
322-
) }}
317+
- source: {{ files_switch(['/etc/ntp.conf.jinja'],
318+
lookup='Configure NTP'
319+
)
320+
}}
323321
- watch_in:
324322
- service: Enable and start NTP service
325323
- require:

template/config/file.sls

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ include:
1313
template-config-file-file-managed:
1414
file.managed:
1515
- name: {{ template.config }}
16-
- source: {{ files_switch(
17-
salt['config.get'](
18-
tplroot ~ ':tofs:source_files:template-config-file-file-managed',
19-
['example.tmpl', 'example.tmpl.jinja']
20-
)
21-
) }}
16+
- source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja'],
17+
lookup='template-config-file-file-managed'
18+
)
19+
}}
2220
- mode: 644
2321
- user: root
2422
- group: root

template/macros.jinja

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{%- macro files_switch(source_files,
2+
lookup=None,
23
default_files_switch=['id', 'os_family'],
34
indent_width=6) %}
45
{#-
@@ -8,6 +9,8 @@
89
910
Params:
1011
* source_files: ordered list of files to look for
12+
* lookup: key under '<tplroot>:tofs:source_files' to override
13+
list of source files
1114
* default_files_switch: if there's no pillar
1215
'<tplroot>:tofs:files_switch' this is the ordered list of grains to
1316
use as selector switch of the directories under
@@ -21,11 +24,8 @@
2124
Deploy configuration:
2225
file.managed:
2326
- name: /etc/yyy/zzz.conf
24-
- source: {{ files_switch(
25-
salt['config.get'](
26-
tplroot ~ ':tofs:source_files:Deploy configuration',
27-
['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja']
28-
)
27+
- source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'],
28+
lookup='Deploy configuration'
2929
) }}
3030
- template: jinja
3131
@@ -52,6 +52,11 @@
5252
tplroot ~ ':tofs:files_switch',
5353
default_files_switch
5454
) %}
55+
{#- Lookup files or fallback to source_files parameter #}
56+
{%- set src_files = salt['config.get'](
57+
tplroot ~ ':tofs:source_files:' ~ lookup,
58+
source_files
59+
) %}
5560
{#- Only add to [''] when supporting older TOFS implementations #}
5661
{%- for path_prefix_ext in [''] %}
5762
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
@@ -66,7 +71,7 @@
6671
{%- do fsl.append('') %}
6772
{%- endif %}
6873
{%- for fs in fsl %}
69-
{%- for source_file in source_files %}
74+
{%- for src_file in src_files %}
7075
{%- if fs %}
7176
{%- set fs_dir = salt['config.get'](fs, fs) %}
7277
{%- else %}
@@ -76,7 +81,7 @@
7681
path_prefix_inc_ext,
7782
files_dir,
7883
fs_dir,
79-
source_file.lstrip('/')
84+
src_file.lstrip('/')
8085
]) %}
8186
{{ url | indent(indent_width, true) }}
8287
{%- endfor %}

0 commit comments

Comments
 (0)