Support datacenter listing

This commit is contained in:
Armon Dadgar 2013-12-12 10:35:50 -08:00
parent 4a2230bfc8
commit 255be3559c
3 changed files with 56 additions and 0 deletions

View File

@ -47,3 +47,19 @@ func (c *Catalog) Deregister(args *rpc.DeregisterRequest, reply *struct{}) error
}
return nil
}
// ListDatacenters is used to query for the list of known datacenters
func (c *Catalog) ListDatacenters(args *struct{}, reply *[]string) error {
c.srv.remoteLock.RLock()
defer c.srv.remoteLock.RUnlock()
// Read the known DCs
var dcs []string
for dc := range c.srv.remoteConsuls {
dcs = append(dcs, dc)
}
// Return
*reply = dcs
return nil
}

View File

@ -5,6 +5,7 @@ import (
"github.com/hashicorp/consul/rpc"
nrpc "net/rpc"
"os"
"sort"
"testing"
"time"
)
@ -144,3 +145,41 @@ func TestCatalogDeregister(t *testing.T) {
t.Fatalf("err: %v", err)
}
}
func TestCatalogListDatacenters(t *testing.T) {
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
client := rpcClient(t, s1)
defer client.Close()
dir2, s2 := testServerDC(t, "dc2")
defer os.RemoveAll(dir2)
defer s2.Shutdown()
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfWANConfig.MemberlistConfig.Port)
if err := s2.JoinWAN(addr); err != nil {
t.Fatalf("err: %v", err)
}
time.Sleep(10 * time.Millisecond)
var out []string
if err := client.Call("Catalog.ListDatacenters", struct{}{}, &out); err != nil {
t.Fatalf("err: %v", err)
}
// Sort the dcs
sort.Strings(out)
if len(out) != 2 {
t.Fatalf("bad: %v", out)
}
if out[0] != "dc1" {
t.Fatalf("bad: %v", out)
}
if out[1] != "dc2" {
t.Fatalf("bad: %v", out)
}
}

View File

@ -82,6 +82,7 @@ func (s *StateStore) initialize() error {
`CREATE TABLE nodes (name text unique, address text);`,
`CREATE TABLE services (node text REFERENCES nodes(name) ON DELETE CASCADE, service text, tag text, port integer);`,
`CREATE INDEX servName ON services(service);`,
`CREATE INDEX nodeName ON services(node);`,
}
for _, t := range tables {
if _, err := s.db.Exec(t); err != nil {