diplomat 2.2.5 → 2.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ee74cdd0ff09ba455fa1185856231070da1a7b5296bcec09db981a72cb0a235
4
- data.tar.gz: 2fd779c20984bc5f394c9c5fb6aef1a30abfcc99bd74d1700044c6fe2cc1d4e4
3
+ metadata.gz: d4fc2a1fed34abcea03720130cdb3403c7f4e95183ba2685a7cb0e97a8b38021
4
+ data.tar.gz: e3d95df82fe5db865d2018f4cdc5bfc4795bf91e2d006b2ddc96141cea0e4184
5
5
  SHA512:
6
- metadata.gz: 9d9a9c7e1d7a2fdfdabb7139013da70e5dee028c18dcd2fa820ed5d235a34a26ac3a1b5108d7fab2d76be1bad370ed85a47b791b190104088be2a63287d76d58
7
- data.tar.gz: 9daf8932e9bd5e2e74ad19cb91b070552805f1f249d553c2975899edc2f1ee27146b6207fb07867cbbb563b0b60b8768d68fc3d276902de28ce0b0a65885d662
6
+ metadata.gz: 2f139bed5e80cd4f8f793a93d7171de48de69161a679336c5675847c26e3141d0c28b5bcea79dfc2447e7632750e12336d721ba9e48d11d60efd88062a799602
7
+ data.tar.gz: f6c7ad2ded6476794f53f03e70d663d06bbae0386a5246e8a84b3f5ec58cf18aa9f0c970f9b1e09f00da031ff14659e6564262f88b1c2abedf88b1754beafc5f
data/README.md CHANGED
@@ -89,6 +89,26 @@ Diplomat::Kv.get('foo/', recurse: true)
89
89
  # => [{:key=>"foo/a", :value=>"lorem"}, {:key=>"foo/b", :value=>"ipsum"}, {:key=>"foo/c", :value=>"dolor"}]
