from typing_extensions import Literal
+from knot_resolver_manager.datamodel.templates import template_from_str
from knot_resolver_manager.datamodel.types import (
Dir,
DNSRecordTypeEnum,
from knot_resolver_manager.utils.modeling import ConfigSchema
from knot_resolver_manager.utils.modeling.base_schema import lazy_default
+_CACHE_CLEAR_TEMPLATE = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import tojson %}"
+ "{% from 'macros/cache_macros.lua.j2' import cache_clear %}"
+ "{{ tojson(cache_clear(params)) }}"
+)
+
class CacheClearRPCSchema(ConfigSchema):
name: Optional[DomainName] = None
rr_type: Optional[DNSRecordTypeEnum] = None
chunk_size: IntPositive = IntPositive(100)
+ def _validate(self) -> None:
+ if self.rr_type and not self.exact_name:
+ raise ValueError("'rr-type' is only supported with 'exact-name: true'")
+
+ def render_lua(self) -> str:
+ return _CACHE_CLEAR_TEMPLATE.render(params=self) # pyright: reportUnknownMemberType=false
+
class PrefillSchema(ConfigSchema):
"""
--- /dev/null
+{% from 'macros/common_macros.lua.j2' import boolean, quotes, qtype_table %}
+
+
+{% macro cache_clear(params) -%}
+cache.clear(
+{{- quotes(params.name) if params.name else 'nil' -}},
+{{- boolean(params.exact_name) -}},
+{{- qtype_table(params.rr_type) if params.rr_type else 'nil' -}},
+{{- params.chunk_size if not params.exact_name else 'nil' -}}
+)
+{%- endmacro %}
+{% macro tojson(object) -%}
+tojson({{ object }})
+{%- endmacro %}
+
{% macro quotes(string) -%}
'{{ string }}'
{%- endmacro %}
--- /dev/null
+from typing import Any
+
+import pytest
+
+from knot_resolver_manager.datamodel.cache_schema import CacheClearRPCSchema
+from knot_resolver_manager.datamodel.templates import template_from_str
+
+
+@pytest.mark.parametrize(
+ "val,res",
+ [
+ ({}, "cache.clear(nil,false,nil,100)"),
+ ({"chunk-size": 200}, "cache.clear(nil,false,nil,200)"),
+ ({"name": "example.com.", "exact-name": True}, "cache.clear('example.com.',true,nil,nil)"),
+ (
+ {"name": "example.com.", "exact-name": True, "rr-type": "AAAA"},
+ "cache.clear('example.com.',true,kres.type.AAAA,nil)",
+ ),
+ ],
+)
+def test_cache_clear(val: Any, res: Any):
+ tmpl_str = "{% from 'macros/cache_macros.lua.j2' import cache_clear %}{{ cache_clear(x) }}"
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=CacheClearRPCSchema(val)) == res