From 6e62625ebf17c2e54ea7f2b16464bf1d0943e07d Mon Sep 17 00:00:00 2001 From: emile Date: Sun, 22 Nov 2015 21:24:43 +0100 Subject: [PATCH] Add GetBool function in KV provider, used by passHostHeader --- provider/kv.go | 20 +++++++++++++---- provider/kv_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ templates/kv.tmpl | 2 +- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/provider/kv.go b/provider/kv.go index 0ada556a0..ec5966554 100644 --- a/provider/kv.go +++ b/provider/kv.go @@ -11,6 +11,7 @@ import ( "github.com/docker/libkv" "github.com/docker/libkv/store" "github.com/emilevauge/traefik/types" + "strconv" ) // Kv holds common configurations of key-value providers. @@ -73,9 +74,10 @@ func (provider *Kv) loadConfig() *types.Configuration { provider.Prefix, } var KvFuncMap = template.FuncMap{ - "List": provider.list, - "Get": provider.get, - "Last": provider.last, + "List": provider.list, + "Get": provider.get, + "GetBool": provider.getBool, + "Last": provider.last, } configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects) @@ -104,7 +106,7 @@ func (provider *Kv) get(keys ...string) string { joinedKeys := strings.Join(keys, "") keyPair, err := provider.kvclient.Get(joinedKeys) if err != nil { - log.Debug("Error getting key: ", joinedKeys, err) + log.Error("Error getting key: ", joinedKeys, err) return "" } else if keyPair == nil { return "" @@ -112,6 +114,16 @@ func (provider *Kv) get(keys ...string) string { return string(keyPair.Value) } +func (provider *Kv) getBool(keys ...string) bool { + value := provider.get(keys...) + b, err := strconv.ParseBool(string(value)) + if err != nil { + log.Error("Error getting key: ", strings.Join(keys, ""), err) + return false + } + return b +} + func (provider *Kv) last(key string) string { splittedKey := strings.Split(key, "/") return splittedKey[len(splittedKey)-1] diff --git a/provider/kv_test.go b/provider/kv_test.go index 3cb79f257..6873e4d18 100644 --- a/provider/kv_test.go +++ b/provider/kv_test.go @@ -194,6 +194,61 @@ func TestKvGet(t *testing.T) { } } +func TestKvGetBool(t *testing.T) { + cases := []struct { + provider *Kv + keys []string + expected bool + }{ + { + provider: &Kv{ + kvclient: &Mock{ + KVPairs: []*store.KVPair{ + { + Key: "foo", + Value: []byte("true"), + }, + }, + }, + }, + keys: []string{"foo"}, + expected: true, + }, + { + provider: &Kv{ + kvclient: &Mock{ + KVPairs: []*store.KVPair{ + { + Key: "foo", + Value: []byte("false"), + }, + }, + }, + }, + keys: []string{"foo"}, + expected: false, + }, + } + + for _, c := range cases { + actual := c.provider.getBool(c.keys...) + if actual != c.expected { + t.Fatalf("expected %v, got %v for %v and %v", c.expected, actual, c.keys, c.provider) + } + } + + // Error case + provider := &Kv{ + kvclient: &Mock{ + Error: true, + }, + } + actual := provider.get("anything") + if actual != "" { + t.Fatalf("Should have return nil, got %v", actual) + } +} + func TestKvLast(t *testing.T) { cases := []struct { key string diff --git a/templates/kv.tmpl b/templates/kv.tmpl index de66676a7..272093e2f 100644 --- a/templates/kv.tmpl +++ b/templates/kv.tmpl @@ -28,7 +28,7 @@ {{$frontend := Last .}} [frontends.{{$frontend}}] backend = "{{Get . "/backend"}}" - passHostHeader = "{{Get . "/passHostHeader"}}" + passHostHeader = "{{GetBool . "/passHostHeader"}}" {{$routes := List . "/routes/"}} {{range $routes}} [frontends.{{$frontend}}.routes.{{Last .}}]