Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
added consul wrapper (#39)
Browse files Browse the repository at this point in the history
* added consul wrapper

* rename test function

* tmp. added verbose flag

* - hiding error removed from the constructor
- added interface for update TTL
- added dummy consul client

* removed verbose flag from test runner

* update change log
  • Loading branch information
DayFan authored and avdva committed Feb 11, 2019
1 parent 2ac2f2e commit d17a9fa
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
go:
- 1.11.2

script:
- go test ./...

Expand All @@ -10,8 +10,13 @@ services:
- postgresql
- rabbitmq

env:
- CONSUL_VERSION=1.4.2

before_install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS db_test;'
- curl -sLo consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip
- unzip consul.zip -d $HOME/bin

before_script:
- psql -c 'CREATE DATABASE db_test;' -U postgres
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [2.4.0] - 2019-02-11
### Add
- Consul client

## [2.3.0] - 2019-02-08
### Change
Expand All @@ -24,7 +27,7 @@

## [1.1.1] - 2018-11-23
- add amqp-kit close method
- fix amqp-kit data race serve() method
- fix amqp-kit data race serve() method

## [1.0.0] - 2018-11-13
### Added
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
10. [json_formatter.go](#formatter)
11. [messagebus.go](#messagebus)
12. [amqp-kit](#amqp-kit)
13. [consul](#consul)

<a name="debug" />

Expand Down Expand Up @@ -146,4 +147,11 @@ func main() {

AMQP wrapper in go-kit style

see test example for use: [publisher_test.go](https://github.com/space307/go-utils/blob/master/amqp-kit/publisher_test.go)
see test example for use: [publisher_test.go](https://github.com/space307/go-utils/blob/master/amqp-kit/publisher_test.go)


<a name="consul" />

### 13. consul

Consul package contains a wrapper for Consul API for simplicity registration a service in the local agent.
6 changes: 6 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
app:
opt1: opt1
opt2: 2
app2:
opt1: 2
opt2: opt2
94 changes: 94 additions & 0 deletions consul/consul.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package consul

import (
"github.com/hashicorp/consul/api"
)

// TTLUpdater is an interface to update the TTL of service
type TTLUpdater interface {
UpdateTTL(checkID, status, output string) error
}

// Client provides a client to functions of Consul API
// for one serivce
type Client struct {
clientAPI *api.Client
srvID string
}

// NewDefaultClient is the most default constructor for Consul agent for one serivce.
// It initializes HTTP client for a local agent and register service on it.
// srvName sets for ID, Name and CheckID of service.
func NewDefaultClient(srvName, localIP string, svcPort int, checkTTL string) (*Client, error) {
var err error

client := &Client{srvID: srvName}

client.clientAPI, err = api.NewClient(api.DefaultConfig())
if err != nil {
return nil, err
}

srvRegInfo := &api.AgentServiceRegistration{
ID: srvName,
Name: srvName,
Address: localIP,
Port: svcPort,
Check: &api.AgentServiceCheck{
CheckID: srvName,
TTL: checkTTL,
},
}

if err = client.Register(srvRegInfo); err != nil {
return nil, err
}

return client, nil
}

// Agent returns a handle to the agent endpoints
func (c *Client) Agent() *api.Agent {
return c.clientAPI.Agent()
}

// IsReachable check whether we can reach the agent
func (c *Client) IsReachable() bool {
_, err := c.clientAPI.Agent().Self()

return err == nil
}

// Register is used to register a new service with given ID
func (c *Client) Register(srvInfo *api.AgentServiceRegistration) error {
c.srvID = srvInfo.ID
return c.clientAPI.Agent().ServiceRegister(srvInfo)
}

// Deregister is used to deregister a service
func (c *Client) Deregister() error {
return c.clientAPI.Agent().ServiceDeregister(c.srvID)
}

// PassingTTL is used to update the TTL of a default check
// with status 'passing'
func (c *Client) PassingTTL(output string) error {
return c.UpdateTTL(c.srvID, api.HealthPassing, output)
}

// CriticalTTL is used to update the TTL of a default check
// with status 'critical'
func (c *Client) CriticalTTL(output string) error {
return c.UpdateTTL(c.srvID, api.HealthCritical, output)
}

// WarningTTL is used to update the TTL of a default check
// with status 'warning'
func (c *Client) WarningTTL(output string) error {
return c.UpdateTTL(c.srvID, api.HealthWarning, output)
}

// UpdateTTL is used to update the TTL of a check
func (c *Client) UpdateTTL(checkID, status, output string) error {
return c.clientAPI.Agent().UpdateTTL(checkID, output, status)
}
65 changes: 65 additions & 0 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package consul

import (
"testing"

"github.com/hashicorp/consul/testutil"
"github.com/stretchr/testify/assert"
)

func TestConsulWrapper(t *testing.T) {
srv1, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
c.Ports.HTTP = 8500
})
if err != nil {
t.Fatal(err)
}

serviceName := "testService"
client, err := NewDefaultClient(serviceName, `127.0.0.1`, 8080, "10m")
assert.NoError(t, err)
assert.Implements(t, (*TTLUpdater)(nil), client)

consulAgent := client.Agent()

assert.NoError(t, client.PassingTTL(testutil.HealthPassing))

status, info, err := consulAgent.AgentHealthServiceByID(serviceName)
assert.NoError(t, err)
assert.Equal(t, testutil.HealthPassing, info.Checks[0].Output)
assert.Equal(t, testutil.HealthPassing, status)

assert.NoError(t, client.WarningTTL(testutil.HealthWarning))

status, info, err = consulAgent.AgentHealthServiceByID(serviceName)
assert.NoError(t, err)
assert.Equal(t, testutil.HealthWarning, info.Checks[0].Output)
assert.Equal(t, testutil.HealthWarning, status)

assert.NoError(t, client.CriticalTTL(testutil.HealthCritical))

status, info, err = consulAgent.AgentHealthServiceByID(serviceName)
assert.NoError(t, err)
assert.Equal(t, testutil.HealthCritical, info.Checks[0].Output)
assert.Equal(t, testutil.HealthCritical, status)

assert.NoError(t, client.UpdateTTL(serviceName, testutil.HealthPassing, testutil.HealthPassing))

status, info, err = consulAgent.AgentHealthServiceByID(serviceName)
assert.NoError(t, err)
assert.Equal(t, testutil.HealthPassing, info.Checks[0].Output)
assert.Equal(t, testutil.HealthPassing, status)

assert.NoError(t, client.Deregister())

status, _, _ = consulAgent.AgentHealthServiceByID(serviceName)
assert.Equal(t, testutil.HealthCritical, status)

srv1.Stop()

assert.False(t, client.IsReachable())

client, err = NewDefaultClient(serviceName, `127.0.0.1`, 8080, "10m")
assert.Error(t, err)
assert.Nil(t, client)
}
15 changes: 15 additions & 0 deletions consul/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package consul

// Dummy is struct for implements TTLUpdater interface
type Dummy struct{}

// NewDummyClient return initialized struct Dummy
func NewDummyClient() *Dummy {
return &Dummy{}
}

// UpdateTTL implements TTLUpdater interface.
// Return always nil.
func (c *Dummy) UpdateTTL(checkID, status, output string) error {
return nil
}
14 changes: 14 additions & 0 deletions consul/dummy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package consul

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDummyConsulWrapper(t *testing.T) {
client := NewDummyClient()
assert.Implements(t, (*TTLUpdater)(nil), client)

assert.NoError(t, client.UpdateTTL("", "", ""))
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/Azure/azure-sdk-for-go v25.0.0+incompatible // indirect
github.com/Azure/go-autorest v11.4.0+incompatible // indirect
github.com/SAP/go-hdb v0.13.2 // indirect
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 // indirect
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190203032130-408505a5fba8 // indirect
github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61 // indirect
Expand Down Expand Up @@ -46,15 +47,18 @@ require (
github.com/gorilla/websocket v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/consul v1.4.2 // indirect
github.com/hashicorp/consul v1.4.2
github.com/hashicorp/go-gcp-common v0.0.0-20180425173946-763e39302965 // indirect
github.com/hashicorp/go-hclog v0.0.0-20190109152822-4783caec6f2e // indirect
github.com/hashicorp/go-plugin v0.0.0-20190129155509-362c99b11937 // indirect
github.com/hashicorp/go-retryablehttp v0.5.2 // indirect
github.com/hashicorp/go-rootcerts v1.0.0 // indirect
github.com/hashicorp/go-sockaddr v1.0.1 // indirect
github.com/hashicorp/go-version v1.1.0 // indirect
github.com/hashicorp/hil v0.0.0-20190129155652-59d7c1fee952 // indirect
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69 // indirect
github.com/hashicorp/nomad v0.8.7 // indirect
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea // indirect
github.com/hashicorp/serf v0.8.2 // indirect
github.com/hashicorp/vault v1.0.2
github.com/hashicorp/vault-plugin-auth-alicloud v0.0.0-20181109180636-f278a59ca3e8 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX
github.com/Azure/go-autorest v11.4.0+incompatible h1:z3Yr6KYqs0nhSNwqGXEBpWK977hxVqsLv2n9PVYcixY=
github.com/Azure/go-autorest v11.4.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895 h1:dmc/C8bpE5VkQn65PNbbyACDC8xw8Hpp/NEurdPmQDQ=
github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Jeffail/gabs v1.1.1 h1:V0uzR08Hj22EX8+8QMhyI9sX2hwRu+/RJhJUmnwda/E=
github.com/Jeffail/gabs v1.1.1/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
Expand All @@ -28,6 +29,7 @@ github.com/RoaringBitmap/roaring v0.4.16/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQi
github.com/SAP/go-hdb v0.13.1/go.mod h1:etBT+FAi1t5k3K3tf5vQTnosgYmhDkRi8jEnQqCnxF0=
github.com/SAP/go-hdb v0.13.2 h1:19+oZb2RGKCJpSZjFN/dcdOuSACSWGJlinHwgVyvUnM=
github.com/SAP/go-hdb v0.13.2/go.mod h1:etBT+FAi1t5k3K3tf5vQTnosgYmhDkRi8jEnQqCnxF0=
github.com/SermoDigital/jose v0.9.1/go.mod h1:ARgCUhI1MHQH+ONky/PAtmVHQrP5JlGY0F3poXOp/fA=
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 h1:koK7z0nSsRiRiBWwa+E714Puh+DO+ZRdIyAXiXzL+lg=
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2/go.mod h1:ARgCUhI1MHQH+ONky/PAtmVHQrP5JlGY0F3poXOp/fA=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
Expand Down Expand Up @@ -67,6 +69,7 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCS
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/boombuler/barcode v1.0.0 h1:s1TvRnXwL2xJRaccrdcBQMZxq6X7DvsMogtmJeHDdrc=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
Expand All @@ -86,7 +89,9 @@ github.com/centrify/cloud-golang-sdk v0.0.0-20180119173102-7c97cc6fde16/go.mod h
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chrismalek/oktasdk-go v0.0.0-20181212195951-3430665dfaa0 h1:CWU8piLyqoi9qXEUwzOh5KFKGgmSU5ZhktJyYcq6ryQ=
github.com/chrismalek/oktasdk-go v0.0.0-20181212195951-3430665dfaa0/go.mod h1:5d8DqS60xkj9k3aXfL3+mXBH0DPYO0FQjcKosxl+b/Q=
github.com/circonus-labs/circonus-gometrics v2.2.5+incompatible h1:KsuY3ogbxgVv3FNhbLUoT+SE9znoWEUIuChSIT4HukI=
github.com/circonus-labs/circonus-gometrics v2.2.5+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3 h1:TJH+oke8D16535+jHExHj4nQvzlZrj7ug5D7I/orNUA=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
Expand Down Expand Up @@ -271,15 +276,21 @@ github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCO
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hil v0.0.0-20190129155652-59d7c1fee952 h1:2touDRqIeu/4eKrg7WHcH+WZwpW97r0NKOHDixzroJg=
github.com/hashicorp/hil v0.0.0-20190129155652-59d7c1fee952/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.0/go.mod h1:ncdBp14cuox2iFOq3kDiquKU6fqsTBc3W6JvZwjxxsE=
github.com/hashicorp/memberlist v0.1.3 h1:EmmoJme1matNzb+hMpDuR/0sbJSUisxyqBGG676r31M=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69 h1:lc3c72qGlIMDqQpQH82Y4vaglRMMFdJbziYWriR4UcE=
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69/go.mod h1:/z+jUGRBlwVpUZfjute9jWaF6/HuhjuFQuL1YXzVD1Q=
github.com/hashicorp/nomad v0.8.7 h1:jOrmJdAoWcyhKgoG4OxHQhG5SU6RniXFjfwKg6a492U=
github.com/hashicorp/nomad v0.8.7/go.mod h1:WRaKjdO1G2iqi86TvTjIYtKTyxg4pl7NLr9InxtWaI0=
github.com/hashicorp/raft v1.0.0 h1:htBVktAOtGs4Le5Z7K8SF5H2+oWsQFYVmOgH5loro7Y=
github.com/hashicorp/raft v1.0.0/go.mod h1:DVSAWItjLjTOkVbSpWQ0j0kUADIvDaCtBxIcbNAQLkI=
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea h1:xykPFhrBAS2J0VBzVa5e80b5ZtYuNQtgXjN40qBZlD4=
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea/go.mod h1:pNv7Wc3ycL6F5oOWn+tPGo2gWD4a5X+yp/ntwdKLjRk=
github.com/hashicorp/serf v0.8.1/go.mod h1:h/Ru6tmZazX7WO/GDmwdpS975F019L4t5ng5IgwbNrE=
github.com/hashicorp/serf v0.8.2 h1:YZ7UKsJv+hKjqGVUUbtE3HNj79Eln2oQ75tniF6iPt0=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
Expand Down Expand Up @@ -526,6 +537,7 @@ github.com/testcontainers/testcontainer-go v0.0.0-20181115231424-8e868ca12c0f/go
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/tylerb/graceful v1.2.15/go.mod h1:LPYTbOYmUTdabwRt0TGhLllQ0MUNbs0Y5q1WXJOI9II=
github.com/uber-go/atomic v1.3.2 h1:Azu9lPBWRNKzYXSIwRfgRuDuS0YKsK4NFhiQv98gkxo=
Expand Down

0 comments on commit d17a9fa

Please sign in to comment.