Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/http check #156

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
bdd5cd5
Add HTTP check type
Oct 4, 2017
94bb8c6
Add register_http to access methods
Oct 4, 2017
37046f6
Fix travis errors where applicable
Oct 6, 2017
aa8bbb7
Updated README and Changelog
Oct 6, 2017
08b339f
Use `PUT` as the verb for deregistering a service. (#158)
blanquer Nov 11, 2017
78c8ab8
Suggest using X-Consul-Token for ACLs
sixfeetover Dec 28, 2017
575c866
Move ApiOptions methods into Restclient
Annih Mar 3, 2018
ec3be3a
Allow to pass ::Diplomat::Configuration object to the Rest Client
Annih Mar 3, 2018
243b9af
Specify StandardError on few rescue statements to make rubocop happy
Annih Mar 3, 2018
d6b2975
Support for tokens when registering/unregistering entities on Agent
pierresouchay Sep 6, 2018
70a71b0
Fixed rubocop warnings + Fixed rubocop version
pierresouchay Sep 6, 2018
0b70f62
Depreciate Ruby < 2.3 in travis tests
pierresouchay Sep 6, 2018
b320048
Merge pull request #166 from criteo-forks/token_on_registration_dereg…
pierresouchay Sep 6, 2018
d753006
Added new unit test for service registration with token
pierresouchay Sep 6, 2018
4f39bfb
New version 2.0.3 with token support for agent registration
pierresouchay Sep 6, 2018
28d41db
Merge pull request #163 from criteo-forks/non_static_configuration
pierresouchay Oct 17, 2018
a2b4180
Fixed minor rubocop style warnings after last PR #163
pierresouchay Oct 31, 2018
71c921b
README: Drop defunct badge, use SVG
olleolleolle Dec 15, 2018
985f4b8
CI: Update to current set of Rubies
olleolleolle Dec 15, 2018
621ae1c
Merge pull request #169 from olleolleolle/patch-1
pierresouchay Dec 19, 2018
9acebcb
Merge pull request #160 from sixfeetover/patch-1
pierresouchay Dec 19, 2018
19d9813
Travis: Exempt 2.6.0-rc2 from matrix
olleolleolle Dec 19, 2018
55b7db7
CI: Drop matrix allow_failures - 2.6.0-rc2 passes
olleolleolle Dec 19, 2018
6e1aa66
Merge pull request #170 from olleolleolle/patch-2
pierresouchay Dec 19, 2018
e327f81
Added automatic publication of GEM on a branch
pierresouchay Feb 25, 2019
13dacc0
Use bundle v2.0+
pierresouchay Feb 25, 2019
acd356e
Do not build with ruby 2.2.x in travis build
pierresouchay Feb 25, 2019
e67773f
Bump version to 2.0.4
pierresouchay Feb 25, 2019
688a6c9
Use correct verbs for Check methods.
pierresouchay Feb 25, 2019
e388ef0
Updated changelog
pierresouchay Feb 25, 2019
1617f67
Added new link to https://rubygems.org/gems/diplomat
pierresouchay Feb 25, 2019
f45b152
Use json_pure if ruby < 1.9.3 to avoid using a compiler
pierresouchay Feb 25, 2019
1fb4560
Updated CHANGELOG.md
pierresouchay Feb 25, 2019
9e90b93
Support for Output in TTL checks
kinnalru Feb 26, 2019
9f332f5
Merge pull request #178 from RnD-Soft/master
pierresouchay Feb 26, 2019
2412de1
Diplomat release 2.0.5 Bugfix release
pierresouchay Feb 26, 2019
95bee47
Updated travis versions of Ruby to use stable 2.6.1
pierresouchay Feb 26, 2019
3229669
simpler convert_to_hash function
Feb 11, 2019
ef57278
Merge pull request #176 from robmbrooks/master
pierresouchay Feb 27, 2019
0f80781
Updated changelog
pierresouchay Feb 27, 2019
35c369f
Enable configuration override on each consul api call
tionebsalocin Feb 27, 2019
ff4e2f8
Merge pull request #179 from tionebsalocin/options-everywhere
pierresouchay Mar 11, 2019
588eaf0
Adding support for KV flags during lock acquisition.
dudemcbacon Mar 6, 2019
830f1fe
Merge pull request #180 from dudemcbacon/master
pierresouchay Mar 13, 2019
0880966
Avoid having a HTTP 302 if key requested starts with '/' (#181)
pierresouchay Mar 15, 2019
f931728
Add some options example in the documentation
tionebsalocin Mar 15, 2019
bf5a26b
Changelog for release 2.1.0
pierresouchay Mar 18, 2019
5297bb9
fix syntax error colon instead of =>
Oribracha Mar 19, 2019
d415447
BUGFIX release 2.1.1, fixes #186 using #185
pierresouchay Mar 19, 2019
8458296
Bump version number to 2.1.1
pierresouchay Mar 19, 2019
fc22b27
Fix raw data usage in kv
tionebsalocin Mar 19, 2019
5f57ca9
BUGFIX release 2.1.2
pierresouchay Mar 19, 2019
34c045f
Add HTTP check type
Oct 4, 2017
6c3fc6b
register_http check name parameter is mandatory
Mar 20, 2019
a8df947
Merge branch 'feature/http-check' of https://github.com/lremes/diplom…
Mar 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
AllCops:
DisplayCopNames: true

Metrics/AbcSize:
Max: 60

Metrics/CyclomaticComplexity:
Max: 15

Metrics/LineLength:
# This will disable the rule completely, regardless what other options you put
Enabled: true
# Change the default 80 chars limit value
Max: 120

Metrics/MethodLength:
# This cop checks if the length of a method exceeds some maximum value
Enabled: true
# Change the default 10 lines limit value
Max: 50

# Allow classes longer than 100 lines of code
ClassLength:
Max: 250

Naming/UncommunicativeMethodParamName:
Enabled: false

Lint/UnneededCopDisableDirective:
Enabled: false
22 changes: 14 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
language: ruby
before_install:
- gem install bundler
before_install:
- gem install bundler
rvm:
- "2.0.0"
- "2.1.0"
- "2.2.1"
- "2.2.2"
- 2.3.8
- 2.4.5
- 2.5.3
- 2.6.1
addons:
code_climate:
repo_token: 0c910d8f8af55cc2d2f38170d5362be4e16be8ce232df9c565057985af264b7b
code_climate:
repo_token: 0c910d8f8af55cc2d2f38170d5362be4e16be8ce232df9c565057985af264b7b
deploy:
provider: rubygems
api_key:
secure: RbB9Va0vmEY3hyCDuvDxj6G/cmgD8ZQ3Wwg2Xx+oi64AWOckmTa4e4nVR6231kkgF63fG2swpkRhyHU+pfRSy+m/Ng8qAK4IIDgce6FTMPbzY45TlzPDq9OKsLyChYtkCnRTjXI16cS9+t1OBAXtwEDgQnXG7VDU4cRi3NMyFCk=
on:
tags: true
51 changes: 51 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
# Changelog

## Next

## 2.1.3

- 2019-03-20 Lars Remes [lars.remes@ramboll.fi][1] Support HTTP service checks

## 2.1.2

Bugfix relase: fix #188

Release 2.1.0 did break a few things. Ensure more compatibility with 2.0.x

## 2.1.1

Bugfix release.

- Fix for #186

## 2.1.0

This release cleanup a lot the existing APIs while preserving ascending compatibility.
It will avoid relying on side effects to configure diplomat and allow to override most
configuration options per API call as implemented in https://github.com/WeAreFarmGeek/diplomat/pull/179.
It is now easy to use one instance of the lib and to perform several calls with different tokens and/or
consistency options for instance.

Full changelog:

- Fix behavior of HTTP 302 on some 2.5 ruby releases (#181 fix #171)
- Set flags attribute on KVPair during lock acquisition and release (#180)
- Now allow to override most parameters per request (#179)
- use dedup for safer/simpler conversion of results to hash #176

## 2.0.5

- Fix incorrect verbs for checks Fix https://github.com/WeAreFarmGeek/diplomat/issues/173
- Use json_pure to avoid the need for installing a compiler. Fix https://github.com/WeAreFarmGeek/diplomat/issues/177
- Allow updating Output with TTL checks https://github.com/WeAreFarmGeek/diplomat/pull/178

## 2.0.4

- automatic GEM publication from Travis when a tag is pushed
- Depreciate old Ruby version 2.2.x
- Bump bundler to version 2.0.x

## 2.0.3

- 2018-09-06 Allow to register/deregister entities using tokens
- 2017-11-09 Josep M. Blanquer (@blanquer) Fix service deregister to use the proper verb (`PUT` instead of `GET`). Consul 1.x seems to
have started enforcing [it](https://www.consul.io/docs/upgrade-specific.html#http-verbs-are-enforced-in-many-http-apis).

## 2.0.2

- 2017-08-23 Trevor Wood [trevor.g.wood@gmail.com][1] Revert the change to single values
Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Diplomat
[![Gem Version](https://badge.fury.io/rb/diplomat.svg)](http://badge.fury.io/rb/diplomat) [![Gem](https://img.shields.io/gem/dt/diplomat.svg)](https://rubygems.org/gems/diplomat/versions/2.0.0) [![Build Status](https://travis-ci.org/WeAreFarmGeek/diplomat.svg?branch=master)](https://travis-ci.org/WeAreFarmGeek/diplomat) [![Code Climate](https://codeclimate.com/github/johnhamelink/diplomat.png)](https://codeclimate.com/github/WeAreFarmGeek/diplomat) [![Dependency Status](https://gemnasium.com/WeAreFarmGeek/diplomat.svg)](https://gemnasium.com/WeAreFarmGeek/diplomat) [![Inline docs](http://inch-ci.org/github/wearefarmgeek/diplomat.svg?branch=master)](http://inch-ci.org/github/wearefarmgeek/diplomat)
[![Gem Version](https://badge.fury.io/rb/diplomat.svg)](https://rubygems.org/gems/diplomat) [![Gem](https://img.shields.io/gem/dt/diplomat.svg)](https://rubygems.org/gems/diplomat/versions/2.0.0) [![Build Status](https://travis-ci.org/WeAreFarmGeek/diplomat.svg?branch=master)](https://travis-ci.org/WeAreFarmGeek/diplomat) [![Code Climate](https://codeclimate.com/github/johnhamelink/diplomat.svg)](https://codeclimate.com/github/WeAreFarmGeek/diplomat) [![Inline docs](http://inch-ci.org/github/wearefarmgeek/diplomat.svg?branch=master)](http://inch-ci.org/github/wearefarmgeek/diplomat)
### A HTTP Ruby API for [Consul](http://www.consul.io/)

![Diplomacy Boad Game](http://i.imgur.com/Nkuy4b7.jpg)
![Diplomacy Board Game](http://i.imgur.com/Nkuy4b7.jpg)


## FAQ
Expand Down Expand Up @@ -72,12 +72,14 @@ foo = Diplomat::Kv.get('foo')
```

Or retrieve a value from another datacenter:

```ruby
foo = Diplomat::Kv.get('foo', :dc => 'dc-west')
# => "baz"
```

You can also retrieve values recursively:

```ruby
Diplomat::Kv.put('foo/a', 'lorem')
Diplomat::Kv.put('foo/b', 'ipsum')
Expand All @@ -94,6 +96,7 @@ Or list all available keys:
Diplomat::Kv.get('/', :keys => true) # => ['foo/a', 'foo/b']
```
You can convert the consul data to a ruby hash

```ruby
Diplomat::Kv.put('foo/a', 'lorem')
Diplomat::Kv.put('foo/b', 'ipsum')
Expand Down Expand Up @@ -180,6 +183,14 @@ services = Diplomat::Service.get_all({ :dc => 'My_Datacenter' })
# => #<OpenStruct consul=[], foo=[], bar=[]>
```

#### Checks

Register a check:
```ruby
headers = { "Authorization" => [ "Basic ZGlwbG9tYXQ6cGFzc3dvcmQ=" ] }
Diplomat::Check.register_http("http://#{addr}:#{port}/health_check", '10s', id: 'health-check-1', name: 'Health check', notes: 'Node level HTTP health check', method: 'GET', headers: {}, timeout: '1s')
```

### Datacenters

Getting a list of datacenters is quite simple and gives you the option to extract all services out of
Expand Down Expand Up @@ -310,15 +321,27 @@ Diplomat.configure do |config|
config.url = "http://localhost:8888"
# Set up a custom Faraday Middleware
config.middleware = MyCustomMiddleware
# Connect into consul with custom access token (ACL)
config.acl_token = "xxxxxxxx-yyyy-zzzz-1111-222222222222"
# Set extra Faraday configuration options
config.options = {ssl: { version: :TLSv1_2 }}
# Set extra Faraday configuration options and custom access token (ACL)
config.options = {ssl: {version: :TLSv1_2}, headers: {"X-Consul-Token" => "xxxxxxxx-yyyy-zzzz-1111-222222222222"}}
end
```

This is traditionally kept inside the `config/initializers` directory if you're using rails. The middleware allows you to customise what happens when faraday sends and receives data. This can be useful if you want to instrument your use of diplomat, for example. You can read more about Faraday's custom middleware [here](http://stackoverflow.com/a/20973008).

Alternatively, configuration settings can be overriden at each method call allowing for instance to address different consul agents, with some other token.

```ruby
Diplomat::Service.get('foo', { http_addr: 'http://consu01:8500' })
Diplomat::Service.get('foo', { http_addr: 'http://consu02:8500' })
Diplomat::Kv.put('key/path', 'value', { http_addr: 'http://localhost:8500', dc: 'dc1', token: '111-222-333-444-555' })
```

Most common options are:
* dc: target datacenter
* token: identity used to perform the corresponding action
* http_addr: to target a remote consul node
* stale: use consistency mode that allows any server to service the read regardless of whether it is the leader

### Todo

- [ ] Updating Docs with latest changes
Expand Down
18 changes: 10 additions & 8 deletions diplomat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ Gem::Specification.new 'diplomat', Diplomat::VERSION do |spec|

spec.files = `git ls-files lib README.md LICENSE features`.split("\n")

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'pry', '~> 0.9'
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'fakes-rspec', '~> 2.1'
spec.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.1'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.0'
spec.add_development_dependency 'cucumber', '~> 2.0'
spec.add_development_dependency 'fakes-rspec', '~> 2.1'
spec.add_development_dependency 'fivemat', '~> 1.3'
spec.add_development_dependency 'gem-release', '~> 0.7'
spec.add_development_dependency 'cucumber', '~> 2.0'
spec.add_development_dependency 'rubocop', '~> 0.47', '>= 0.47.1'
spec.add_development_dependency 'pry', '~> 0.9'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'rubocop', '~> 0.49'
spec.add_development_dependency 'webmock'

spec.add_runtime_dependency 'json' if RUBY_VERSION < '1.9.3'
spec.add_runtime_dependency 'deep_merge', '~> 1.0', '>= 1.0.1'
spec.add_runtime_dependency 'faraday', '~> 0.9'
spec.add_runtime_dependency 'json_pure' if RUBY_VERSION < '1.9.3'
end
8 changes: 4 additions & 4 deletions lib/diplomat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def require_libs(*libs)

raise 'Diplomat only supports ruby >= 2.0.0' unless RUBY_VERSION.to_f >= 2.0

self.root_path = File.expand_path '..', __FILE__
self.lib_path = File.expand_path '../diplomat', __FILE__
self.root_path = File.expand_path __dir__
self.lib_path = File.expand_path 'diplomat', __dir__

require_libs 'configuration', 'rest_client', 'api_options', 'kv', 'datacenter',
'service', 'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
require_libs 'configuration', 'rest_client', 'kv', 'datacenter', 'service',
'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
'error', 'event', 'acl', 'maintenance', 'query', 'agent', 'status'
self.configuration ||= Diplomat::Configuration.new

Expand Down
57 changes: 23 additions & 34 deletions lib/diplomat/acl.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
module Diplomat
# Methods for interacting with the Consul ACL API endpoint
class Acl < Diplomat::RestClient
include ApiOptions

@access_methods = %i[list info create destroy update]
attr_reader :id, :type, :acl

# Get Acl info by ID
# @param id [String] ID of the Acl to get
# @param options [Hash] options parameter hash
# @return [Hash]
# rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
def info(id, options = nil, not_found = :reject, found = :return)
# rubocop:disable PerceivedComplexity
def info(id, options = {}, not_found = :reject, found = :return)
@id = id
@options = options
url = ["/v1/acl/info/#{id}"]
url << check_acl_token
url << use_consistency(options)
custom_params = []
custom_params << use_consistency(options)

raw = send_get_request(@conn_no_err, ["/v1/acl/info/#{id}"], options, custom_params)

raw = @conn_no_err.get concat_url url
if raw.status == 200 && raw.body.chomp != 'null'
case found
when :reject
Expand All @@ -37,55 +36,45 @@ def info(id, options = nil, not_found = :reject, found = :return)
raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}"
end
end
# rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
# rubocop:enable PerceivedComplexity

# List all Acls
# @param options [Hash] options parameter hash
# @return [List] list of [Hash] of Acls
def list
url = ['/v1/acl/list']
url += check_acl_token
@raw = @conn_no_err.get concat_url url
def list(options = {})
@raw = send_get_request(@conn_no_err, ['/v1/acl/list'], options)
parse_body
end

# Update an Acl definition, create if not present
# @param value [Hash] Acl definition, ID field is mandatory
# @param options [Hash] options parameter hash
# @return [Hash] The result Acl
def update(value)
raise Diplomat::IdParameterRequired unless value['ID']
def update(value, options = {})
raise Diplomat::IdParameterRequired unless value['ID'] || value[:ID]

@raw = @conn.put do |req|
url = ['/v1/acl/update']
url += check_acl_token
url += use_cas(@options)
req.url concat_url url
req.body = value.to_json
end
custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/update'], options, value.to_json, custom_params)
parse_body
end

# Create an Acl definition
# @param value [Hash] Acl definition, ID field is mandatory
# @param options [Hash] options parameter hash
# @return [Hash] The result Acl
def create(value)
@raw = @conn.put do |req|
url = ['/v1/acl/create']
url += check_acl_token
url += use_cas(@options)
req.url concat_url url
req.body = value.to_json
end
def create(value, options = {})
custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/create'], options, value.to_json, custom_params)
parse_body
end

# Destroy an ACl token by its id
# @param ID [String] the Acl ID
# @param options [Hash] options parameter hash
# @return [Bool]
def destroy(id)
def destroy(id, options = {})
@id = id
url = ["/v1/acl/destroy/#{@id}"]
url << check_acl_token
@raw = @conn.put concat_url url
@raw = send_put_request(@conn, ["/v1/acl/destroy/#{@id}"], options, nil)
@raw.body.chomp == 'true'
end
end
Expand Down
Loading