Skip to content

Commit

Permalink
refactor(tofs): avoid using “salt['config.get']” for forumla writers
Browse files Browse the repository at this point in the history
We can hide the call to “salt['config.get']” in the macro by only
asking for a namespace where to lookup the “source_files”.

* docs/TOFS_pattern.rst (Example): document the use of the new
  “namespace” parameter.

* template/macros.jinja: add a new “namespace” parameter.
  Lookup files override under
  “<tplroot>:tofs:sources_files:<namespace>” and fallback to
  “source_files” parameter.

* template/config/file.sls (template-config-file-file-managed): use
  the new “namespace” parameter
  • Loading branch information
baby-gnu committed Mar 11, 2019
1 parent 18e5a3d commit 8003492
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
10 changes: 4 additions & 6 deletions docs/TOFS_pattern.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,10 @@ We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in t
file.managed:
- name: {{ ntp.config }}
- template: jinja
- source: {{ files_switch(
salt['config.get'](
tplroot ~ ':tofs:source_files:Configure NTP',
['/etc/ntp.conf.jinja']
)
) }}
- source: {{ files_switch(namespace='Configure NTP',
source_files=['/etc/ntp.conf.jinja']
)
}}
- watch_in:
- service: Enable and start NTP service
- require:
Expand Down
10 changes: 4 additions & 6 deletions template/config/file.sls
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ include:
template-config-file-file-managed:
file.managed:
- name: {{ template.config }}
- source: {{ files_switch(
salt['config.get'](
tplroot ~ ':tofs:source_files:template-config-file-file-managed',
['example.tmpl', 'example.tmpl.jinja']
)
) }}
- source: {{ files_switch(namespace='template-config-file-file-managed',
source_files=['example.tmpl', 'example.tmpl.jinja']
)
}}
- mode: 644
- user: root
- group: root
Expand Down
19 changes: 12 additions & 7 deletions template/macros.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{%- macro files_switch(source_files,
{%- macro files_switch(namespace,
source_files,
default_files_switch=['id', 'os_family'],
indent_width=6) %}
{#-
Expand All @@ -7,6 +8,8 @@
Files Switch (TOFS) pattern.
Params:
* namespace: key under '<tplroot>:tofs:source_files' to override
list of source files
* source_files: ordered list of files to look for
* default_files_switch: if there's no pillar
'<tplroot>:tofs:files_switch' this is the ordered list of grains to
Expand All @@ -21,11 +24,8 @@
Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source: {{ files_switch(
salt['config.get'](
tplroot ~ ':tofs:source_files:Deploy configuration',
['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja']
)
- source: {{ files_switch(namespace='Deploy configuration',
['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja']
) }}
- template: jinja
Expand All @@ -52,6 +52,11 @@
tplroot ~ ':tofs:files_switch',
default_files_switch
) %}
{# Lookup files or fallback to source_files parameter #}
{%- set files = salt['config.get'](
tplroot ~ ':tofs:source_files:' ~ namespace,
source_files
) %}
{#- Only add to [''] when supporting older TOFS implementations #}
{%- for path_prefix_ext in [''] %}
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
Expand All @@ -66,7 +71,7 @@
{%- do fsl.append('') %}
{%- endif %}
{%- for fs in fsl %}
{%- for source_file in source_files %}
{%- for source_file in files %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
{%- else %}
Expand Down

0 comments on commit 8003492

Please sign in to comment.