From 551a4fc031ac64dadc9072d3d278f24e325be54e Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 16 Feb 2016 11:45:29 -0800 Subject: [PATCH] Adds support for EnableTagOverride to the API client. --- api/agent.go | 26 +++++++------ api/agent_test.go | 43 +++++++++++++++++++++ api/catalog.go | 15 ++++---- api/catalog_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 19 deletions(-) diff --git a/api/agent.go b/api/agent.go index e4466a651..183ae6efd 100644 --- a/api/agent.go +++ b/api/agent.go @@ -18,11 +18,12 @@ type AgentCheck struct { // AgentService represents a service known to the agent type AgentService struct { - ID string - Service string - Tags []string - Port int - Address string + ID string + Service string + Tags []string + Port int + Address string + EnableTagOverride bool } // AgentMember represents a cluster member known to the agent @@ -42,13 +43,14 @@ type AgentMember struct { // AgentServiceRegistration is used to register a new service type AgentServiceRegistration struct { - ID string `json:",omitempty"` - Name string `json:",omitempty"` - Tags []string `json:",omitempty"` - Port int `json:",omitempty"` - Address string `json:",omitempty"` - Check *AgentServiceCheck - Checks AgentServiceChecks + ID string `json:",omitempty"` + Name string `json:",omitempty"` + Tags []string `json:",omitempty"` + Port int `json:",omitempty"` + Address string `json:",omitempty"` + EnableTagOverride bool `json:",omitempty"` + Check *AgentServiceCheck + Checks AgentServiceChecks } // AgentCheckRegistration is used to register a new check diff --git a/api/agent_test.go b/api/agent_test.go index c49696aeb..c5e1854dd 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -196,6 +196,49 @@ func TestAgent_ServiceAddress(t *testing.T) { } } +func TestAgent_EnableTagOverride(t *testing.T) { + t.Parallel() + c, s := makeClient(t) + defer s.Stop() + + agent := c.Agent() + + reg1 := &AgentServiceRegistration{ + Name: "foo1", + Port: 8000, + Address: "192.168.0.42", + EnableTagOverride: true, + } + reg2 := &AgentServiceRegistration{ + Name: "foo2", + Port: 8000, + } + if err := agent.ServiceRegister(reg1); err != nil { + t.Fatalf("err: %v", err) + } + if err := agent.ServiceRegister(reg2); err != nil { + t.Fatalf("err: %v", err) + } + + services, err := agent.Services() + if err != nil { + t.Fatalf("err: %v", err) + } + + if _, ok := services["foo1"]; !ok { + t.Fatalf("missing service: %v", services) + } + if services["foo1"].EnableTagOverride != true { + t.Fatalf("tag override not set on service foo1: %v", services) + } + if _, ok := services["foo2"]; !ok { + t.Fatalf("missing service: %v", services) + } + if services["foo2"].EnableTagOverride != false { + t.Fatalf("tag override set on service foo2: %v", services) + } +} + func TestAgent_Services_MultipleChecks(t *testing.T) { t.Parallel() c, s := makeClient(t) diff --git a/api/catalog.go b/api/catalog.go index cf64bd909..52a00b304 100644 --- a/api/catalog.go +++ b/api/catalog.go @@ -6,13 +6,14 @@ type Node struct { } type CatalogService struct { - Node string - Address string - ServiceID string - ServiceName string - ServiceAddress string - ServiceTags []string - ServicePort int + Node string + Address string + ServiceID string + ServiceName string + ServiceAddress string + ServiceTags []string + ServicePort int + ServiceEnableTagOverride bool } type CatalogNode struct { diff --git a/api/catalog_test.go b/api/catalog_test.go index bb8be25b0..691c05618 100644 --- a/api/catalog_test.go +++ b/api/catalog_test.go @@ -277,3 +277,94 @@ func TestCatalog_Registration(t *testing.T) { t.Fatalf("err: %s", err) }) } + +func TestCatalog_EnableTagOverride(t *testing.T) { + t.Parallel() + c, s := makeClient(t) + defer s.Stop() + + catalog := c.Catalog() + + service := &AgentService{ + ID: "redis1", + Service: "redis", + Tags: []string{"master", "v1"}, + Port: 8000, + } + + reg := &CatalogRegistration{ + Datacenter: "dc1", + Node: "foobar", + Address: "192.168.10.10", + Service: service, + } + + testutil.WaitForResult(func() (bool, error) { + if _, err := catalog.Register(reg, nil); err != nil { + return false, err + } + + node, _, err := catalog.Node("foobar", nil) + if err != nil { + return false, err + } + + if _, ok := node.Services["redis1"]; !ok { + return false, fmt.Errorf("missing service: redis1") + } + if node.Services["redis1"].EnableTagOverride != false { + return false, fmt.Errorf("tag override set") + } + + services, _, err := catalog.Service("redis", "", nil) + if err != nil { + return false, err + } + + if len(services) < 1 || services[0].ServiceName != "redis" { + return false, fmt.Errorf("missing service: redis") + } + if services[0].ServiceEnableTagOverride != false { + return false, fmt.Errorf("tag override set") + } + + return true, nil + }, func(err error) { + t.Fatalf("err: %s", err) + }) + + service.EnableTagOverride = true + testutil.WaitForResult(func() (bool, error) { + if _, err := catalog.Register(reg, nil); err != nil { + return false, err + } + + node, _, err := catalog.Node("foobar", nil) + if err != nil { + return false, err + } + + if _, ok := node.Services["redis1"]; !ok { + return false, fmt.Errorf("missing service: redis1") + } + if node.Services["redis1"].EnableTagOverride != true { + return false, fmt.Errorf("tag override not set") + } + + services, _, err := catalog.Service("redis", "", nil) + if err != nil { + return false, err + } + + if len(services) < 1 || services[0].ServiceName != "redis" { + return false, fmt.Errorf("missing service: redis") + } + if services[0].ServiceEnableTagOverride != true { + return false, fmt.Errorf("tag override not set") + } + + return true, nil + }, func(err error) { + t.Fatalf("err: %s", err) + }) +}