diplomat 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,75 +8,55 @@ module Diplomat
8
8
  # @param n [String] the node
9
9
  # @param options [Hash] :dc string for dc specific query
10
10
  # @return [OpenStruct] all data associated with the node
11
- def node(n, options = nil)
12
- url = ["/v1/health/node/#{n}"]
13
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
11
+ def node(n, options = {})
12
+ custom_params = []
13
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
14
14
 
15
- # If the request fails, it's probably due to a bad path
16
- # so return a PathNotFound error.
17
- ret = @conn.get concat_url url
15
+ ret = send_get_request(@conn, ["/v1/health/node/#{n}"], options, custom_params)
18
16
  JSON.parse(ret.body).map { |node| OpenStruct.new node }
19
- rescue Faraday::ClientError
20
- raise Diplomat::PathNotFound
21
17
  end
22
18
 
23
19
  # Get service checks
24
20
  # @param s [String] the service
25
21
  # @param options [Hash] :dc string for dc specific query
26
22
  # @return [OpenStruct] all data associated with the node
27
- def checks(s, options = nil)
28
- url = ["/v1/health/checks/#{s}"]
29
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
23
+ def checks(s, options = {})
24
+ custom_params = []
25
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
30
26
 
31
- # If the request fails, it's probably due to a bad path
32
- # so return a PathNotFound error.
33
- ret = @conn.get concat_url url
27
+ ret = send_get_request(@conn, ["/v1/health/checks/#{s}"], options, custom_params)
34
28
  JSON.parse(ret.body).map { |check| OpenStruct.new check }
35
- rescue Faraday::ClientError
36
- raise Diplomat::PathNotFound
37
29
  end
38
30
 
39
31
  # Get service health
40
32
  # @param s [String] the service
41
- # @param options [Hash] :dc string for dc specific query
42
- # @param options [Hash] :passing boolean to return only checks in passing state
43
- # @param options [Hash] :tag string for specific tag
33
+ # @param options [Hash] options parameter hash
44
34
  # @return [OpenStruct] all data associated with the node
45
- # rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize
46
- def service(s, options = nil)
47
- url = ["/v1/health/service/#{s}"]
48
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
49
- url << 'passing' if options && options[:passing]
50
- url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
51
- url << use_named_parameter('near', options[:near]) if options && options[:near]
35
+ # rubocop:disable PerceivedComplexity
36
+ def service(s, options = {})
37
+ custom_params = []
38
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
39
+ custom_params << ['passing'] if options[:passing]
40
+ custom_params << use_named_parameter('tag', options[:tag]) if options[:tag]
41
+ custom_params << use_named_parameter('near', options[:near]) if options[:near]
52
42
 
53
- # If the request fails, it's probably due to a bad path
54
- # so return a PathNotFound error.
55
- ret = @conn.get concat_url url
43
+ ret = send_get_request(@conn, ["/v1/health/service/#{s}"], options, custom_params)
56
44
  JSON.parse(ret.body).map { |service| OpenStruct.new service }
57
- rescue Faraday::ClientError
58
- raise Diplomat::PathNotFound
59
45
  end
60
- # rubocop:enable PerceivedComplexity, CyclomaticComplexity, AbcSize
46
+ # rubocop:enable PerceivedComplexity
61
47
 
62
48
  # Get service health
63
49
  # @param s [String] the state ("any", "passing", "warning", or "critical")
64
50
  # @param options [Hash] :dc string for dc specific query
65
51
  # @return [OpenStruct] all data associated with the node
66
- # rubocop:disable AbcSize
67
- def state(s, options = nil)
68
- url = ["/v1/health/state/#{s}"]
69
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
70
- url << use_named_parameter('near', options[:near]) if options && options[:near]
52
+ def state(s, options = {})
53
+ custom_params = []
54
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
55
+ custom_params << use_named_parameter('near', options[:near]) if options[:near]
71
56
 
