Repository: allegro/marathon-consul
Pull request #278 (2017-10-31 13:00:29)

Agents interface unification
Change `GetAnyAgent` to return `Agent` instead of `Client` to make API consistent.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13968495/badge)](https://coveralls.io/builds/13968495)

Changes Unknown when pulling **db00b5a473a720b1ad327b6161bbd6096e9a9620 on 276-agents-api-unified** into ** on master**.


Patch:
diff --git a/consul/agents.go b/consul/agents.go
index 4a1e6fa..1eae67d 100644
--- a/consul/agents.go
+++ b/consul/agents.go
@@ -10,11 +10,10 @@ import (
 	log "github.com/Sirupsen/logrus"
 	"github.com/allegro/marathon-consul/metrics"
 	"github.com/allegro/marathon-consul/utils"
-	consulapi "github.com/hashicorp/consul/api"
 )
 
 type Agents interface {
-	GetAgent(agentAddress string) (agent *consulapi.Client, err error)
+	GetAgent(agentAddress string) (agent *Agent, err error)
 	GetAnyAgent() (agent *Agent, err error)
 	RemoveAgent(agentAddress string)
 }
@@ -49,8 +48,10 @@ func NewAgents(config *Config) *ConcurrentAgents {
 				"Cannot connect with consul agent. Check if configuration is valid.")
 		}
 
+		client := agent.Client
+
 		// Get all agents from current DC and store them in cache
-		nodes, _, err := agent.Catalog().Nodes(nil)
+		nodes, _, err := client.Catalog().Nodes(nil)
 		if err != nil {
 			log.WithError(err).WithField("agent", config.LocalAgentHost).Warn(
 				"Cannot obtain agents from local consul agent.")
@@ -101,7 +102,7 @@ func (a *ConcurrentAgents) RemoveAgent(agentAddress string) {
 	}
 }
 
-func (a *ConcurrentAgents) GetAgent(agentAddress string) (*consulapi.Client, error) {
+func (a *ConcurrentAgents) GetAgent(agentAddress string) (*Agent, error) {
 	a.lock.Lock()
 	defer a.lock.Unlock()
 
@@ -112,7 +113,7 @@ func (a *ConcurrentAgents) GetAgent(agentAddress string) (*consulapi.Client, err
 	ipAddress := IP.String()
 
 	if agent, ok := a.agents[ipAddress]; ok {
-		return agent.Client, nil
+		return agent, nil
 	}
 
 	newAgent, err := a.createAgent(ipAddress)
@@ -121,7 +122,7 @@ func (a *ConcurrentAgents) GetAgent(agentAddress string) (*consulapi.Client, err
 	}
 	a.addAgent(ipAddress, newAgent)
 
-	return newAgent.Client, nil
+	return newAgent, nil
 }
 
 func (a *ConcurrentAgents) addAgent(agentHost string, agent *Agent) {
diff --git a/consul/consul.go b/consul/consul.go
index c4b2410..3d32e67 100644
--- a/consul/consul.go
+++ b/consul/consul.go
@@ -181,6 +181,9 @@ func (c *Consul) register(service *consulapi.AgentServiceRegistration) error {
 	if err != nil {
 		return err
 	}
+
+	client := agent.Client
+
 	fields := log.Fields{
 		"Name":              service.Name,
 		"Id":                service.ID,
@@ -191,7 +194,7 @@ func (c *Consul) register(service *consulapi.AgentServiceRegistration) error {
 	}
 	log.WithFields(fields).Info("Registering")
 
-	err = agent.Agent().ServiceRegister(service)
+	err = client.Agent().ServiceRegister(service)
 	if err != nil {
 		log.WithError(err).WithFields(fields).Error("Unable to register")
 	}
@@ -266,9 +269,11 @@ func (c *Consul) deregister(toDeregister *service.Service) error {
 		return err
 	}
 
+	client := agent.Client
+
 	log.WithField("Id", toDeregister.ID).WithField("Address", toDeregister.RegisteringAgentAddress).Info("Deregistering")
 
-	err = agent.Agent().ServiceDeregister(toDeregister.ID.String())
+	err = client.Agent().ServiceDeregister(toDeregister.ID.String())
 	if err != nil {
 		log.WithError(err).WithField("Id", toDeregister.ID).WithField("Address", toDeregister.RegisteringAgentAddress).Error("Unable to deregister")
 	}


Test patch:
diff --git a/consul/agents_test.go b/consul/agents_test.go
index 30925d6..44d4013 100644
--- a/consul/agents_test.go
+++ b/consul/agents_test.go
@@ -3,7 +3,6 @@ package consul
 import (
 	"testing"
 
-	consulapi "github.com/hashicorp/consul/api"
 	"github.com/stretchr/testify/assert"
 )
 
@@ -58,7 +57,7 @@ func TestGetAnyAgent(t *testing.T) {
 
 	// then
 	assert.NoError(t, err)
-	assert.Contains(t, []*consulapi.Client{agent1, agent2, agent3}, anyAgent.Client)
+	assert.Contains(t, []*Agent{agent1, agent2, agent3}, anyAgent)
 }
 
 func TestGetAgent_ShouldResolveAddressToIP(t *testing.T) {
@@ -124,7 +123,7 @@ func TestRemoveAgent(t *testing.T) {
 	// then
 	for i := 0; i < 10; i++ {
 		agent, err := agents.GetAnyAgent()
-		assert.Equal(t, agent.Client, agent2)
+		assert.Equal(t, agent, agent2)
 		assert.Equal(t, "127.0.0.2", agent.IP)
 		assert.NoError(t, err)
 	}