is a younger brother of envoy and it makes a bridge between Consul and application data structures a little more beautiful.
In order to follow all the docs below, bring attaché in:
go get github.com/tolitius/attache
attache.MapToConsul(consulapi.DefaultConfig(), configMap)
takes a consul config and a configMap, which is a map[string]string
,
and inserts all the {key, value} pairs into Consul.
For exampe let's say we have a configMap
Go map:
{"hubble/store": "spacecraft://tape"
"hubble/camera/mode": "color"
"hubble/mission/target": "Horsehead Nebula"}
attaché could write all these entries into Consul respecting /
hierarchy:
attache.MapToConsul(consulapi.DefaultConfig(), configMap)
will bring Hubble "online" preserving config's hierarchy:
and you can see the Consul log confirming it:
2017/10/18 15:21:53 [DEBUG] http: Request PUT /v1/kv/hubble/store (367.261µs) from=127.0.0.1:59376
2017/10/18 15:21:53 [DEBUG] http: Request PUT /v1/kv/hubble/camera/mode (278.666µs) from=127.0.0.1:59376
2017/10/18 15:21:53 [DEBUG] http: Request PUT /v1/kv/hubble/mission/target (361.468µs) from=127.0.0.1:59376
attache.ConsulToMap(consulapi.DefaultConfig(), pathOffset)
returns a map[string]string
of all the k/v
under the pathOffset
.
For exampe let's say we have this structure in Consul at localhost:8500
:
{"hubble":
{"store": "spacecraft://tape",
"camera":
{"mode": "color"},
"mission":
{"target": "Horsehead Nebula"}}}
attaché could read it all into a Go map by:
config := consulapi.DefaultConfig()
config.Address = "localhost:8500"
attache.ConsulToMap(config, "/hubble")
which would produce a Go map:
{"hubble/store": "spacecraft://tape"
"hubble/camera/mode": "color"
"hubble/mission/target": "Horsehead Nebula"}
where keys and values are string
s
It will do that by calling Consul once (i.e. one GET), starting from the path offset we provided ("/hubble"
):
2017/10/18 15:21:53 [DEBUG] http: Request GET /v1/kv/hubble?recurse= (214.843µs) from=127.0.0.1:59378
When working with several environments, application configs could live under different env specisfic offsets, for example /dev
or /qa
, etc.
By default attaché returns a map with keys that include a full path starting from the root. Frequently applications should not care about this difference in env specific offsets, hence we can ask attaché to exclude offset from key names:
attache.ConsulToMap(consulapi.DefaultConfig(), pathOffset, keysWithOffset)
the last optional argument is a boolean keysWithOffset
which we could pass as false
config := consulapi.DefaultConfig()
config.Address = "localhost:8500"
attache.ConsulToMap(config, "/hubble", false)
which would produce a Go map:
{"/store": "spacecraft://tape"
"/camera/mode": "color"
"/mission/target": "Horsehead Nebula"}
notice no "hubble"
offset in key names.
attaché relies on the official Consul API and all attaché functions take consul/api.Config, hence any of its options can be set either by overriding the config:
consulConfig := consulapi.DefaultconsulConfig()
consulConfig.Address = "localhost:8500"
consulConfig.Datacenter = "fn42-datacenter"
consulConfig.Token = "7a0f3b39-8871-e16e-2101-c1b30a911883"
appConfig := attache.ConsulToMap(consulConfig, "/hubble")
Or by using "official" ENV variables from Consul API.
Copyright © 2017 tolitius
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.