diplomat 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/features/step_definitions/setup_diplomat.rb +8 -6
- data/features/step_definitions/test_key_value.rb +2 -2
- data/lib/diplomat.rb +19 -9
- data/lib/diplomat/acl.rb +31 -33
- data/lib/diplomat/api_options.rb +4 -3
- data/lib/diplomat/check.rb +26 -44
- data/lib/diplomat/configuration.rb +2 -3
- data/lib/diplomat/datacenter.rb +10 -15
- data/lib/diplomat/error.rb +2 -0
- data/lib/diplomat/event.rb +67 -69
- data/lib/diplomat/health.rb +48 -53
- data/lib/diplomat/kv.rb +41 -47
- data/lib/diplomat/lock.rb +14 -12
- data/lib/diplomat/maintenance.rb +17 -20
- data/lib/diplomat/members.rb +4 -6
- data/lib/diplomat/node.rb +20 -28
- data/lib/diplomat/nodes.rb +15 -17
- data/lib/diplomat/query.rb +124 -0
- data/lib/diplomat/rest_client.rb +47 -51
- data/lib/diplomat/service.rb +29 -27
- data/lib/diplomat/session.rb +70 -23
- data/lib/diplomat/version.rb +1 -1
- metadata +25 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6feb74783d35459eb7f08e895688ffe20ed975b1
|
4
|
+
data.tar.gz: 21a8d4d02987f5d6298414c047fa86758968dd58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 709cdba5352b6c9f045065e8d335d4ce00d3b0065d0dbba39b83ed1d0ba8ea873088de4dd7b998fd127682dcd40104d5726daf91e9af40a45c13a2837c8240f9
|
7
|
+
data.tar.gz: 0621ad4ca2152759913a2e82f6daf650e2044bb2083f66b028caa73bac19e38f7cefcf044f1e73d79c4edbf1b1a9d1a2e40d0d82b43be58f51d91064d9d50f45
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'diplomat'
|
2
|
-
|
2
|
+
|
3
|
+
Given 'I am setting up a default diplomat' do
|
3
4
|
end
|
4
5
|
|
5
|
-
Given
|
6
|
-
class StubMiddleware
|
6
|
+
Given 'I am setting up a custom diplomat' do
|
7
|
+
class StubMiddleware # :nodoc:
|
7
8
|
def initialize(app, options = {})
|
8
9
|
@app = app
|
9
10
|
@options = options
|
10
11
|
end
|
12
|
+
|
11
13
|
def call(env)
|
12
14
|
@app.call(env)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
expect
|
18
|
+
expect do
|
17
19
|
Diplomat.configure do |config|
|
18
|
-
config.url =
|
20
|
+
config.url = 'http://localhost:8500'
|
19
21
|
config.middleware = StubMiddleware
|
20
22
|
end
|
21
|
-
|
23
|
+
end.to_not raise_error
|
22
24
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
Then
|
1
|
+
Then 'I should be able to get and put keys' do
|
2
2
|
# High-fructose Corn Syrup
|
3
3
|
Diplomat.put('drink', 'Irn Bru')
|
4
|
-
expect(Diplomat.get('drink')).to eq(
|
4
|
+
expect(Diplomat.get('drink')).to eq('Irn Bru')
|
5
5
|
|
6
6
|
# Sugar
|
7
7
|
Diplomat::Kv.put('cake', 'Sponge')
|
data/lib/diplomat.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
|
1
|
+
require 'json'
|
2
|
+
require 'base64'
|
3
|
+
require 'faraday'
|
2
4
|
|
5
|
+
# Top level namespace ensures all required libraries are included and initializes the gem configration.
|
6
|
+
module Diplomat
|
3
7
|
class << self
|
4
|
-
|
5
8
|
attr_accessor :root_path
|
6
9
|
attr_accessor :lib_path
|
7
10
|
attr_accessor :configuration
|
@@ -20,16 +23,15 @@ module Diplomat
|
|
20
23
|
|
21
24
|
raise 'Diplomat only supports ruby >= 2.0.0' unless RUBY_VERSION.to_f >= 2.0
|
22
25
|
|
23
|
-
self.root_path = File.expand_path
|
24
|
-
self.lib_path = File.expand_path
|
26
|
+
self.root_path = File.expand_path '..', __FILE__
|
27
|
+
self.lib_path = File.expand_path '../diplomat', __FILE__
|
25
28
|
|
26
|
-
require_libs
|
27
|
-
|
28
|
-
|
29
|
+
require_libs 'configuration', 'rest_client', 'api_options', 'kv', 'datacenter',
|
30
|
+
'service', 'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
|
31
|
+
'error', 'event', 'acl', 'maintenance', 'query'
|
29
32
|
self.configuration ||= Diplomat::Configuration.new
|
30
33
|
|
31
34
|
class << self
|
32
|
-
|
33
35
|
# Build optional configuration by yielding a block to configure
|
34
36
|
# @yield [Diplomat::Configuration]
|
35
37
|
def configure
|
@@ -46,7 +48,15 @@ module Diplomat
|
|
46
48
|
# @param &block block to send to Kv
|
47
49
|
# @return [Object]
|
48
50
|
def method_missing(name, *args, &block)
|
49
|
-
Diplomat::Kv.new.send(name, *args, &block)
|
51
|
+
Diplomat::Kv.new.send(name, *args, &block) || super
|
52
|
+
end
|
53
|
+
|
54
|
+
# Make `respond_to_missing?` fall back to super
|
55
|
+
#
|
56
|
+
# @param meth_id [Symbol] the tested method
|
57
|
+
# @oaram with_private if private methods should be tested too
|
58
|
+
def respond_to_missing?(meth_id, with_private = false)
|
59
|
+
access_method?(meth_id) || super
|
50
60
|
end
|
51
61
|
end
|
52
62
|
end
|
data/lib/diplomat/acl.rb
CHANGED
@@ -1,94 +1,92 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'faraday'
|
3
|
-
|
4
1
|
module Diplomat
|
2
|
+
# Methods for interacting with the Consul ACL API endpoint
|
5
3
|
class Acl < Diplomat::RestClient
|
6
|
-
|
7
4
|
include ApiOptions
|
8
5
|
|
9
|
-
@access_methods = [
|
6
|
+
@access_methods = [:list, :info, :create, :destroy, :update]
|
10
7
|
attr_reader :id, :type, :acl
|
11
8
|
|
12
9
|
# Get Acl info by ID
|
13
10
|
# @param id [String] ID of the Acl to get
|
14
11
|
# @return [Hash]
|
15
|
-
|
12
|
+
# rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
|
13
|
+
def info(id, options = nil, not_found = :reject, found = :return)
|
16
14
|
@id = id
|
17
15
|
@options = options
|
18
|
-
|
19
|
-
url
|
20
|
-
url
|
21
|
-
url += use_consistency(@options)
|
16
|
+
url = ["/v1/acl/info/#{id}"]
|
17
|
+
url << check_acl_token
|
18
|
+
url << use_consistency(options)
|
22
19
|
|
23
20
|
raw = @conn_no_err.get concat_url url
|
24
|
-
if raw.status == 200
|
21
|
+
if raw.status == 200 && raw.body != 'null'
|
25
22
|
case found
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
when :reject
|
24
|
+
raise Diplomat::AclAlreadyExists, id
|
25
|
+
when :return
|
26
|
+
@raw = raw
|
27
|
+
return parse_body
|
31
28
|
end
|
32
|
-
elsif raw.status == 200
|
29
|
+
elsif raw.status == 200 && raw.body == 'null'
|
33
30
|
case not_found
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
when :reject
|
32
|
+
raise Diplomat::AclNotFound, id
|
33
|
+
when :return
|
34
|
+
return nil
|
38
35
|
end
|
39
36
|
else
|
40
|
-
raise Diplomat::UnknownStatus, "status #{raw.status}"
|
37
|
+
raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}"
|
41
38
|
end
|
42
39
|
end
|
40
|
+
# rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
|
43
41
|
|
44
42
|
# List all Acls
|
45
43
|
# @return [List] list of [Hash] of Acls
|
46
44
|
def list
|
47
|
-
url = [
|
45
|
+
url = ['/v1/acl/list']
|
48
46
|
url += check_acl_token
|
49
47
|
@raw = @conn_no_err.get concat_url url
|
50
|
-
|
48
|
+
parse_body
|
51
49
|
end
|
52
50
|
|
53
51
|
# Update an Acl definition, create if not present
|
54
52
|
# @param value [Hash] Acl definition, ID field is mandatory
|
55
53
|
# @return [Hash] The result Acl
|
56
|
-
def update
|
54
|
+
def update(value)
|
57
55
|
raise Diplomat::IdParameterRequired unless value['ID']
|
58
56
|
|
59
57
|
@raw = @conn.put do |req|
|
60
|
-
url = [
|
58
|
+
url = ['/v1/acl/update']
|
61
59
|
url += check_acl_token
|
62
60
|
url += use_cas(@options)
|
63
61
|
req.url concat_url url
|
64
62
|
req.body = value.to_json
|
65
63
|
end
|
66
|
-
|
64
|
+
parse_body
|
67
65
|
end
|
68
66
|
|
69
67
|
# Create an Acl definition
|
70
68
|
# @param value [Hash] Acl definition, ID field is mandatory
|
71
69
|
# @return [Hash] The result Acl
|
72
|
-
def create
|
70
|
+
def create(value)
|
73
71
|
@raw = @conn.put do |req|
|
74
|
-
url = [
|
72
|
+
url = ['/v1/acl/create']
|
75
73
|
url += check_acl_token
|
76
74
|
url += use_cas(@options)
|
77
75
|
req.url concat_url url
|
78
76
|
req.body = value.to_json
|
79
77
|
end
|
80
|
-
|
78
|
+
parse_body
|
81
79
|
end
|
82
80
|
|
83
81
|
# Destroy an ACl token by its id
|
84
82
|
# @param ID [String] the Acl ID
|
85
83
|
# @return [Bool]
|
86
|
-
def destroy
|
84
|
+
def destroy(id)
|
87
85
|
@id = id
|
88
86
|
url = ["/v1/acl/destroy/#{@id}"]
|
89
|
-
url
|
87
|
+
url << check_acl_token
|
90
88
|
@raw = @conn.put concat_url url
|
91
|
-
|
89
|
+
@raw.body == 'true'
|
92
90
|
end
|
93
91
|
end
|
94
92
|
end
|
data/lib/diplomat/api_options.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module Diplomat
|
2
|
+
# Helper methods for interacting with the Consul RESTful API
|
2
3
|
module ApiOptions
|
3
4
|
def check_acl_token
|
4
|
-
use_named_parameter(
|
5
|
+
use_named_parameter('token', Diplomat.configuration.acl_token)
|
5
6
|
end
|
6
7
|
|
7
8
|
def use_cas(options)
|
8
|
-
|
9
|
+
options ? use_named_parameter('cas', options[:cas]) : []
|
9
10
|
end
|
10
11
|
|
11
12
|
def use_consistency(options)
|
12
|
-
|
13
|
+
options && options[:consistency] ? [options[:consistency].to_s] : []
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/diplomat/check.rb
CHANGED
@@ -1,17 +1,14 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'faraday'
|
3
|
-
|
4
1
|
module Diplomat
|
2
|
+
# Methods for interacting with the Consul check API endpoint
|
5
3
|
class Check < Diplomat::RestClient
|
6
|
-
|
7
|
-
|
8
|
-
:deregister, :pass, :warn, :fail ]
|
4
|
+
@access_methods = [:checks, :register_script, :register_ttl,
|
5
|
+
:deregister, :pass, :warn, :fail]
|
9
6
|
|
10
7
|
# Get registered checks
|
11
8
|
# @return [OpenStruct] all data associated with the service
|
12
9
|
def checks
|
13
|
-
ret = @conn.get
|
14
|
-
|
10
|
+
ret = @conn.get '/v1/agent/checks'
|
11
|
+
JSON.parse(ret.body)
|
15
12
|
end
|
16
13
|
|
17
14
|
# Register a check
|
@@ -22,23 +19,15 @@ module Diplomat
|
|
22
19
|
# @param interval [String] frequency (with units) of the check execution
|
23
20
|
# @param ttl [String] time (with units) to mark a check down
|
24
21
|
# @return [Integer] Status code
|
25
|
-
|
26
|
-
|
27
|
-
{
|
28
|
-
"ID" => check_id,
|
29
|
-
"Name" => name,
|
30
|
-
"Notes" => notes,
|
31
|
-
"Script" => script,
|
32
|
-
"Interval" => interval
|
33
|
-
}
|
34
|
-
)
|
35
|
-
|
22
|
+
#
|
23
|
+
def register_script(check_id, name, notes, script, interval)
|
36
24
|
ret = @conn.put do |req|
|
37
|
-
req.url
|
38
|
-
req.body =
|
25
|
+
req.url '/v1/agent/check/register'
|
26
|
+
req.body = JSON.generate(
|
27
|
+
'ID' => check_id, 'Name' => name, 'Notes' => notes, 'Script' => script, 'Interval' => interval
|
28
|
+
)
|
39
29
|
end
|
40
|
-
|
41
|
-
return ret.status == 200
|
30
|
+
ret.status == 200
|
42
31
|
end
|
43
32
|
|
44
33
|
# Register a TTL check
|
@@ -47,53 +36,46 @@ module Diplomat
|
|
47
36
|
# @param notes [String] notes about the check
|
48
37
|
# @param ttl [String] time (with units) to mark a check down
|
49
38
|
# @return [Boolean] Success
|
50
|
-
def register_ttl
|
51
|
-
json = JSON.generate({
|
52
|
-
"ID" => check_id,
|
53
|
-
"Name" => name,
|
54
|
-
"Notes" => notes,
|
55
|
-
"TTL" => ttl,
|
56
|
-
})
|
57
|
-
|
39
|
+
def register_ttl(check_id, name, notes, ttl)
|
58
40
|
ret = @conn.put do |req|
|
59
|
-
req.url
|
60
|
-
req.body =
|
41
|
+
req.url '/v1/agent/check/register'
|
42
|
+
req.body = JSON.generate(
|
43
|
+
'ID' => check_id, 'Name' => name, 'Notes' => notes, 'TTL' => ttl
|
44
|
+
)
|
61
45
|
end
|
62
|
-
|
63
|
-
return ret.status == 200
|
46
|
+
ret.status == 200
|
64
47
|
end
|
65
48
|
|
66
49
|
# Deregister a check
|
67
50
|
# @param check_id [String] the unique id of the check
|
68
51
|
# @return [Integer] Status code
|
69
|
-
def deregister
|
52
|
+
def deregister(check_id)
|
70
53
|
ret = @conn.get "/v1/agent/check/deregister/#{check_id}"
|
71
|
-
|
54
|
+
ret.status == 200
|
72
55
|
end
|
73
56
|
|
74
57
|
# Pass a check
|
75
58
|
# @param check_id [String] the unique id of the check
|
76
59
|
# @return [Integer] Status code
|
77
|
-
def pass
|
60
|
+
def pass(check_id)
|
78
61
|
ret = @conn.get "/v1/agent/check/pass/#{check_id}"
|
79
|
-
|
62
|
+
ret.status == 200
|
80
63
|
end
|
81
64
|
|
82
65
|
# Warn a check
|
83
66
|
# @param check_id [String] the unique id of the check
|
84
67
|
# @return [Integer] Status code
|
85
|
-
def warn
|
68
|
+
def warn(check_id)
|
86
69
|
ret = @conn.get "/v1/agent/check/warn/#{check_id}"
|
87
|
-
|
70
|
+
ret.status == 200
|
88
71
|
end
|
89
72
|
|
90
73
|
# Warn a check
|
91
74
|
# @param check_id [String] the unique id of the check
|
92
75
|
# @return [Integer] Status code
|
93
|
-
def fail
|
76
|
+
def fail(check_id)
|
94
77
|
ret = @conn.get "/v1/agent/check/fail/#{check_id}"
|
95
|
-
|
78
|
+
ret.status == 200
|
96
79
|
end
|
97
|
-
|
98
80
|
end
|
99
81
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module Diplomat
|
2
|
+
# Methods for configuring Diplomat
|
2
3
|
class Configuration
|
3
4
|
attr_accessor :middleware
|
4
5
|
attr_accessor :url, :acl_token, :options
|
@@ -7,7 +8,7 @@ module Diplomat
|
|
7
8
|
# @param url [String] consul's connection URL
|
8
9
|
# @param acl_token [String] a connection token used when making requests to consul
|
9
10
|
# @param options [Hash] extra options to configure Faraday::Connection
|
10
|
-
def initialize(url=
|
11
|
+
def initialize(url = 'http://localhost:8500', acl_token = nil, options = {})
|
11
12
|
@middleware = []
|
12
13
|
@url = url
|
13
14
|
@acl_token = acl_token
|
@@ -20,7 +21,5 @@ module Diplomat
|
|
20
21
|
return @middleware = middleware if middleware.is_a? Array
|
21
22
|
@middleware = [middleware]
|
22
23
|
end
|
23
|
-
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
data/lib/diplomat/datacenter.rb
CHANGED
@@ -1,27 +1,22 @@
|
|
1
|
-
require 'base64'
|
2
|
-
require 'faraday'
|
3
|
-
|
4
1
|
module Diplomat
|
2
|
+
# Methods for interacting with the Consul dataceneter API endpoint
|
5
3
|
class Datacenter < Diplomat::RestClient
|
6
|
-
|
7
|
-
@access_methods = [ :get ]
|
4
|
+
@access_methods = [:get]
|
8
5
|
|
9
6
|
# Get an array of all avaliable datacenters accessible by the local consul agent
|
10
7
|
# @param meta [Hash] output structure containing header information about the request (index)
|
11
8
|
# @return [OpenStruct] all datacenters avaliable to this consul agent
|
12
|
-
def get
|
13
|
-
|
14
|
-
url = ["/v1/catalog/datacenters"]
|
9
|
+
def get(meta = nil)
|
10
|
+
url = ['/v1/catalog/datacenters']
|
15
11
|
|
16
12
|
ret = @conn.get concat_url url
|
17
13
|
|
18
|
-
if meta
|
19
|
-
meta[:index] = ret.headers[
|
20
|
-
meta[:knownleader] = ret.headers[
|
21
|
-
meta[:lastcontact] = ret.headers[
|
14
|
+
if meta && ret.headers
|
15
|
+
meta[:index] = ret.headers['x-consul-index']
|
16
|
+
meta[:knownleader] = ret.headers['x-consul-knownleader']
|
17
|
+
meta[:lastcontact] = ret.headers['x-consul-lastcontact']
|
22
18
|
end
|
23
|
-
|
19
|
+
JSON.parse(ret.body)
|
24
20
|
end
|
25
|
-
|
26
21
|
end
|
27
|
-
end
|
22
|
+
end
|
data/lib/diplomat/error.rb
CHANGED
@@ -6,6 +6,8 @@ module Diplomat
|
|
6
6
|
class AclAlreadyExists < StandardError; end
|
7
7
|
class EventNotFound < StandardError; end
|
8
8
|
class EventAlreadyExists < StandardError; end
|
9
|
+
class QueryNotFound < StandardError; end
|
10
|
+
class QueryAlreadyExists < StandardError; end
|
9
11
|
class UnknownStatus < StandardError; end
|
10
12
|
class IdParameterRequired < StandardError; end
|
11
13
|
end
|
data/lib/diplomat/event.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
|
3
1
|
module Diplomat
|
2
|
+
# Methods for interacting with the Consul event API endpoint
|
4
3
|
class Event < Diplomat::RestClient
|
5
|
-
|
6
4
|
include ApiOptions
|
7
|
-
|
8
|
-
@access_methods = [
|
5
|
+
|
6
|
+
@access_methods = [:fire, :get_all, :get]
|
9
7
|
|
10
8
|
# Send an event
|
11
9
|
# @param name [String] the event name
|
@@ -14,15 +12,14 @@ module Diplomat
|
|
14
12
|
# @param node [String] the target node name
|
15
13
|
# @param tag [String] the target tag name, must only be used with service
|
16
14
|
# @return [nil]
|
17
|
-
def fire
|
18
|
-
url = [
|
15
|
+
def fire(name, value = nil, service = nil, node = nil, tag = nil)
|
16
|
+
url = ["/v1/event/fire/#{name}"]
|
19
17
|
url += check_acl_token
|
20
|
-
url += use_named_parameter(
|
21
|
-
url += use_named_parameter(
|
22
|
-
url += use_named_parameter(
|
23
|
-
url = concat_url url
|
18
|
+
url += use_named_parameter('service', service) if service
|
19
|
+
url += use_named_parameter('node', node) if node
|
20
|
+
url += use_named_parameter('tag', tag) if tag
|
24
21
|
|
25
|
-
@conn.put(url, value
|
22
|
+
@conn.put concat_url(url), value
|
26
23
|
nil
|
27
24
|
end
|
28
25
|
|
@@ -56,31 +53,32 @@ module Diplomat
|
|
56
53
|
# - W R - get the first or current value; always return something, but
|
57
54
|
# block only when necessary
|
58
55
|
# - W W - get the first or next value; wait until there is an update
|
59
|
-
|
60
|
-
|
56
|
+
# rubocop:disable MethodLength, AbcSize
|
57
|
+
def get_all(name = nil, not_found = :reject, found = :return)
|
58
|
+
url = ['/v1/event/list']
|
61
59
|
url += check_acl_token
|
62
|
-
url += use_named_parameter(
|
60
|
+
url += use_named_parameter('name', name)
|
63
61
|
url = concat_url url
|
64
62
|
|
65
63
|
# Event list never returns 404 or blocks, but may return an empty list
|
66
64
|
@raw = @conn.get url
|
67
|
-
if JSON.parse(@raw.body).count
|
65
|
+
if JSON.parse(@raw.body).count.zero?
|
68
66
|
case not_found
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
when :reject
|
68
|
+
raise Diplomat::EventNotFound, name
|
69
|
+
when :return
|
70
|
+
return []
|
73
71
|
end
|
74
72
|
else
|
75
73
|
case found
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
74
|
+
when :reject
|
75
|
+
raise Diplomat::EventAlreadyExists, name
|
76
|
+
when :return
|
77
|
+
# Always set the response to 200 so we always return
|
78
|
+
# the response body.
|
79
|
+
@raw.status = 200
|
80
|
+
@raw = parse_body
|
81
|
+
return return_payload
|
84
82
|
end
|
85
83
|
end
|
86
84
|
|
@@ -88,6 +86,7 @@ module Diplomat
|
|
88
86
|
@raw = parse_body
|
89
87
|
return_payload
|
90
88
|
end
|
89
|
+
# rubocop:enable MethodLength, AbcSize
|
91
90
|
|
92
91
|
# Get a specific event in the sequence matching name
|
93
92
|
# @param name [String] the name of the event (regex)
|
@@ -109,67 +108,66 @@ module Diplomat
|
|
109
108
|
# middle, though these can only be identified relative to the preceding
|
110
109
|
# event. However, this is ideal for iterating through the sequence of
|
111
110
|
# events (while being sure that none are missed).
|
112
|
-
|
113
|
-
|
111
|
+
# rubocop:disable MethodLength, CyclomaticComplexity, AbcSize
|
112
|
+
def get(name = nil, token = :last, not_found = :wait, found = :return)
|
113
|
+
url = ['/v1/event/list']
|
114
114
|
url += check_acl_token
|
115
|
-
url += use_named_parameter(
|
116
|
-
|
117
|
-
@raw = @conn.get url
|
115
|
+
url += use_named_parameter('name', name)
|
116
|
+
@raw = @conn.get concat_url url
|
118
117
|
body = JSON.parse(@raw.body)
|
119
118
|
# TODO: deal with unknown symbols, invalid indices (find_index will return nil)
|
120
119
|
idx = case token
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
120
|
+
when :first then 0
|
121
|
+
when :last then body.length - 1
|
122
|
+
when :next then body.length
|
123
|
+
else body.find_index { |e| e['ID'] == token } + 1
|
125
124
|
end
|
126
|
-
if idx == body.length
|
125
|
+
if idx == body.length
|
127
126
|
case not_found
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
127
|
+
when :reject
|
128
|
+
raise Diplomat::EventNotFound, name
|
129
|
+
when :return
|
130
|
+
event_name = ''
|
131
|
+
event_payload = ''
|
132
|
+
event_token = :last
|
133
|
+
when :wait
|
134
|
+
@raw = wait_for_next_event(url)
|
135
|
+
@raw = parse_body
|
136
|
+
# If it's possible for two events to arrive at once,
|
137
|
+
# this needs to #find again:
|
138
|
+
event = @raw.last
|
139
|
+
event_name = event['Name']
|
140
|
+
event_payload = Base64.decode64(event['Payload'])
|
141
|
+
event_token = event['ID']
|
143
142
|
end
|
144
143
|
else
|
145
144
|
case found
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
145
|
+
when :reject
|
146
|
+
raise Diplomat::EventAlreadyExits, name
|
147
|
+
when :return
|
148
|
+
event = body[idx]
|
149
|
+
event_name = event['Name']
|
150
|
+
event_payload = Base64.decode64(event['Payload'])
|
151
|
+
event_token = event['ID']
|
153
152
|
end
|
154
153
|
end
|
155
154
|
|
156
155
|
{
|
157
|
-
:
|
158
|
-
:
|
156
|
+
value: { name: event_name, payload: event_payload },
|
157
|
+
token: event_token
|
159
158
|
}
|
160
159
|
end
|
161
|
-
|
160
|
+
# rubocop:enable MethodLength, CyclomaticComplexity, AbcSize
|
162
161
|
|
163
162
|
private
|
164
163
|
|
165
|
-
def wait_for_next_event
|
166
|
-
index = @raw.headers[
|
167
|
-
url = [url, use_named_parameter(
|
168
|
-
|
164
|
+
def wait_for_next_event(url)
|
165
|
+
index = @raw.headers['x-consul-index']
|
166
|
+
url = [url, use_named_parameter('index', index)].join('&')
|
167
|
+
@conn.get do |req|
|
169
168
|
req.url concat_url url
|
170
|
-
req.options.timeout =
|
169
|
+
req.options.timeout = 86_400
|
171
170
|
end
|
172
171
|
end
|
173
|
-
|
174
172
|
end
|
175
173
|
end
|