Skip to content

Commit

Permalink
Merge pull request #1726 from hashicorp/f-tag-override-api
Browse files Browse the repository at this point in the history
Adds support for EnableTagOverride to the API client.
  • Loading branch information
slackpad committed Feb 18, 2016
2 parents d31d50c + afdeb2f commit aed82bf
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 19 deletions.
26 changes: 14 additions & 12 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions api/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions api/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
91 changes: 91 additions & 0 deletions api/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit aed82bf

Please sign in to comment.