Basic Consul concepts, output logging / debugging

I’m trying to learn how to use consul-template, and reading the docs on the github project has a lot of examples, but it seems to assume that I already know a lot about the ecosystem. Problem is, I don’t.

For example, this is some sample code given:

{{ with node }}
{{ .Node.Address }}{{ end }}

Where can I find more information on “node”? Specifically, which properties are available other than “Address”?

And why do some examples use “.Node.Foo” format, some use “.Data.Foo”, and others simply use “.Foo”?

Ideally, I’d be able to ‘dump’ out the data so that I could see it, but every time I try that my job fails with some unhelpful error (because I’m doing something really wrong, evidently)…

Template failed: (dynamic): execute: template: :13:17: executing "" at <toJSONPretty>: wrong type for value; expected map[string]interface {}; got []*dependency.HealthService

Any help would be appreciated – I’m flailing and failing!

Hello,

First, thanks for bringing to our attention that the documentation is not clear. We’ll see what we can do to improve it.

To answer your question, the details for the various fields available in the resultant structs returned by a given API function can be found by referencing Consul’s Golang API documentation. For example, the node function returns a CatalogNode struct which contains two fields: Node and Services (an map of AgentService structs).

I realize it may not be readily apparent as to which Go struct is returned by a given consul-template function, and this is certainly one opportunity we have to improve the docs. In the interim, you can use Go’s Template printf function to dump & inspect the result returned from a given consul-template function to determine the struct type.

# node.tpl
{{ with node }}
{{ printf "%#v" . }}
{{ end }}

# CLI
$ consul-template -template node.tpl -dry -once
>

&dependency.CatalogNode{Node:(*dependency.Node)(0x21da150), Services:[]*dependency.CatalogNodeService{(*dependency.CatalogNodeService)(0x21da120)}}

I hope this helps!

That is exceptionally helpful! I figured there was some API documentation but I had no luck finding it, and the printf is just what I was looking for. It’s much appreciated!

I’ve been using the following funny chain of functions to get a pretty printed view of arbitrary objects:

{{ with node -}}
{{ . | toJSON | parseJSON | toJSONPretty }}{{ end }}

unfortunately toJSONPretty and toYAML can’t handle types? or like, they only accept map[string]interface {} … soooo, to json-parse-town we go :woman_shrugging:

Thanks for the idea. Ticket filed.