72
- # If the request fails, it's probably due to a bad path
73
- # so return a PathNotFound error.
74
- ret = @conn.get concat_url url
57
+ ret = send_get_request(@conn, ["/v1/health/state/#{s}"], options, custom_params)
75
58
  JSON.parse(ret.body).map { |status| OpenStruct.new status }
76
- rescue Faraday::ClientError
77
- raise Diplomat::PathNotFound
78
59
  end
79
- # rubocop:enable AbcSize
80
60
 
81
61
  # Convenience method to get services in any state
82
62
  def any
@@ -40,24 +40,26 @@ module Diplomat
40
40
  # - W R - get the first or current value; always return something, but
41
41
  # block only when necessary
42
42
  # - W W - get the first or next value; wait until there is an update
43
- # rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize, LineLength
44
- def get(key, options = nil, not_found = :reject, found = :return)
45
- @key = key
43
+ # rubocop:disable PerceivedComplexity, MethodLength, LineLength, CyclomaticComplexity
44
+ def get(key, options = {}, not_found = :reject, found = :return)
45
+ key_subst = if key.start_with? '/'
46
+ key[1..-1]
47
+ else
48
+ key.freeze
49
+ end
50
+ @key = key_subst
46
51
  @options = options
47
-
48
- url = ["/v1/kv/#{@key}"]
49
- url += recurse_get(@options)
50
- url += check_acl_token
51
- url += use_consistency(@options)
52
- url += dc(@options)
53
- url += keys(@options)
54
- url += separator(@options)
52
+ custom_params = []
53
+ custom_params << recurse_get(@options)
54
+ custom_params << use_consistency(options)
55
+ custom_params << dc(@options)
56
+ custom_params << keys(@options)
57
+ custom_params << separator(@options)
55
58
 
56
59
  return_nil_values = @options && @options[:nil_values]
57
60
  transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil
58
61
 
59
- # 404s OK using this connection
60
- raw = @conn_no_err.get concat_url url
62
+ raw = send_get_request(@conn_no_err, ["/v1/kv/#{@key}"], options, custom_params)
61
63
  if raw.status == 404
62
64
  case not_found
63
65
  when :reject
@@ -88,15 +90,17 @@ module Diplomat
88
90
  end
89
91
 
90
92
  # Wait for first/next value
91
- url += use_named_parameter('index', index)
92
- @raw = @conn.get do |req|
93
- req.url concat_url url
94
- req.options.timeout = 86_400
93
+ custom_params << use_named_parameter('index', index)
94
+ if options.nil?
95
+ options = { timeout: 86_400 }
96
+ else
97
+ options[:timeout] = 86_400
95
98
  end
99
+ @raw = send_get_request(@conn, ["/v1/kv/#{@key}"], options, custom_params)
96
100
  @raw = parse_body
97
101
  return_value(return_nil_values, transformation)
98
102
  end
99
- # rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize, LineLength
103
+ # rubocop:enable PerceivedComplexity, LineLength, MethodLength, CyclomaticComplexity
100
104
 
101
105
  # Associate a value with a key
102
106
  # @param key [String] the key
@@ -106,25 +110,19 @@ module Diplomat
106
110
  # @option options [String] :dc Target datacenter
107
111
  # @option options [String] :acquire Session to attach to key
108
112
  # @return [Bool] Success or failure of the write (can fail in c-a-s mode)
109
- # rubocop:disable MethodLength, AbcSize
110
- def put(key, value, options = nil)
113
+ def put(key, value, options = {})
111
114
  @options = options
112
- @raw = @conn.put do |req|
113
- url = ["/v1/kv/#{key}"]
114
- url += check_acl_token
115
- url += use_cas(@options)
116
- url += dc(@options)
117
- url += acquire(@options)
118
- req.url concat_url url
119
- req.body = value
120
- end
115
+ custom_params = []
116
+ custom_params << use_cas(@options)
117
+ custom_params << dc(@options)
118
+ custom_params << acquire(@options)
119
+ @raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, value, custom_params)
121
120
  if @raw.body.chomp == 'true'
