Skip to content

Commit

Permalink
Issue #55: Expand ${DC} to consul datacenter
Browse files Browse the repository at this point in the history
Expand the ${DC} variable in a urlprefix to the
data center of the consul agent.
  • Loading branch information
magiconair committed May 3, 2016
1 parent 3990612 commit d1f478f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
17 changes: 15 additions & 2 deletions registry/consul/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package consul

import (
"log"
"os"
"strings"
)

// parseURLPrefixTag expects an input in the form of 'tag-host/path'
// and returns the lower cased host plus the path unaltered if the
// prefix matches the tag.
func parseURLPrefixTag(s, prefix string) (host, path string, ok bool) {
func parseURLPrefixTag(s, prefix string, env map[string]string) (host, path string, ok bool) {
if !strings.HasPrefix(s, prefix) {
return "", "", false
}
Expand All @@ -20,6 +21,18 @@ func parseURLPrefixTag(s, prefix string) (host, path string, ok bool) {
return "", "", false
}

host, path = strings.ToLower(strings.TrimSpace(p[0])), "/"+strings.TrimSpace(p[1])
// expand $x or ${x} to env[x] or ""
expand := func(s string) string {
return os.Expand(s, func(x string) string {
if env == nil {
return ""
}
return env[x]
})
}

host = strings.ToLower(expand(strings.TrimSpace(p[0])))
path = "/" + expand(strings.TrimSpace(p[1]))

return host, path, true
}
58 changes: 42 additions & 16 deletions registry/consul/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,61 @@ import "testing"
func TestParseTag(t *testing.T) {
prefix := "p-"
tests := []struct {
tag, host, path string
ok bool
tag string
env map[string]string
host string
path string
ok bool
}{
{"p", "", "", false},
{"p-", "", "", false},
{"p- ", "", "", false},
{"p-/", "", "/", true},
{"p-/ ", "", "/", true},
{"p- / ", "", "/", true},
{"p-/foo", "", "/foo", true},
{"p-bar/foo", "bar", "/foo", true},
{"p-bar/foo/foo", "bar", "/foo/foo", true},
{"p-www.bar.com/foo/foo", "www.bar.com", "/foo/foo", true},
{"p-WWW.BAR.COM/foo/foo", "www.bar.com", "/foo/foo", true},
{tag: "p", host: "", path: "", ok: false},
{tag: "p-", host: "", path: "", ok: false},
{tag: "p- ", host: "", path: "", ok: false},
{tag: "p-/", host: "", path: "/", ok: true},
{tag: "p-/ ", host: "", path: "/", ok: true},
{tag: "p- / ", host: "", path: "/", ok: true},
{tag: "p-/foo", host: "", path: "/foo", ok: true},
{tag: "p-bar/foo", host: "bar", path: "/foo", ok: true},
{tag: "p-bar/foo/foo", host: "bar", path: "/foo/foo", ok: true},
{tag: "p-www.bar.com/foo/foo", host: "www.bar.com", path: "/foo/foo", ok: true},
{tag: "p-WWW.BAR.COM/foo/foo", host: "www.bar.com", path: "/foo/foo", ok: true},

{
tag: "p-$x/$y",
host: "", path: "/",
ok: true,
},
{
tag: "p-${x}/${y}",
host: "", path: "/",
ok: true,
},
{
tag: "p-$x/$Y",
env: map[string]string{"x": "Xx", "Y": "Yy"},
host: "xx", path: "/Yy",
ok: true,
},
{
tag: "p-${x}/${Y}",
env: map[string]string{"x": "Xx", "Y": "Yy"},
host: "xx", path: "/Yy",
ok: true,
},
}

for i, tt := range tests {
host, path, ok := parseURLPrefixTag(tt.tag, prefix)
host, path, ok := parseURLPrefixTag(tt.tag, prefix, tt.env)
if got, want := ok, tt.ok; got != want {
t.Errorf("%d: got %v want %v", i, got, want)
}
if !ok {
continue
}
if got, want := host, tt.host; got != want {
t.Errorf("%d: got %s want %s", i, got, want)
t.Errorf("%d: got host %q want %q", i, got, want)
}
if got, want := path, tt.path; got != want {
t.Errorf("%d: got %s want %s", i, got, want)
t.Errorf("%d: got path %q want %q", i, got, want)
}
}
}
12 changes: 11 additions & 1 deletion registry/consul/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,23 @@ func serviceConfig(client *api.Client, name string, passing map[string]bool, tag
return nil
}

dc, err := datacenter(client)
if err != nil {
log.Printf("[WARN] consul: Error getting datacenter. %s", err)
return nil
}

q := &api.QueryOptions{RequireConsistent: true}
svcs, _, err := client.Catalog().Service(name, "", q)
if err != nil {
log.Printf("[WARN] consul: Error getting catalog service %s. %v", name, err)
return nil
}

env := map[string]string{
"DC": dc,
}

for _, svc := range svcs {
// check if the instance is in the list of instances
// which passed the health check
Expand All @@ -78,7 +88,7 @@ func serviceConfig(client *api.Client, name string, passing map[string]bool, tag
}

for _, tag := range svc.ServiceTags {
if host, path, ok := parseURLPrefixTag(tag, tagPrefix); ok {
if host, path, ok := parseURLPrefixTag(tag, tagPrefix, env); ok {
name, addr, port := svc.ServiceName, svc.ServiceAddress, svc.ServicePort

// use consul node address if service address is not set
Expand Down

0 comments on commit d1f478f

Please sign in to comment.