Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template fliter and function together usage #93

Open
itshikanov opened this issue Jul 11, 2023 · 1 comment
Open

Template fliter and function together usage #93

itshikanov opened this issue Jul 11, 2023 · 1 comment
Assignees

Comments

@itshikanov
Copy link

Hello.
I.m try convert my confd config to remco.

try to use filter and function together from here
https://github.com/HeavyHorst/remco/tree/26a321844506ca53d2840f22e41ed6ea89043cba/docs/content/template

here the code

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
{% set conn_url = data.conn_url | split:"/" | index:"2"  %}
        server {{member.Key | base}} {{ replace (conn_url, "@", "/", -1) | base}}  maxconn 300 check port {{ data.api_url | split:"/" | index:"2" | split:":" | index:"1" }}
{% endfor %}

and here another try

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
        server {{member.Key | base}} {{ replace (data.conn_url | split:"/" | index:"2", "@", "/", -1) | base}}  maxconn 300 check port {{ data.api_url | split:"/" | index:"2" | split:":" | index:"1" }}
{% endfor %}

Got the error with replace function

2023-07-11T09:12:55.566+0300 [DEBUG] retrieving keys: backend=zookeeper key_prefix=/patroni/15-main prefix=remco[206] resource=haproxy
2023-07-11T09:12:55.628+0300 [DEBUG] compiling source template: prefix=remco[206] resource=haproxy template=/tmp/haproxy.tmpl
2023-07-11T09:12:55.628+0300 [ERROR] default handler: prefix=remco[206] resource=haproxy error="createStageFileAndSync failed: create stage file failed: template execution failed: [Error (where: execution) in /tmp/haproxy.tmpl | Line 33 Col 41 near 'replace'] Function input argument 0 of 'replace' must be of type string or *pongo2.Value (not *pongo2.Value)."

How can i use function and filter together?

@HeavyHorst HeavyHorst self-assigned this Jul 11, 2023
@itshikanov
Copy link
Author

itshikanov commented Jul 11, 2023

I'm trying to use Custom filters JavaScript replace filter.
Here is example
https://github.com/HeavyHorst/remco/blob/26a321844506ca53d2840f22e41ed6ea89043cba/docs/content/template/template-filters.md

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
{{ data.conn_url | split:"/" | index:"2" | replace:"@,/"  }}
{% endfor %}

got error

2023-07-11T15:30:29.414+0300 [INFO]  set backend nodes: backend=zookeeper nodes=["192.168.50.1:2181"] prefix=remco[1642]
2023/07/11 15:30:29 Connected to 192.168.50.1:2181
2023/07/11 15:30:29 Authenticated: id=72058294651455021, timeout=4000
2023/07/11 15:30:29 Re-submitting `0` credentials after reconnect
2023-07-11T15:30:29.420+0300 [ERROR] failed to process: prefix=remco[1642] resource=haproxy error="createStageFileAndSync failed: create stage file failed: template execution failed: [Error (where: javascript-filter:replace) | Line 35 Col 44 near 'replace'] TypeError: Object has no member 'replace' at replace (<eval>:10:23(48))"
2023-07-11T15:30:29.420+0300 [ERROR] not all templates could be rendered, trying again after 22 seconds: prefix=remco[1642] resource=haproxy

That work

{{ data.conn_url | toJSON | replace:"@,/" }}

I think because "data.conn_url | toJSON" become String. But if use another pipe, the type of data is changing. If there was toSRING filter at least, it will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants