diplomat 2.0.5 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -1
- data/lib/diplomat/acl.rb +23 -32
- data/lib/diplomat/agent.rb +12 -40
- data/lib/diplomat/check.rb +46 -31
- data/lib/diplomat/datacenter.rb +6 -7
- data/lib/diplomat/error.rb +1 -0
- data/lib/diplomat/event.rb +28 -37
- data/lib/diplomat/health.rb +23 -43
- data/lib/diplomat/kv.rb +45 -53
- data/lib/diplomat/lock.rb +18 -24
- data/lib/diplomat/maintenance.rb +7 -11
- data/lib/diplomat/members.rb +3 -2
- data/lib/diplomat/node.rb +12 -23
- data/lib/diplomat/nodes.rb +6 -9
- data/lib/diplomat/query.rb +26 -61
- data/lib/diplomat/rest_client.rb +80 -44
- data/lib/diplomat/service.rb +37 -59
- data/lib/diplomat/session.rb +25 -49
- data/lib/diplomat/status.rb +6 -6
- data/lib/diplomat/version.rb +1 -1
- metadata +37 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2638dd7e205e74662fe2dd9124a268447b21173a7fa383f124cea572a7fbf0c0
|
4
|
+
data.tar.gz: d2630310fb56f3b8fe9ae2a4cf4b71b1773481078d6349c8bd2fd5bd189cbe3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb12a73c20d249c32f87c3a36b59e90e691a71192f534c29ec127f82042e92d4f697b314924d33b4a8d6104892a5e68063ec2d450e4fc3153d3ab8be74110bac
|
7
|
+
data.tar.gz: 742b7f771257d00c95c037869e9e23458d3c81dbaecefa1dfc848722566082ac90c0770ed87a4563bd19f638291bbe24e5cd69e6b676bce3d2be248e62d5d5dc
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
[![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)
|
3
3
|
### A HTTP Ruby API for [Consul](http://www.consul.io/)
|
4
4
|
|
5
|
-
![Diplomacy
|
5
|
+
![Diplomacy Board Game](http://i.imgur.com/Nkuy4b7.jpg)
|
6
6
|
|
7
7
|
|
8
8
|
## FAQ
|
@@ -72,12 +72,14 @@ foo = Diplomat::Kv.get('foo')
|
|
72
72
|
```
|
73
73
|
|
74
74
|
Or retrieve a value from another datacenter:
|
75
|
+
|
75
76
|
```ruby
|
76
77
|
foo = Diplomat::Kv.get('foo', :dc => 'dc-west')
|
77
78
|
# => "baz"
|
78
79
|
```
|
79
80
|
|
80
81
|
You can also retrieve values recursively:
|
82
|
+
|
81
83
|
```ruby
|
82
84
|
Diplomat::Kv.put('foo/a', 'lorem')
|
83
85
|
Diplomat::Kv.put('foo/b', 'ipsum')
|
@@ -94,6 +96,7 @@ Or list all available keys:
|
|
94
96
|
Diplomat::Kv.get('/', :keys => true) # => ['foo/a', 'foo/b']
|
95
97
|
```
|
96
98
|
You can convert the consul data to a ruby hash
|
99
|
+
|
97
100
|
```ruby
|
98
101
|
Diplomat::Kv.put('foo/a', 'lorem')
|
99
102
|
Diplomat::Kv.put('foo/b', 'ipsum')
|
@@ -317,6 +320,20 @@ end
|
|
317
320
|
|
318
321
|
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).
|
319
322
|
|
323
|
+
Alternatively, configuration settings can be overriden at each method call allowing for instance to address different consul agents, with some other token.
|
324
|
+
|
325
|
+
```ruby
|
326
|
+
Diplomat::Service.get('foo', { http_addr: 'http://consu01:8500' })
|
327
|
+
Diplomat::Service.get('foo', { http_addr: 'http://consu02:8500' })
|
328
|
+
Diplomat::Kv.put('key/path', 'value', { http_addr: 'http://localhost:8500', dc: 'dc1', token: '111-222-333-444-555' })
|
329
|
+
```
|
330
|
+
|
331
|
+
Most common options are:
|
332
|
+
* dc: target datacenter
|
333
|
+
* token: identity used to perform the corresponding action
|
334
|
+
* http_addr: to target a remote consul node
|
335
|
+
* stale: use consistency mode that allows any server to service the read regardless of whether it is the leader
|
336
|
+
|
320
337
|
### Todo
|
321
338
|
|
322
339
|
- [ ] Updating Docs with latest changes
|
data/lib/diplomat/acl.rb
CHANGED
@@ -6,16 +6,17 @@ module Diplomat
|
|
6
6
|
|
7
7
|
# Get Acl info by ID
|
8
8
|
# @param id [String] ID of the Acl to get
|
9
|
+
# @param options [Hash] options parameter hash
|
9
10
|
# @return [Hash]
|
10
|
-
# rubocop:disable PerceivedComplexity
|
11
|
-
def info(id, options =
|
11
|
+
# rubocop:disable PerceivedComplexity
|
12
|
+
def info(id, options = {}, not_found = :reject, found = :return)
|
12
13
|
@id = id
|
13
14
|
@options = options
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
custom_params = []
|
16
|
+
custom_params << use_consistency(options)
|
17
|
+
|
18
|
+
raw = send_get_request(@conn_no_err, ["/v1/acl/info/#{id}"], options, custom_params)
|
17
19
|
|
18
|
-
raw = @conn_no_err.get concat_url url
|
19
20
|
if raw.status == 200 && raw.body.chomp != 'null'
|
20
21
|
case found
|
21
22
|
when :reject
|
@@ -35,55 +36,45 @@ module Diplomat
|
|
35
36
|
raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}"
|
36
37
|
end
|
37
38
|
end
|
38
|
-
# rubocop:enable PerceivedComplexity
|
39
|
+
# rubocop:enable PerceivedComplexity
|
39
40
|
|
40
41
|
# List all Acls
|
42
|
+
# @param options [Hash] options parameter hash
|
41
43
|
# @return [List] list of [Hash] of Acls
|
42
|
-
def list
|
43
|
-
|
44
|
-
url += check_acl_token
|
45
|
-
@raw = @conn_no_err.get concat_url url
|
44
|
+
def list(options = {})
|
45
|
+
@raw = send_get_request(@conn_no_err, ['/v1/acl/list'], options)
|
46
46
|
parse_body
|
47
47
|
end
|
48
48
|
|
49
49
|
# Update an Acl definition, create if not present
|
50
50
|
# @param value [Hash] Acl definition, ID field is mandatory
|
51
|
+
# @param options [Hash] options parameter hash
|
51
52
|
# @return [Hash] The result Acl
|
52
|
-
def update(value)
|
53
|
-
raise Diplomat::IdParameterRequired unless value['ID']
|
53
|
+
def update(value, options = {})
|
54
|
+
raise Diplomat::IdParameterRequired unless value['ID'] || value[:ID]
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
url += check_acl_token
|
58
|
-
url += use_cas(@options)
|
59
|
-
req.url concat_url url
|
60
|
-
req.body = value.to_json
|
61
|
-
end
|
56
|
+
custom_params = use_cas(@options)
|
57
|
+
@raw = send_put_request(@conn, ['/v1/acl/update'], options, value, custom_params)
|
62
58
|
parse_body
|
63
59
|
end
|
64
60
|
|
65
61
|
# Create an Acl definition
|
66
62
|
# @param value [Hash] Acl definition, ID field is mandatory
|
63
|
+
# @param options [Hash] options parameter hash
|
67
64
|
# @return [Hash] The result Acl
|
68
|
-
def create(value)
|
69
|
-
|
70
|
-
|
71
|
-
url += check_acl_token
|
72
|
-
url += use_cas(@options)
|
73
|
-
req.url concat_url url
|
74
|
-
req.body = value.to_json
|
75
|
-
end
|
65
|
+
def create(value, options = {})
|
66
|
+
custom_params = use_cas(@options)
|
67
|
+
@raw = send_put_request(@conn, ['/v1/acl/create'], options, value, custom_params)
|
76
68
|
parse_body
|
77
69
|
end
|
78
70
|
|
79
71
|
# Destroy an ACl token by its id
|
80
72
|
# @param ID [String] the Acl ID
|
73
|
+
# @param options [Hash] options parameter hash
|
81
74
|
# @return [Bool]
|
82
|
-
def destroy(id)
|
75
|
+
def destroy(id, options = {})
|
83
76
|
@id = id
|
84
|
-
|
85
|
-
url << check_acl_token
|
86
|
-
@raw = @conn.put concat_url url
|
77
|
+
@raw = send_put_request(@conn, ["/v1/acl/destroy/#{@id}"], options, nil)
|
87
78
|
@raw.body.chomp == 'true'
|
88
79
|
end
|
89
80
|
end
|
data/lib/diplomat/agent.rb
CHANGED
@@ -8,62 +8,34 @@ module Diplomat
|
|
8
8
|
@access_methods = %i[self checks services members]
|
9
9
|
|
10
10
|
# Get agent configuration
|
11
|
+
# @param options [Hash] options parameter hash
|
11
12
|
# @return [OpenStruct] all data associated with the node
|
12
|
-
def self
|
13
|
-
|
14
|
-
|
15
|
-
# If the request fails, it's probably due to a bad path
|
16
|
-
# so return a PathNotFound error.
|
17
|
-
begin
|
18
|
-
ret = @conn.get concat_url url
|
19
|
-
rescue Faraday::ClientError
|
20
|
-
raise Diplomat::PathNotFound
|
21
|
-
end
|
13
|
+
def self(options = {})
|
14
|
+
ret = send_get_request(@conn, ['/v1/agent/self'], options)
|
22
15
|
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
|
23
16
|
end
|
24
17
|
|
25
18
|
# Get local agent checks
|
19
|
+
# @param options [Hash] options parameter hash
|
26
20
|
# @return [OpenStruct] all agent checks
|
27
|
-
def checks
|
28
|
-
|
29
|
-
|
30
|
-
# If the request fails, it's probably due to a bad path
|
31
|
-
# so return a PathNotFound error.
|
32
|
-
begin
|
33
|
-
ret = @conn.get concat_url url
|
34
|
-
rescue Faraday::ClientError
|
35
|
-
raise Diplomat::PathNotFound
|
36
|
-
end
|
21
|
+
def checks(options = {})
|
22
|
+
ret = send_get_request(@conn, ['/v1/agent/checks'], options)
|
37
23
|
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
|
38
24
|
end
|
39
25
|
|
40
26
|
# Get local agent services
|
27
|
+
# @param options [Hash] options parameter hash
|
41
28
|
# @return [OpenStruct] all agent services
|
42
|
-
def services
|
43
|
-
|
44
|
-
|
45
|
-
# If the request fails, it's probably due to a bad path
|
46
|
-
# so return a PathNotFound error.
|
47
|
-
begin
|
48
|
-
ret = @conn.get concat_url url
|
49
|
-
rescue Faraday::ClientError
|
50
|
-
raise Diplomat::PathNotFound
|
51
|
-
end
|
29
|
+
def services(options = {})
|
30
|
+
ret = send_get_request(@conn, ['/v1/agent/services'], options)
|
52
31
|
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
|
53
32
|
end
|
54
33
|
|
55
34
|
# Get cluster members (as seen by the agent)
|
35
|
+
# @param options [Hash] options parameter hash
|
56
36
|
# @return [OpenStruct] all members
|
57
|
-
def members
|
58
|
-
|
59
|
-
|
60
|
-
# If the request fails, it's probably due to a bad path
|
61
|
-
# so return a PathNotFound error.
|
62
|
-
begin
|
63
|
-
ret = @conn.get concat_url url
|
64
|
-
rescue Faraday::ClientError
|
65
|
-
raise Diplomat::PathNotFound
|
66
|
-
end
|
37
|
+
def members(options = {})
|
38
|
+
ret = send_get_request(@conn, ['/v1/agent/members'], options)
|
67
39
|
JSON.parse(ret.body).map { |node| OpenStruct.new node }
|
68
40
|
end
|
69
41
|
end
|
data/lib/diplomat/check.rb
CHANGED
@@ -6,8 +6,8 @@ module Diplomat
|
|
6
6
|
|
7
7
|
# Get registered checks
|
8
8
|
# @return [OpenStruct] all data associated with the service
|
9
|
-
def checks
|
10
|
-
ret = @conn
|
9
|
+
def checks(options = {})
|
10
|
+
ret = send_get_request(@conn, ['/v1/agent/checks'], options)
|
11
11
|
JSON.parse(ret.body)
|
12
12
|
end
|
13
13
|
|
@@ -15,42 +15,52 @@ module Diplomat
|
|
15
15
|
# @param check_id [String] the unique id of the check
|
16
16
|
# @param name [String] the name
|
17
17
|
# @param notes [String] notes about the check
|
18
|
-
# @param
|
18
|
+
# @param args [String[]] command to be run for check
|
19
19
|
# @param interval [String] frequency (with units) of the check execution
|
20
|
-
# @param
|
20
|
+
# @param options [Hash] options parameter hash
|
21
21
|
# @return [Integer] Status code
|
22
|
-
#
|
23
|
-
def register_script(check_id, name, notes,
|
24
|
-
|
25
|
-
|
26
|
-
req.body = JSON.generate(
|
27
|
-
'ID' => check_id, 'Name' => name, 'Notes' => notes, 'Script' => script, 'Interval' => interval
|
28
|
-
)
|
22
|
+
# rubocop:disable ParameterLists
|
23
|
+
def register_script(check_id, name, notes, args, interval, options = {})
|
24
|
+
unless args.is_a?(Array)
|
25
|
+
raise(Diplomat::DeprecatedArgument, 'Script usage is deprecated, replace by an array of args')
|
29
26
|
end
|
27
|
+
|
28
|
+
definition = {
|
29
|
+
'ID' => check_id,
|
30
|
+
'Name' => name,
|
31
|
+
'Notes' => notes,
|
32
|
+
'Args' => args,
|
33
|
+
'Interval' => interval
|
34
|
+
}
|
35
|
+
ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
|
30
36
|
ret.status == 200
|
31
37
|
end
|
38
|
+
# rubocop:enable ParameterLists
|
32
39
|
|
33
40
|
# Register a TTL check
|
34
41
|
# @param check_id [String] the unique id of the check
|
35
42
|
# @param name [String] the name
|
36
43
|
# @param notes [String] notes about the check
|
37
44
|
# @param ttl [String] time (with units) to mark a check down
|
45
|
+
# @param options [Hash] options parameter hash
|
38
46
|
# @return [Boolean] Success
|
39
|
-
def register_ttl(check_id, name, notes, ttl)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
def register_ttl(check_id, name, notes, ttl, options = {})
|
48
|
+
definition = {
|
49
|
+
'ID' => check_id,
|
50
|
+
'Name' => name,
|
51
|
+
'Notes' => notes,
|
52
|
+
'TTL' => ttl
|
53
|
+
}
|
54
|
+
ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
|
46
55
|
ret.status == 200
|
47
56
|
end
|
48
57
|
|
49
58
|
# Deregister a check
|
50
59
|
# @param check_id [String] the unique id of the check
|
60
|
+
# @param options [Hash] options parameter hash
|
51
61
|
# @return [Integer] Status code
|
52
|
-
def deregister(check_id)
|
53
|
-
ret = @conn
|
62
|
+
def deregister(check_id, options = {})
|
63
|
+
ret = send_put_request(@conn, ["/v1/agent/check/deregister/#{check_id}"], options, nil)
|
54
64
|
ret.status == 200
|
55
65
|
end
|
56
66
|
|
@@ -58,37 +68,42 @@ module Diplomat
|
|
58
68
|
# @param check_id [String] the unique id of the check
|
59
69
|
# @param status [String] status of the check. Valid values are "passing", "warning", and "critical"
|
60
70
|
# @param output [String] human-readable message will be passed through to the check's Output field
|
71
|
+
# @param options [Hash] options parameter hash
|
61
72
|
# @return [Integer] Status code
|
62
|
-
def update_ttl(check_id, status, output = nil)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
73
|
+
def update_ttl(check_id, status, output = nil, options = {})
|
74
|
+
definition = {
|
75
|
+
'Status' => status,
|
76
|
+
'Output' => output
|
77
|
+
}
|
78
|
+
ret = send_put_request(@conn, ["/v1/agent/check/update/#{check_id}"], options, definition)
|
67
79
|
ret.status == 200
|
68
80
|
end
|
69
81
|
|
70
82
|
# Pass a check
|
71
83
|
# @param check_id [String] the unique id of the check
|
72
84
|
# @param output [String] human-readable message will be passed through to the check's Output field
|
85
|
+
# @param options [Hash] options parameter hash
|
73
86
|
# @return [Integer] Status code
|
74
|
-
def pass(check_id, output = nil)
|
75
|
-
update_ttl(check_id, 'passing', output)
|
87
|
+
def pass(check_id, output = nil, options = {})
|
88
|
+
update_ttl(check_id, 'passing', output, options)
|
76
89
|
end
|
77
90
|
|
78
91
|
# Warn a check
|
79
92
|
# @param check_id [String] the unique id of the check
|
80
93
|
# @param output [String] human-readable message will be passed through to the check's Output field
|
94
|
+
# @param options [Hash] options parameter hash
|
81
95
|
# @return [Integer] Status code
|
82
|
-
def warn(check_id, output = nil)
|
83
|
-
update_ttl(check_id, 'warning', output)
|
96
|
+
def warn(check_id, output = nil, options = {})
|
97
|
+
update_ttl(check_id, 'warning', output, options)
|
84
98
|
end
|
85
99
|
|
86
100
|
# Fail a check
|
87
101
|
# @param check_id [String] the unique id of the check
|
88
102
|
# @param output [String] human-readable message will be passed through to the check's Output field
|
103
|
+
# @param options [Hash] options parameter hash
|
89
104
|
# @return [Integer] Status code
|
90
|
-
def fail(check_id, output = nil)
|
91
|
-
update_ttl(check_id, 'critical', output)
|
105
|
+
def fail(check_id, output = nil, options = {})
|
106
|
+
update_ttl(check_id, 'critical', output, options)
|
92
107
|
end
|
93
108
|
end
|
94
109
|
end
|
data/lib/diplomat/datacenter.rb
CHANGED
@@ -5,16 +5,15 @@ module Diplomat
|
|
5
5
|
|
6
6
|
# Get an array of all avaliable datacenters accessible by the local consul agent
|
7
7
|
# @param meta [Hash] output structure containing header information about the request (index)
|
8
|
+
# @param options [Hash] options parameter hash
|
8
9
|
# @return [OpenStruct] all datacenters avaliable to this consul agent
|
9
|
-
def get(meta = nil)
|
10
|
-
|
11
|
-
|
12
|
-
ret = @conn.get concat_url url
|
10
|
+
def get(meta = nil, options = {})
|
11
|
+
ret = send_get_request(@conn, ['/v1/catalog/datacenters'], options)
|
13
12
|
|
14
13
|
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']
|
14
|
+
meta[:index] = ret.headers['x-consul-index'] if ret.headers['x-consul-index']
|
15
|
+
meta[:knownleader] = ret.headers['x-consul-knownleader'] if ret.headers['x-consul-knownleader']
|
16
|
+
meta[:lastcontact] = ret.headers['x-consul-lastcontact'] if ret.headers['x-consul-lastcontact']
|
18
17
|
end
|
19
18
|
JSON.parse(ret.body)
|
20
19
|
end
|
data/lib/diplomat/error.rb
CHANGED
data/lib/diplomat/event.rb
CHANGED
@@ -10,17 +10,17 @@ module Diplomat
|
|
10
10
|
# @param node [String] the target node name
|
11
11
|
# @param tag [String] the target tag name, must only be used with service
|
12
12
|
# @param dc [String] the dc to target
|
13
|
+
# @param options [Hash] options parameter hash
|
13
14
|
# @return [nil]
|
14
15
|
# rubocop:disable Metrics/ParameterLists
|
15
|
-
def fire(name, value = nil, service = nil, node = nil, tag = nil, dc = nil)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
url += use_named_parameter('dc', dc) if dc
|
16
|
+
def fire(name, value = nil, service = nil, node = nil, tag = nil, dc = nil, options = {})
|
17
|
+
custom_params = []
|
18
|
+
custom_params << use_named_parameter('service', service) if service
|
19
|
+
custom_params << use_named_parameter('node', node) if node
|
20
|
+
custom_params << use_named_parameter('tag', tag) if tag
|
21
|
+
custom_params << use_named_parameter('dc', dc) if dc
|
22
22
|
|
23
|
-
@conn
|
23
|
+
send_put_request(@conn, ["/v1/event/fire/#{name}"], options, value, custom_params)
|
24
24
|
nil
|
25
25
|
end
|
26
26
|
# rubocop:enable Metrics/ParameterLists
|
@@ -32,6 +32,7 @@ module Diplomat
|
|
32
32
|
# @param found [Symbol] behaviour if there are already events matching name;
|
33
33
|
# :reject with exception, :return its current value, or :wait for its next value
|
34
34
|
# @return [Array[hash]] The list of { :name, :payload } hashes
|
35
|
+
# @param options [Hash] options parameter hash
|
35
36
|
# @note
|
36
37
|
# Events are sent via the gossip protocol; there is no guarantee of delivery
|
37
38
|
# success or order, but the local agent will store up to 256 events that do
|
@@ -55,15 +56,9 @@ module Diplomat
|
|
55
56
|
# - W R - get the first or current value; always return something, but
|
56
57
|
# block only when necessary
|
57
58
|
# - W W - get the first or next value; wait until there is an update
|
58
|
-
|
59
|
-
def get_all(name = nil, not_found = :reject, found = :return)
|
60
|
-
url = ['/v1/event/list']
|
61
|
-
url += check_acl_token
|
62
|
-
url += use_named_parameter('name', name)
|
63
|
-
url = concat_url url
|
64
|
-
|
59
|
+
def get_all(name = nil, not_found = :reject, found = :return, options = {})
|
65
60
|
# Event list never returns 404 or blocks, but may return an empty list
|
66
|
-
@raw = @conn
|
61
|
+
@raw = send_get_request(@conn, ['/v1/event/list'], options, use_named_parameter('name', name))
|
67
62
|
if JSON.parse(@raw.body).count.zero?
|
68
63
|
case not_found
|
69
64
|
when :reject
|
@@ -76,19 +71,15 @@ module Diplomat
|
|
76
71
|
when :reject
|
77
72
|
raise Diplomat::EventAlreadyExists, name
|
78
73
|
when :return
|
79
|
-
# Always set the response to 200 so we always return
|
80
|
-
# the response body.
|
81
|
-
@raw.status = 200
|
82
74
|
@raw = parse_body
|
83
75
|
return return_payload
|
84
76
|
end
|
85
77
|
end
|
86
78
|
|
87
|
-
@raw = wait_for_next_event(
|
79
|
+
@raw = wait_for_next_event(['/v1/event/list'], options, use_named_parameter('name', name))
|
88
80
|
@raw = parse_body
|
89
81
|
return_payload
|
90
82
|
end
|
91
|
-
# rubocop:enable MethodLength, AbcSize
|
92
83
|
|
93
84
|
# Get a specific event in the sequence matching name
|
94
85
|
# @param name [String] the name of the event (regex)
|
@@ -102,6 +93,7 @@ module Diplomat
|
|
102
93
|
# @return [hash] A hash with keys :value and :token;
|
103
94
|
# :value is a further hash of the :name and :payload of the event,
|
104
95
|
# :token is the event's ordinate in the sequence and can be passed to future calls to get the subsequent event
|
96
|
+
# @param options [Hash] options parameter hash
|
105
97
|
# @note
|
106
98
|
# Whereas the consul API for events returns all past events that match
|
107
99
|
# name, this method allows retrieval of individual events from that
|
@@ -110,12 +102,9 @@ module Diplomat
|
|
110
102
|
# middle, though these can only be identified relative to the preceding
|
111
103
|
# event. However, this is ideal for iterating through the sequence of
|
112
104
|
# events (while being sure that none are missed).
|
113
|
-
# rubocop:disable
|
114
|
-
def get(name = nil, token = :last, not_found = :wait, found = :return)
|
115
|
-
|
116
|
-
url += check_acl_token
|
117
|
-
url += use_named_parameter('name', name)
|
118
|
-
@raw = @conn.get concat_url url
|
105
|
+
# rubocop:disable PerceivedComplexity
|
106
|
+
def get(name = nil, token = :last, not_found = :wait, found = :return, options = {})
|
107
|
+
@raw = send_get_request(@conn, ['/v1/event/list'], options, use_named_parameter('name', name))
|
119
108
|
body = JSON.parse(@raw.body)
|
120
109
|
# TODO: deal with unknown symbols, invalid indices (find_index will return nil)
|
121
110
|
idx = case token
|
@@ -124,7 +113,7 @@ module Diplomat
|
|
124
113
|
when :next then body.length
|
125
114
|
else body.find_index { |e| e['ID'] == token } + 1
|
126
115
|
end
|
127
|
-
if idx == body.length
|
116
|
+
if JSON.parse(@raw.body).count.zero? || idx == body.length
|
128
117
|
case not_found
|
129
118
|
when :reject
|
130
119
|
raise Diplomat::EventNotFound, name
|
@@ -133,7 +122,7 @@ module Diplomat
|
|
133
122
|
event_payload = ''
|
134
123
|
event_token = :last
|
135
124
|
when :wait
|
136
|
-
@raw = wait_for_next_event(
|
125
|
+
@raw = wait_for_next_event(['/v1/event/list'], options, use_named_parameter('name', name))
|
137
126
|
@raw = parse_body
|
138
127
|
# If it's possible for two events to arrive at once,
|
139
128
|
# this needs to #find again:
|
@@ -149,7 +138,7 @@ module Diplomat
|
|
149
138
|
when :return
|
150
139
|
event = body[idx]
|
151
140
|
event_name = event['Name']
|
152
|
-
event_payload = Base64.decode64(event['Payload'])
|
141
|
+
event_payload = event['Payload'].nil? ? nil : Base64.decode64(event['Payload'])
|
153
142
|
event_token = event['ID']
|
154
143
|
end
|
155
144
|
end
|
@@ -159,17 +148,19 @@ module Diplomat
|
|
159
148
|
token: event_token
|
160
149
|
}
|
161
150
|
end
|
162
|
-
# rubocop:enable
|
151
|
+
# rubocop:enable PerceivedComplexity
|
163
152
|
|
164
153
|
private
|
165
154
|
|
166
|
-
def wait_for_next_event(url)
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
req.options.timeout = 86_400
|
155
|
+
def wait_for_next_event(url, options = {}, param = nil)
|
156
|
+
if options.nil?
|
157
|
+
options = { timeout: 86_400 }
|
158
|
+
else
|
159
|
+
options[:timeout] = 86_400
|
172
160
|
end
|
161
|
+
index = @raw.headers['x-consul-index']
|
162
|
+
param += use_named_parameter('index', index)
|
163
|
+
send_get_request(@conn, url, options, param)
|
173
164
|
end
|
174
165
|
end
|
175
166
|
end
|