122
121
  @key = key
123
122
  @value = value
124
123
  end
125
124
  @raw.body.chomp == 'true'
126
125
  end
127
- # rubocop:enable MethodLength, AbcSize
128
126
 
129
127
  # Delete a value by its key
130
128
  # @param key [String] the key
@@ -132,14 +130,13 @@ module Diplomat
132
130
  # @option options [String] :dc Target datacenter
133
131
  # @option options [Boolean] :recurse If to make recursive get or not
134
132
  # @return [OpenStruct]
135
- def delete(key, options = nil)
133
+ def delete(key, options = {})
136
134
  @key = key
137
135
  @options = options
138
- url = ["/v1/kv/#{@key}"]
139
- url += recurse_get(@options)
140
- url += check_acl_token
141
- url += dc(@options)
142
- @raw = @conn.delete concat_url url
136
+ custom_params = []
137
+ custom_params << recurse_get(@options)
138
+ custom_params << dc(@options)
139
+ @raw = send_delete_request(@conn, ["/v1/kv/#{@key}"], options, custom_params)
143
140
  end
144
141
 
145
142
  # Perform a key/value store transaction.
@@ -163,42 +160,37 @@ module Diplomat
163
160
  # @option options [String] :consistency the accepted staleness level of the transaction.
164
161
  # Can be 'stale' or 'consistent'
165
162
  # @return [OpenStruct] result of the transaction
166
- def txn(value, options = nil)
163
+ def txn(value, options = {})
167
164
  # Verify the given value for the transaction
168
165
  transaction_verification(value)
169
166
  # Will return 409 if transaction was rolled back
170
- raw = @conn_no_err.put do |req|
171
- url = ['/v1/txn']
172
- url += check_acl_token
173
- url += dc(options)
174
- url += transaction_consistency(options)
175
-
176
- req.url concat_url url
177
- req.body = JSON.generate(value)
178
- end
167
+ custom_params = []
168
+ custom_params << dc(options)
169
+ custom_params << transaction_consistency(options)
170
+ raw = send_put_request(@conn_no_err, ['/v1/txn'], options, value, custom_params)
179
171
  transaction_return JSON.parse(raw.body), options
180
172
  end
181
173
 
182
174
  private
183
175
 
184
176
  def recurse_get(options)
185
- options && options[:recurse] ? ['recurse'] : []
177
+ options[:recurse] ? ['recurse'] : []
186
178
  end
187
179
 
188
180
  def dc(options)
189
- options && options[:dc] ? use_named_parameter('dc', options[:dc]) : []
181
+ options[:dc] ? use_named_parameter('dc', options[:dc]) : []
190
182
  end
191
183
 
192
184
  def acquire(options)
193
- options && options[:acquire] ? use_named_parameter('acquire', options[:acquire]) : []
185
+ options[:acquire] ? use_named_parameter('acquire', options[:acquire]) : []
194
186
  end
195
187
 
196
188
  def keys(options)
197
- options && options[:keys] ? ['keys'] : []
189
+ options[:keys] ? ['keys'] : []
198
190
  end
199
191
 
200
192
  def separator(options)
201
- options && options[:separator] ? use_named_parameter('separator', options[:separator]) : []
193
+ options[:separator] ? use_named_parameter('separator', options[:separator]) : []
202
194
  end
203
195
 
204
196
  def transaction_consistency(options)
@@ -247,11 +239,11 @@ module Diplomat
247
239
 
248
240
  def transaction_return(raw_return, options)
249
241
  decoded_return =
250
- options && options[:decode_values] == false ? raw_return : decode_transaction(raw_return)
242
+ options[:decode_values] == false ? raw_return : decode_transaction(raw_return)
251
243
  OpenStruct.new decoded_return
252
244
  end
253
245
 
254
- def decode_transaction(transaction) # rubocop:disable Metrics/MethodLength
246
+ def decode_transaction(transaction)
255
247
  return transaction if transaction['Results'].nil? || transaction['Results'].empty?
256
248
 
257
249
  transaction.tap do |txn|
@@ -7,31 +7,26 @@ module Diplomat
7
7
  # @param key [String] the key
8
8
  # @param session [String] the session, generated from Diplomat::Session.create
9
9
  # @param value [String] the value for the key
10
- # @param options [Hash] :dc string for dc specific query
10
+ # @param options [Hash] options parameter hash
11
11
  # @return [Boolean] If the lock was acquired
12
- # rubocop:disable AbcSize
13
- def acquire(key, session, value = nil, options = nil)
14
- raw = @conn.put do |req|
15
- url = ["/v1/kv/#{key}"]
16
- url += use_named_parameter('acquire', session)
17
- url += check_acl_token
18
- url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
19
-
20
- req.url concat_url url
21
- req.body = value unless value.nil?
22
- end
12
+ def acquire(key, session, value = nil, options = {})
13
+ custom_params = []
14
+ custom_params << use_named_parameter('acquire', session)
15
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
16
+ custom_params << use_named_parameter('flags', options[:flags]) if options && options[:flags]
17
+ data = value unless value.nil?
18
+ raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, data, custom_params)
23
19
  raw.body.chomp == 'true'
24
20
  end
25
- # rubocop:enable AbcSize
26
21
 
27
22
  # wait to aquire a lock
28
23
  # @param key [String] the key
29
24
  # @param session [String] the session, generated from Diplomat::Session.create
30
25
  # @param value [String] the value for the key
31
26
  # @param check_interval [Integer] number of seconds to wait between retries
32
- # @param options [Hash] :dc string for dc specific query
27
+ # @param options [Hash] options parameter hash
33
28
  # @return [Boolean] If the lock was acquired
34
- def wait_to_acquire(key, session, value = nil, check_interval = 10, options = nil)
29
+ def wait_to_acquire(key, session, value = nil, check_interval = 10, options = {})
35
30
  acquired = false
36
31
  until acquired
37
32
  acquired = acquire(key, session, value, options)
@@ -45,16 +40,15 @@ module Diplomat
45
40
  # @param session [String] the session, generated from Diplomat::Session.create
46
41
  # @param options [Hash] :dc string for dc specific query
47
42
  # @return [nil]
48
- def release(key, session, options = nil)
49
- raw = @conn.put do |req|
50
- url = ["/v1/kv/#{key}"]
51
- url += use_named_parameter('release', session)
52
- url += check_acl_token
53
- url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
54
-
55
- req.url concat_url url
56
- end
43
+ # rubocop:disable AbcSize
44
+ def release(key, session, options = {})
45
+ custom_params = []
46
+ custom_params << use_named_parameter('release', session)
47
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
48
+ custom_params << use_named_parameter('flags', options[:flags]) if options && options[:flags]
49
+ raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, nil, custom_params)
57
50
  raw.body
58
51
  end
52
+ # rubocop:enable AbcSize
59
53
  end
60
54
  end
@@ -7,7 +7,7 @@ module Diplomat
7
7
  # @param n [String] the node
8
8
  # @param options [Hash] :dc string for dc specific query
9
9
  # @return [Hash] { :enabled => true, :reason => 'foo' }
10
- def enabled(n, options = nil)
10
+ def enabled(n, options = {})
11
11
  health = Diplomat::Health.new(@conn)
12
12
  result = health.node(n, options)
13
13
  .select { |check| check['CheckID'] == '_node_maintenance' }
@@ -25,21 +25,17 @@ module Diplomat
25
25
  # @param reason [String] the reason for enabling maintenance mode
26
26
  # @param options [Hash] :dc string for dc specific query
27
27
  # @return true if call is successful
