Add GetBool function in KV provider, used by passHostHeader

This commit is contained in:
emile 2015-11-22 21:24:43 +01:00
parent c8a0a83e2b
commit 6e62625ebf
3 changed files with 72 additions and 5 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/docker/libkv" "github.com/docker/libkv"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
"github.com/emilevauge/traefik/types" "github.com/emilevauge/traefik/types"
"strconv"
) )
// Kv holds common configurations of key-value providers. // Kv holds common configurations of key-value providers.
@ -75,6 +76,7 @@ func (provider *Kv) loadConfig() *types.Configuration {
var KvFuncMap = template.FuncMap{ var KvFuncMap = template.FuncMap{
"List": provider.list, "List": provider.list,
"Get": provider.get, "Get": provider.get,
"GetBool": provider.getBool,
"Last": provider.last, "Last": provider.last,
} }
@ -104,7 +106,7 @@ func (provider *Kv) get(keys ...string) string {
joinedKeys := strings.Join(keys, "") joinedKeys := strings.Join(keys, "")
keyPair, err := provider.kvclient.Get(joinedKeys) keyPair, err := provider.kvclient.Get(joinedKeys)
if err != nil { if err != nil {
log.Debug("Error getting key: ", joinedKeys, err) log.Error("Error getting key: ", joinedKeys, err)
return "" return ""
} else if keyPair == nil { } else if keyPair == nil {
return "" return ""
@ -112,6 +114,16 @@ func (provider *Kv) get(keys ...string) string {
return string(keyPair.Value) 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 { func (provider *Kv) last(key string) string {
splittedKey := strings.Split(key, "/") splittedKey := strings.Split(key, "/")
return splittedKey[len(splittedKey)-1] return splittedKey[len(splittedKey)-1]

View file

@ -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) { func TestKvLast(t *testing.T) {
cases := []struct { cases := []struct {
key string key string

View file

@ -28,7 +28,7 @@
{{$frontend := Last .}} {{$frontend := Last .}}
[frontends.{{$frontend}}] [frontends.{{$frontend}}]
backend = "{{Get . "/backend"}}" backend = "{{Get . "/backend"}}"
passHostHeader = "{{Get . "/passHostHeader"}}" passHostHeader = "{{GetBool . "/passHostHeader"}}"
{{$routes := List . "/routes/"}} {{$routes := List . "/routes/"}}
{{range $routes}} {{range $routes}}
[frontends.{{$frontend}}.routes.{{Last .}}] [frontends.{{$frontend}}.routes.{{Last .}}]