diplomat 2.3.3 → 2.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -2
- data/lib/diplomat.rb +1 -1
- data/lib/diplomat/acl.rb +1 -1
- data/lib/diplomat/autopilot.rb +30 -0
- data/lib/diplomat/configuration.rb +11 -1
- data/lib/diplomat/health.rb +3 -1
- data/lib/diplomat/rest_client.rb +17 -4
- data/lib/diplomat/service.rb +1 -15
- data/lib/diplomat/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e64194c0344267aaf676dd5d7b6c38eeefa47bb212abf933d6d6194e3fb5381a
|
4
|
+
data.tar.gz: 5ea9866b9a9f1a0e62a3bad1f15f1187868ca7c9c6961489748b8f4e454512ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78e17bb1d532d06e8314641f08e721677181f4b59e18b72299b8c031c2e0c58a118da1d03bdacb6a0125fa3759a78a9a7c8c1846e00a32d86f4389a4fab817de
|
7
|
+
data.tar.gz: ca9f3e479ba0351b1c17e5c81a2c3e2bbbb623ce24c5e34560fd04f5a963e48e015a979bd188f062e307d553f8ea025bd70fc0a09d738587c238e17fee049b37
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Diplomat
|
2
|
-
[![
|
2
|
+
[![Build Status](https://github.com/WeAreFarmGeek/diplomat/workflows/Ruby/badge.svg?branch=master)](https://github.com/WeAreFarmGeek/diplomat/actions?query=branch%3Amaster)
|
3
|
+
[![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) [![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)
|
3
4
|
### A HTTP Ruby API for [Consul](http://www.consul.io/)
|
4
5
|
|
5
6
|
![Diplomacy Board Game](http://i.imgur.com/Nkuy4b7.jpg)
|
@@ -41,7 +42,7 @@ production:
|
|
41
42
|
|
42
43
|
### Which versions of Ruby does Diplomat support? Where did my ruby 1.9 compatibility go?
|
43
44
|
|
44
|
-
Check out [
|
45
|
+
Check out [GitHub Actions](https://github.com/WeAreFarmGeek/diplomat/blob/master/.github/workflows/ruby.yml) to see which versions of ruby we currently test when we're making builds.
|
45
46
|
|
46
47
|
We've dropped ruby 1.9 support. You can still depend on Diplomat by directly using the `ruby-1.9-compatible` branch on github, although be advised it's not actively maintained anymore.
|
47
48
|
|
@@ -314,6 +315,22 @@ Get an array of Raft peers for the datacenter in which the agent is running
|
|
314
315
|
Diplomat::Status.peers()
|
315
316
|
```
|
316
317
|
|
318
|
+
### Autopilot
|
319
|
+
|
320
|
+
Returns information about the autopilot configuration of the Consul cluster
|
321
|
+
|
322
|
+
Get the current autopilot configuration
|
323
|
+
|
324
|
+
```ruby
|
325
|
+
Diplomat::Autopilot.get_configuration()
|
326
|
+
```
|
327
|
+
|
328
|
+
Get the health status from autopilot
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
Diplomat::Autopilot.get_health()
|
332
|
+
```
|
333
|
+
|
317
334
|
### Maintenance mode
|
318
335
|
|
319
336
|
Enable maintenance mode on a host, with optional reason and DC (requires access to local agent)
|
data/lib/diplomat.rb
CHANGED
@@ -31,7 +31,7 @@ module Diplomat
|
|
31
31
|
require_libs 'configuration', 'rest_client', 'kv', 'datacenter', 'service',
|
32
32
|
'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
|
33
33
|
'error', 'event', 'acl', 'maintenance', 'query', 'agent', 'status',
|
34
|
-
'policy', 'token', 'role'
|
34
|
+
'policy', 'token', 'role', 'autopilot'
|
35
35
|
self.configuration ||= Diplomat::Configuration.new
|
36
36
|
|
37
37
|
class << self
|
data/lib/diplomat/acl.rb
CHANGED
@@ -73,7 +73,7 @@ module Diplomat
|
|
73
73
|
# Destroy an ACl token by its id
|
74
74
|
# @param ID [String] the Acl ID
|
75
75
|
# @param options [Hash] options parameter hash
|
76
|
-
# @return [Bool]
|
76
|
+
# @return [Bool] true if operation succeeded
|
77
77
|
def destroy(id, options = {})
|
78
78
|
@id = id
|
79
79
|
@raw = send_put_request(@conn, ["/v1/acl/destroy/#{@id}"], options, nil)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Diplomat
|
4
|
+
# Methods for interacting with the Consul operator autopilot API endpoint
|
5
|
+
class Autopilot < Diplomat::RestClient
|
6
|
+
@access_methods = %i[get_configuration get_health update]
|
7
|
+
|
8
|
+
# Get autopilot configuration
|
9
|
+
# @param options [Hash] options parameter hash
|
10
|
+
# @return [OpenStruct] all data associated with the autopilot configuration
|
11
|
+
def get_configuration(options = {})
|
12
|
+
custom_params = []
|
13
|
+
custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
|
14
|
+
|
15
|
+
ret = send_get_request(@conn, ['/v1/operator/autopilot/configuration'], options, custom_params)
|
16
|
+
JSON.parse(ret.body)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Get health status from the autopilot
|
20
|
+
# @param options [Hash] options parameter hash
|
21
|
+
# @return [OpenStruct] all data associated with the health of the autopilot
|
22
|
+
def get_health(options = {})
|
23
|
+
custom_params = []
|
24
|
+
custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
|
25
|
+
|
26
|
+
ret = send_get_request(@conn, ['/v1/operator/autopilot/health'], options, custom_params)
|
27
|
+
JSON.parse(ret.body)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -6,11 +6,20 @@ module Diplomat
|
|
6
6
|
attr_reader :middleware
|
7
7
|
attr_accessor :url, :acl_token, :options
|
8
8
|
|
9
|
+
# Get the most appropriate consul agent value from env
|
10
|
+
# Parse the environment variable `CONSUL_HTTP_ADDR` and prefixes it with http:// if needed
|
11
|
+
# Return default http://localhost:8500 if not found
|
12
|
+
def self.parse_consul_addr
|
13
|
+
ret = ENV['CONSUL_HTTP_ADDR'] || 'http://localhost:8500'
|
14
|
+
ret = "http://#{ret}" unless ret.start_with?('http://', 'https://')
|
15
|
+
ret
|
16
|
+
end
|
17
|
+
|
9
18
|
# Override defaults for configuration
|
10
19
|
# @param url [String] consul's connection URL
|
11
20
|
# @param acl_token [String] a connection token used when making requests to consul
|
12
21
|
# @param options [Hash] extra options to configure Faraday::Connection
|
13
|
-
def initialize(url =
|
22
|
+
def initialize(url = Configuration.parse_consul_addr, acl_token = ENV['CONSUL_HTTP_TOKEN'], options = {})
|
14
23
|
@middleware = []
|
15
24
|
@url = url
|
16
25
|
@acl_token = acl_token
|
@@ -19,6 +28,7 @@ module Diplomat
|
|
19
28
|
|
20
29
|
# Define a middleware for Faraday
|
21
30
|
# @param middleware [Class] Faraday Middleware class
|
31
|
+
# @return [Array] Array of Faraday Middlewares
|
22
32
|
def middleware=(middleware)
|
23
33
|
if middleware.is_a? Array
|
24
34
|
@middleware = middleware
|
data/lib/diplomat/health.rb
CHANGED
@@ -39,12 +39,14 @@ module Diplomat
|
|
39
39
|
custom_params = []
|
40
40
|
custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
|
41
41
|
custom_params << ['passing'] if options[:passing]
|
42
|
-
custom_params
|
42
|
+
custom_params += [*options[:tag]].map { |value| use_named_parameter('tag', value) } if options[:tag]
|
43
43
|
custom_params << use_named_parameter('near', options[:near]) if options[:near]
|
44
|
+
custom_params << use_named_parameter('node-meta', options[:node_meta]) if options[:node_meta]
|
44
45
|
|
45
46
|
ret = send_get_request(@conn, ["/v1/health/service/#{s}"], options, custom_params)
|
46
47
|
JSON.parse(ret.body).map { |service| OpenStruct.new service }
|
47
48
|
end
|
49
|
+
|
48
50
|
# rubocop:enable Metrics/PerceivedComplexity
|
49
51
|
|
50
52
|
# Get service health
|
data/lib/diplomat/rest_client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'deep_merge'
|
3
|
+
require 'deep_merge/core'
|
4
4
|
|
5
5
|
module Diplomat
|
6
6
|
# Base class for interacting with the Consul RESTful API
|
@@ -140,15 +140,26 @@ module Diplomat
|
|
140
140
|
faraday.request :url_encoded
|
141
141
|
faraday.response :raise_error unless raise_error
|
142
142
|
|
143
|
-
|
143
|
+
# We have to provide a custom params encoder here because Faraday - by default - assumes that
|
144
|
+
# list keys have [] as part of their name. This is however does not match the expectation of
|
145
|
+
# the Consul API, which assumes the same query param to simply be repeated
|
146
|
+
#
|
147
|
+
# So faraday reduces this: http://localhost:8500?a=1&a=2 to http://localhost:8500?a=2 unless you
|
148
|
+
# explicitly tell it not to.
|
149
|
+
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
150
|
+
|
151
|
+
faraday.adapter Faraday.default_adapter
|
144
152
|
end
|
145
153
|
end
|
146
154
|
|
147
155
|
# Converts k/v data into ruby hash
|
148
156
|
def convert_to_hash(data)
|
149
|
-
data.map do |item|
|
157
|
+
data_h = data.map do |item|
|
150
158
|
item[:key].split('/').reverse.reduce(item[:value]) { |h, v| { v => h } }
|
151
|
-
end
|
159
|
+
end
|
160
|
+
data_h.reduce({}) do |dest, source|
|
161
|
+
DeepMerge.deep_merge!(source, dest, { preserve_unmergeables: true })
|
162
|
+
end
|
152
163
|
end
|
153
164
|
|
154
165
|
# Parse the body, apply it to the raw attribute
|
@@ -226,6 +237,8 @@ module Diplomat
|
|
226
237
|
consistency = 'consistent' if options[:consistent]
|
227
238
|
query_params << consistency
|
228
239
|
|
240
|
+
query_params << 'cached' if options[:cached]
|
241
|
+
|
229
242
|
# Parse url host
|
230
243
|
url_prefix = options[:http_addr] if options[:http_addr]
|
231
244
|
{ query_params: query_params, headers: headers, url_prefix: url_prefix }
|
data/lib/diplomat/service.rb
CHANGED
@@ -17,21 +17,7 @@ module Diplomat
|
|
17
17
|
custom_params << use_named_parameter('wait', options[:wait]) if options[:wait]
|
18
18
|
custom_params << use_named_parameter('index', options[:index]) if options[:index]
|
19
19
|
custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
|
20
|
-
if options[:tag]
|
21
|
-
# tag can be either a String, or an array of strings
|
22
|
-
# by splatting it is guaranteed to be an array of strings
|
23
|
-
[*options[:tag]].each do |value|
|
24
|
-
custom_params << use_named_parameter('tag', value)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# We have to provide a custom params encoder here because Faraday - by default - assumes that
|
29
|
-
# list keys have [] as part of their name. This is however not the case for consul tags, which
|
30
|
-
# just use repeated occurences of the same key.
|
31
|
-
#
|
32
|
-
# So faraday reduces this: http://localhost:8500?a=1&a=2 to http://localhost:8500?a=2 unless you
|
33
|
-
# explicitly tell it not to.
|
34
|
-
options[:params_encoder] = Faraday::FlatParamsEncoder
|
20
|
+
custom_params += [*options[:tag]].map { |value| use_named_parameter('tag', value) } if options[:tag]
|
35
21
|
|
36
22
|
ret = send_get_request(@conn, ["/v1/catalog/service/#{key}"], options, custom_params)
|
37
23
|
if meta && ret.headers
|
data/lib/diplomat/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diplomat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hamelink
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-01-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -201,7 +201,7 @@ dependencies:
|
|
201
201
|
version: '0.9'
|
202
202
|
- - "<"
|
203
203
|
- !ruby/object:Gem::Version
|
204
|
-
version:
|
204
|
+
version: 1.1.0
|
205
205
|
type: :runtime
|
206
206
|
prerelease: false
|
207
207
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -211,7 +211,7 @@ dependencies:
|
|
211
211
|
version: '0.9'
|
212
212
|
- - "<"
|
213
213
|
- !ruby/object:Gem::Version
|
214
|
-
version:
|
214
|
+
version: 1.1.0
|
215
215
|
description: Diplomat is a simple wrapper for Consul
|
216
216
|
email:
|
217
217
|
- john@johnhamelink.com
|
@@ -229,6 +229,7 @@ files:
|
|
229
229
|
- lib/diplomat.rb
|
230
230
|
- lib/diplomat/acl.rb
|
231
231
|
- lib/diplomat/agent.rb
|
232
|
+
- lib/diplomat/autopilot.rb
|
232
233
|
- lib/diplomat/check.rb
|
233
234
|
- lib/diplomat/configuration.rb
|
234
235
|
- lib/diplomat/datacenter.rb
|
@@ -269,8 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
269
270
|
- !ruby/object:Gem::Version
|
270
271
|
version: '0'
|
271
272
|
requirements: []
|
272
|
-
|
273
|
-
rubygems_version: 2.7.7
|
273
|
+
rubygems_version: 3.1.4
|
274
274
|
signing_key:
|
275
275
|
specification_version: 4
|
276
276
|
summary: Diplomat is a simple wrapper for Consul
|