90
90
  ```
91
91
 
92
+ You can also use `get_all` to retrieve values recursively with a consistent return type:
93
+
94
+ ```ruby
95
+ Diplomat::Kv.put('foo/a', 'lorem')
96
+ Diplomat::Kv.put('foo/b', 'ipsum')
97
+ Diplomat::Kv.put('foo/c', 'dolor')
98
+
99
+ Diplomat::Kv.get('foo/', recurse: true)
100
+ # => [{:key=>"foo/a", :value=>"lorem"}, {:key=>"foo/b", :value=>"ipsum"}, {:key=>"foo/c", :value=>"dolor"}]
101
+ Diplomat::Kv.get_all('foo/')
102
+ # => [{:key=>"foo/a", :value=>"lorem"}, {:key=>"foo/b", :value=>"ipsum"}, {:key=>"foo/c", :value=>"dolor"}]
103
+
104
+ Diplomat::Kv.put('bar/a', 'lorem')
105
+
106
+ Diplomat::Kv.get('bar/', recurse: true)
107
+ # => "lorem"
108
+ Diplomat::Kv.get_all('bar/')
109
+ # => [{:key=>"bar/a", :value=>"lorem"}]
110
+ ```
111
+
92
112
 
93
113
  Or list all available keys:
94
114
 
@@ -1,7 +1,7 @@
1
1
  module Diplomat
2
2
  # Methods for interacting with the Consul KV API endpoint
3
3
  class Kv < Diplomat::RestClient
4
- @access_methods = %i[get put delete txn]
4
+ @access_methods = %i[get get_all put delete txn]
5
5
  attr_reader :key, :value, :raw
6
6
 
7
7
  # Get a value by its key, potentially blocking for the first or next value
@@ -18,7 +18,7 @@ module Diplomat
18
18
  # Only applies when combined with :keys option.
19
19
  # @option options [Boolean] :nil_values If to return keys/dirs with nil values
20
20
  # @option options [Boolean] :convert_to_hash Take the data returned from consul and build a hash
21
- # @option options [Callable] :transformation funnction to invoke on keys values
21
+ # @option options [Callable] :transformation function to invoke on keys values
22
22
  # @param not_found [Symbol] behaviour if the key doesn't exist;
23
23
  # :reject with exception, :return degenerate value, or :wait for it to appear
24
24
  # @param found [Symbol] behaviour if the key does exist;
@@ -42,20 +42,11 @@ module Diplomat
42
42
  # - W W - get the first or next value; wait until there is an update
43
43
  # rubocop:disable PerceivedComplexity, MethodLength, LineLength, CyclomaticComplexity
44
44
  def get(key, options = {}, not_found = :reject, found = :return)
45
- key = normalize_key_for_uri(key)
46
- @key = key
47
45
  @options = options
48
- custom_params = []
49
- custom_params << recurse_get(@options)
50
- custom_params << use_consistency(options)
51
- custom_params << dc(@options)
52
- custom_params << keys(@options)
53
- custom_params << separator(@options)
54
-
55
46
  return_nil_values = @options && @options[:nil_values]
56
47
  transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil
48
+ raw = get_raw(key, options)
57
49
 
58
- raw = send_get_request(@conn_no_err, ["/v1/kv/#{@key}"], options, custom_params)
59
50
  if raw.status == 404
60
51
  case not_found
61
52
  when :reject
@@ -86,18 +77,71 @@ module Diplomat
86
77
  end
87
78
 
88
79
  # Wait for first/next value
89
- custom_params << use_named_parameter('index', index)
90
- if options.nil?
91
- options = { timeout: 86_400 }
92
- else
93
- options[:timeout] = 86_400
94
- end
95
- @raw = send_get_request(@conn, ["/v1/kv/#{@key}"], options, custom_params)
80
+ @raw = wait_for_value(index, options)
96
81
  @raw = parse_body
97
82
  return_value(return_nil_values, transformation)
98
83
  end
99
84
  # rubocop:enable PerceivedComplexity, LineLength, MethodLength, CyclomaticComplexity
100
85
 
86
+ # Get all keys recursively, potentially blocking for the first or next value
87
+ # @param key [String] the key
88
+ # @param options [Hash] the query params
89
+ # @option options [String] :consistency The read consistency type
90
+ # @option options [String] :dc Target datacenter
91
+ # @option options [Boolean] :keys Only return key names.
92
+ # @option options [Boolean] :decode_values Return consul response with decoded values.
93
+ # @option options [String] :separator List only up to a given separator.
94
+ # Only applies when combined with :keys option.
95
+ # @option options [Boolean] :nil_values If to return keys/dirs with nil values
96
+ # @option options [Boolean] :convert_to_hash Take the data returned from consul and build a hash
97
+ # @option options [Callable] :transformation function to invoke on keys values
98
+ # @param not_found [Symbol] behaviour if the key doesn't exist;
99
+ # :reject with exception, :return degenerate value, or :wait for it to appear
100
+ # @param found [Symbol] behaviour if the key does exist;
101
+ # :reject with exception, :return its current value, or :wait for its next value
102
+ # @return [List] List of hashes, one hash for each key-value returned
103
+ # rubocop:disable PerceivedComplexity, MethodLength, LineLength, CyclomaticComplexity
104
+ def get_all(key, options = {}, not_found = :reject, found = :return)
105
+ @options = options
106
+ @options[:recurse] = true
107
+ return_nil_values = @options && @options[:nil_values]
108
+ transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil
109
+
110
+ raw = get_raw(key, options)
111
+ if raw.status == 404
112
+ case not_found
113
+ when :reject
114
+ raise Diplomat::KeyNotFound, key
115
+ when :return
116
+ return @value = []
117
+ when :wait
118
+ index = raw.headers['x-consul-index']
119
+ end
120
+ elsif raw.status == 200
121
+ case found
122
+ when :reject
123
+ raise Diplomat::KeyAlreadyExists, key
124
+ when :return
125
+ @raw = raw
126
+ @raw = parse_body
127
+ return decode_values if @options && @options[:decode_values]
128
+ return convert_to_hash(return_value(return_nil_values, transformation, true)) if @options && @options[:convert_to_hash]
129
+
130
+ return return_value(return_nil_values, transformation, true)
131
+ when :wait
132
+ index = raw.headers['x-consul-index']
133
+ end
134
+ else
135
+ raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}"
136
+ end
137
+
138
+ # Wait for first/next value
139
+ @raw = wait_for_value(index, options)
140
+ @raw = parse_body
141
+ return_value(return_nil_values, transformation, true)
142
+ end
143
+ # rubocop:enable PerceivedComplexity, MethodLength, LineLength, CyclomaticComplexity
144
+
101
145
  # Associate a value with a key
102
146
  # @param key [String] the key
103
147
  # @param value [String] the value
@@ -172,6 +216,30 @@ module Diplomat
172
216
 
173
217
  private
174
218
 
219
+ def get_raw(key, options = {}, custom_params = [])
220
+ key = normalize_key_for_uri(key)
221
+ @key = key
222
+ @options = options
223
+ custom_params << recurse_get(@options)
224
+ custom_params << use_consistency(@options)
225
+ custom_params << dc(@options)
226
+ custom_params << keys(@options)
227
+ custom_params << separator(@options)
228
+
229
+ send_get_request(@conn_no_err, ["/v1/kv/#{@key}"], options, custom_params)
230
+ end
231
+
232
+ def wait_for_value(index, options)
233
+ custom_params = []
234
+ custom_params << use_named_parameter('index', index)
235
+ if options.nil?
236
+ options = { timeout: 86_400 }
237
+ else
238
+ options[:timeout] = 86_400
239
+ end
240
+ get_raw(key, options, custom_params)
241
+ end
242
+
175
243
  def recurse_get(options)
176
244
  options[:recurse] ? ['recurse'] : []
177
245
  end
@@ -241,7 +241,7 @@ module Diplomat
241
241
  rest_options[:headers].map { |k, v| req.headers[k.to_sym] = v } unless rest_options[:headers].nil?
242
242
  req.options.timeout = options[:timeout] if options[:timeout]
243
243
  end
244
- rescue Faraday::ClientError => e
244
+ rescue Faraday::ClientError, Faraday::ServerError => e
245
245
  resp = e.response
246
246
  if resp
247
247
  raise Diplomat::AclNotFound, e if resp[:status] == 403 && resp[:body] == 'ACL not found'
@@ -1,3 +1,3 @@
1
1
  module Diplomat
2
- VERSION = '2.2.5'.freeze
2
+ VERSION = '2.2.6'.freeze
3
3
  end
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.2.5
4
+ version: 2.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hamelink
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-06-03 00:00:00.000000000 Z
12
+ date: 2020-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -195,16 +195,22 @@ dependencies:
195
195
  name: faraday
196
196
  requirement: !ruby/object:Gem::Requirement
197
197
  requirements:
198
- - - "~>"
198
+ - - ">="
199
199
  - !ruby/object:Gem::Version
200
200
  version: '0.9'
201
+ - - "<"
202
+ - !ruby/object:Gem::Version
203
+ version: 2.0.0
201
204
  type: :runtime
202
205
  prerelease: false
203
206
  version_requirements: !ruby/object:Gem::Requirement
204
207
  requirements:
205
- - - "~>"
208
+ - - ">="
206
209
  - !ruby/object:Gem::Version
207
210
  version: '0.9'
211
+ - - "<"
212
+ - !ruby/object:Gem::Version
213
+ version: 2.0.0
208
214
  description: Diplomat is a simple wrapper for Consul
209
215
  email:
210
216
  - john@johnhamelink.com
@@ -261,7 +267,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
267
  - !ruby/object:Gem::Version
262
268
  version: '0'
263
269
  requirements: []
264
- rubygems_version: 3.0.3
270
+ rubyforge_project:
271
+ rubygems_version: 2.7.7
265
272
  signing_key:
266
273
  specification_version: 4
267
274
  summary: Diplomat is a simple wrapper for Consul