Skip to content

Commit 985b2b7

Browse files
committed
feat(map): enable import of mapdata from outside the formula
`tplroot` can't be set correctly when the import: - is done from another top directory than the formula directory - the import is done `with context` In this case, the `tpldir` is set to the directory of the importer `.sls` file instead of the `.jinja` imported one. We force the `without context` which permits to directly use `tpldir` as the `tplroot` which is the directory of the imported file. BREAKING CHANGE: `map.jinja` import must use `without context` BREAKING CHANGE: `libmapstack.jinja` import must use `without context` BREAKING CHANGE: `libmatchers.jinja` import must use `without context`
1 parent bbe1c78 commit 985b2b7

File tree

12 files changed

+44
-17
lines changed

12 files changed

+44
-17
lines changed

TEMPLATE/_mapdata/init.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split("/")[0] %}
6-
{%- from tplroot ~ "/map.jinja" import mapdata with context %}
6+
{%- from tplroot ~ "/map.jinja" import mapdata without context %}
77
88
{%- set _mapdata = {
99
"values": mapdata,

TEMPLATE/config/clean.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_service_clean = tplroot ~ '.service.clean' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
99
include:
1010
- {{ sls_service_clean }}

TEMPLATE/config/file.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_package_install = tplroot ~ '.package.install' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
99
1010
include:

TEMPLATE/libmapstack.jinja

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
{#- -*- coding: utf-8 -*- #}
22
{#- vim: ft=jinja #}
33

4-
{#- Get the `tplroot` from `tpldir` #}
5-
{%- set tplroot = tpldir.split("/")[0] %}
4+
{%- if not tplfile.endswith("/libmapstack.jinja") %}
5+
{#- Force import `without context` #}
6+
{#- `with context` override the `tplfile` and `tpldir` variables #}
7+
{{- raise("Import error: libmapstack.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
8+
{%- elif tplfile.startswith("../") %}
9+
{#- Force import with absolute path #}
10+
{{- raise("Import error: libmapstack.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
11+
{%- endif %}
12+
13+
{#- `tplroot` is the directory of the current imported file #}
14+
{%- set tplroot = tpldir %}
615
{%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map %}
716

817
{%- set _default_config_dirs = [

TEMPLATE/libmatchers.jinja

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
{#- -*- coding: utf-8 -*- #}
22
{#- vim: ft=jinja #}
33

4+
{%- if not tplfile.endswith("/libmatchers.jinja") %}
5+
{#- Force import `without context` #}
6+
{#- `with context` override the `tplfile` and `tpldir` variables #}
7+
{{- raise("Import error: libmatchers.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
8+
{%- elif tplfile.startswith("../") %}
9+
{#- Force import with absolute path #}
10+
{{- raise("Import error: libmatchers.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
11+
{%- endif %}
12+
413
{#- Get the `tplroot` from `tpldir` #}
5-
{%- set tplroot = tpldir.split("/")[0] %}
14+
{%- set tplroot = tpldir %}
615
{%- from tplroot ~ "/libsaltcli.jinja" import cli %}
716

817
{%- set query_map = {

TEMPLATE/map.jinja

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
# -*- coding: utf-8 -*-
2-
# vim: ft=jinja
1+
{#- -*- coding: utf-8 -*- #}
2+
{#- vim: ft=jinja #}
33

4-
{#- Get the `tplroot` from `tpldir` #}
5-
{%- set tplroot = tpldir.split("/")[0] %}
6-
{%- from tplroot ~ "/libmapstack.jinja" import mapstack %}
4+
{%- if not tplfile.endswith("/map.jinja") %}
5+
{#- Force import `without context` #}
6+
{#- `with context` override the `tplfile` and `tpldir` variables #}
7+
{{- raise("Import error: map.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
8+
{%- elif tplfile.startswith("../") %}
9+
{#- Force import with absolute path #}
10+
{{- raise("Import error: map.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
11+
{%- endif %}
12+
13+
{#- `tplroot` is the directory of the current imported file #}
14+
{%- set tplroot = tpldir %}
15+
{%- from tplroot ~ "/libmapstack.jinja" import mapstack without context %}
716

817
{#- Where to lookup parameters source files #}
918
{%- set formula_param_dir = tplroot ~ "/parameters" %}

TEMPLATE/package/clean.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_config_clean = tplroot ~ '.config.clean' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
99
include:
1010
- {{ sls_config_clean }}

TEMPLATE/package/install.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
6-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
6+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
77
88
TEMPLATE-package-install-pkg-installed:
99
pkg.installed:

TEMPLATE/service/clean.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
6-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
6+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
77
88
TEMPLATE-service-clean-service-dead:
99
service.dead:

TEMPLATE/service/running.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_config_file = tplroot ~ '.config.file' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
99
include:
1010
- {{ sls_config_file }}

TEMPLATE/subcomponent/config/clean.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_service_clean = tplroot ~ '.service.clean' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
99
include:
1010
- {{ sls_service_clean }}

TEMPLATE/subcomponent/config/file.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
66
{%- set sls_config_file = tplroot ~ '.config.file' %}
7-
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %}
7+
{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %}
88
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
99
1010
include:

0 commit comments

Comments
 (0)