consul-template how to access iteration of a range for a map

2,559 views
Skip to first unread message

r...@telnyx.com

unread,
Oct 20, 2015, 12:40:37 AM10/20/15
to Consul
Have had no luck in finding a method to apply an auto integer for each line generated for a config file, can I access an integer of the current iteration for a range somehow?

the k/v data structure resembles:

service/ref/ref/ref/gateway/ip
                                           | 
                                           |`55.555.139.136
                                            `55.555.139.137

service/ref/ref/ref/gateway/ip/55.555.139.137
                                                        | 
                                                        |`port   -- 5555
                                                        |`setid  -- 100
                                                        |`flags  -- 2:0
                                                        |`attributes -- item0=id1;item1=4000;item2=100
                                                         `description -- "GW 2"  

my template resembles this:

{{ range $key, $pairs := tree "key/namespace" | byKey }}
{{ with tree ($key | printf "key/namespace/%s") | explode }}{{ can I haz integer of iteration for auto int? }}:{{ .data }}:{{ .data }}... etc{{ end }}{{ end }}

Thought I could get this for free but seems like it needs to be handled explicitly as another k/v externally. :(

Seth Vargo

unread,
Oct 20, 2015, 1:07:44 AM10/20/15
to consu...@googlegroups.com
Hi Rob,

If you pass two arguments to the range function, one should be the index: https://golang.org/pkg/text/template/

Best,
Seth

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/consul/issues
IRC: #consul on Freenode
---
You received this message because you are subscribed to the Google Groups "Consul" group.
To unsubscribe from this group and stop receiving emails from it, send an email to consul-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/consul-tool/0ce21890-29c8-4728-8547-b1eded90a032%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

r...@telnyx.com

unread,
Oct 20, 2015, 5:33:49 PM10/20/15
to Consul
Unfortunately, I'm not working with an array so I am unable to use the index as integer for the beginning of each line in the config as an id(int,auto). However, I am able to get a total of items but am unable to get it working in a range with a loop to capture the iteration in order to insert for each config file line. Any help?

so my template now resembles this:

{{ $total := tree "key/namespace" | byKey | len }}
{{ range $i := loop ($total | printf %d) }}
{{ range $key, $pairs := tree "key/namespace" | byKey }}
{{ with tree ($key | printf "key/namespace/%s") | explode }}{{ $i }}:{{ .data }}:{{ .data }}... etc{{ end }}{{ end }}

lines in the config would result as such depending on the number items in top level found when doing byKey

0:55.555.139.136:5555:item0=id1;item1=4000;item2=100:"GW 1"
1:55.555.139.137:5554:item0=id1;item1=4000;item2=100:"GW 2"

having problems when I attempt pass an argument to loop function via interpolation with printf ( unexpected "%" in operand ). 

this would accomplish what I'm after if I can figured out how to pass the total iterations to loop to capture the iteration.

I've tried a few things, not sure how to proceed as I keep running into issues with data type.
Message has been deleted

r...@telnyx.com

unread,
Oct 20, 2015, 9:49:06 PM10/20/15
to Consul
ok, so I resolved my problem with variable interpolation... but the range loop creates the problem of looping the total of items per loop.

{{ range $i := loop (printf "%d" $total | parseInt) }}

0:55.555.139.136
0:55.555.139.137
1:55.555.139.136
1:55.555.139.137

resorting to handling this externally.

perhaps a count / sequence helper function in a future release to used with range when not working with an array? thanks

On Monday, October 19, 2015 at 11:40:37 PM UTC-5, r...@telnyx.com wrote:
Reply all
Reply to author
Forward
0 new messages