28
- # rubocop:disable AbcSize
29
- def enable(enable = true, reason = nil, options = nil)
30
- raw = @conn.put do |req|
31
- url = ['/v1/agent/maintenance']
32
- url << use_named_parameter('enable', enable.to_s)
33
- url << use_named_parameter('reason', reason) if reason
34
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
35
- req.url concat_url url
36
- end
28
+ def enable(enable = true, reason = nil, options = {})
29
+ custom_params = []
30
+ custom_params << use_named_parameter('enable', enable.to_s)
31
+ custom_params << use_named_parameter('reason', reason) if reason
32
+ custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
33
+ raw = send_put_request(@conn, ['/v1/agent/maintenance'], options, nil, custom_params)
37
34
 
38
35
  return_status = raw.status == 200
39
36
  raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}" unless return_status
40
37
 
41
38
  return_status
42
39
  end
43
- # rubocop:enable AbcSize
44
40
  end
45
41
  end
@@ -4,9 +4,10 @@ module Diplomat
4
4
  @access_methods = [:get]
5
5
 
6
6
  # Get all members
7
+ # @param options [Hash] options parameter hash
7
8
  # @return [OpenStruct] all data associated with the service
8
- def get
9
- ret = @conn.get '/v1/agent/members'
9
+ def get(options = {})
10
+ ret = send_get_request(@conn, ['/v1/agent/members'], options)
10
11
  JSON.parse(ret.body)
11
12
  end
12
13
  end
@@ -7,47 +7,36 @@ module Diplomat
7
7
  # @param key [String] the key
8
8
  # @param options [Hash] :dc string for dc specific query
9
9
  # @return [OpenStruct] all data associated with the node
10
- def get(key, options = nil)
11
- url = ["/v1/catalog/node/#{key}"]
12
- url += check_acl_token
13
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
14
-
15
- # If the request fails, it's probably due to a bad path
16
- # so return a PathNotFound error.
17
- ret = @conn.get concat_url url
10
+ def get(key, options = {})
11
+ custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
12
+ ret = send_get_request(@conn, ["/v1/catalog/node/#{key}"], options, custom_params)
18
13
  OpenStruct.new JSON.parse(ret.body)
19
- rescue Faraday::ClientError
20
- raise Diplomat::PathNotFound
21
14
  end
22
15
 
23
16
  # Get all the nodes
24
17
  # @param options [Hash] :dc string for dc specific query
25
18
  # @return [OpenStruct] the list of all nodes
26
- def get_all(options = nil)
27
- url = ['/v1/catalog/nodes']
28
- url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
29
-
30
- ret = @conn.get concat_url url
19
+ def get_all(options = {})
20
+ custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
21
+ ret = send_get_request(@conn, ['/v1/catalog/nodes'], options, custom_params)
31
22
  JSON.parse(ret.body).map { |service| OpenStruct.new service }
32
- rescue Faraday::ClientError
33
- raise Diplomat::PathNotFound
34
23
  end
35
24
 
36
25
  # Register a node
37
26
  # @param definition [Hash] Hash containing definition of a node to register
27
+ # @param options [Hash] options parameter hash
38
28
  # @return [Boolean]
39
- def register(definition, path = '/v1/catalog/register')
40
- register = @conn.put path, JSON.dump(definition)
41
-
29
+ def register(definition, options = {})
30
+ register = send_put_request(@conn, ['/v1/catalog/register'], options, definition)
42
31
  register.status == 200
43
32
  end
44
33
 
45
34
  # De-register a node (and all associated services and checks)
46
35
  # @param definition [Hash] Hash containing definition of a node to de-register
36
+ # @param options [Hash] options parameter hash
47
37
  # @return [Boolean]
48
- def deregister(definition, path = '/v1/catalog/deregister')
49
- deregister = @conn.put path, JSON.dump(definition)
50
-
38
+ def deregister(definition, options = {})
39
+ deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, definition)
51
40
  deregister.status == 200
52
41
  end
53
42
  end