openssh-formula/openssh/libmatchers.jinja

209 lines
5.8 KiB
Plaintext
Raw Normal View History

{#- -*- coding: utf-8 -*- #}
{#- vim: ft=jinja #}
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{%- from tplroot ~ "/libsaltcli.jinja" import cli %}
{%- set query_map = {
"C": "config.get",
"G": "grains.get",
"I": "pillar.get",
} %}
{#- When no part before `@` is provided: #}
{#- - define a filename path, noted `F` #}
{#- - use `salt["config.get"]`, noted `C` #}
{%- set _defaults = {
"type": "F",
"query_type": "C",
} %}
{%- macro parse_matchers(
matchers,
config_get_strategy=None,
log_prefix="libmatchers: "
) %}
{#- matcher format is `[<TYPE>[:<OPTION>]@]<KEY>` #}
{#- each matcher has a type: #}
{#- - `F` to build a file name (the default when no type is set) #}
{#- - `C` to lookup values with `config.get` #}
{#- - `G` to lookup values with `grains.get` #}
{#- - `I` to lookup values with `pillar.get` #}
{#- The `FILE` type option can define query type to build the file name: #}
{#- - `C` for query with `config.get` (the default when to query type is set) #}
{#- - `G` for query with `grains.get` #}
{#- - `I` for query with `pillar.get` #}
{%- set parsed_matchers = [] %}
{%- for matcher in matchers %}
{%- do salt["log.debug"](
log_prefix
~ "process matcher: '"
~ matcher
~ "'"
) %}
{%- set parsed = {} %}
{%- set matcher_parts = matcher.split('@') %}
{%- if matcher_parts | length == 1 %}
{#- By default we load YAML files for config looked up by `config.get` #}
{%- do parsed.update(
{
"type": _defaults["type"],
"option": None,
"query_method": query_map[_defaults["query_type"]],
"query": matcher
}
) %}
{%- do salt["log.debug"](
log_prefix
~ "use built-in defaults for matcher:\n"
~ parsed
| yaml(False)
| indent(4, True)
) %}
{%- else %}
{%- do salt["log.debug"](
log_prefix
~ "parse matcher: '"
~ matcher
~ "'"
) %}
{%- set metadatas = matcher_parts[0].split(":") %}
{%- do parsed.update(
{
"query": matcher_parts[1]
}
) %}
{%- if metadatas | length == 1 %}
{%- do parsed.update(
{
"type": metadatas[0],
"option": "C",
}
) %}
{%- do salt["log.debug"](
log_prefix
~ "parse as 1 metadata matcher:\n"
~ parsed
| yaml(False)
| indent(4, True)
) %}
{%- elif metadatas | length == 2 %}
{%- do parsed.update(
{
"type": metadatas[0],
"option": metadatas[1],
}
) %}
{%- do salt["log.debug"](
log_prefix
~ "parse as 2 metadata matcher:\n"
~ parsed
| yaml(False)
| indent(4, True)
) %}
{%- elif metadatas | length == 3 %}
{%- do parsed.update(
{
"type": metadatas[0],
"option": metadatas[1],
}
) %}
{%- do salt["log.debug"](
log_prefix
~ "parse as 3 metadata matcher:\n"
~ parsed
| yaml(False)
| indent(4, True)
) %}
{%- elif metadatas | length == 4 %}
{%- do parsed.update(
{
"type": metadatas[0],
"option": metadatas[1],
}
) %}
{%- do salt["log.debug"](
log_prefix
~ "parse as 4 metadata matcher:\n"
~ parsed
| yaml(False)
| indent(4, True)
) %}
{%- endif %}
{%- endif %}
{#- The `<OPTION>` has different meaning based on type #}
{%- if query_map.get(parsed.type, False) %}
{%- do parsed.update(
{
"query_method": query_map[parsed.type]
}
) %}
{%- else %}
{%- do parsed.update(
{
"query_method": query_map[
parsed.option
| default("C", boolean=True)
]
}
) %}
{%- endif %}
{#- Add `merge:` option to `salt["config.get"]` if configured #}
{%- if cli in ["minion", "local"] and parsed.query_method == "config.get" and config_get_strategy %}
{%- set merge_opt = {"merge": config_get_strategy} %}
{%- set merge_msg = (
", merge: strategy='"
~ config_get_strategy
~ "'"
) %}
{%- else %}
{%- if cli not in ["minion", "local"] %}
{%- do salt["log.error"](
log_prefix
~ "the 'merge' option of 'config.get' is skipped when the salt command type is '"
~ cli
~ "'"
) %}
{%- endif %}
{%- set merge_opt = {} %}
{%- set merge_msg = "" %}
{%- endif %}
{%- do salt["log.debug"](
log_prefix
~ "lookup '"
~ parsed.query
~ "' with '"
~ parsed.query_method
~ "'"
~ merge_msg
) %}
{%- set values = salt[parsed.query_method](
parsed.query,
default=[],
**merge_opt
) %}
{%- do parsed.update(
{
"value": values
}
) %}
{%- do parsed_matchers.append(parsed) %}
{%- endfor %}
{%- do salt["log.debug"](
log_prefix
~ "parsed matchers:\n"
~ parsed_matchers
| yaml(False)
| indent(4, True)
) %}
{{ parsed_matchers | yaml }}
{%- endmacro %}