consul-templaterb 1.35.1 → 1.36.0

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: a3a0f2cd7a7e458f2af107abbca891f03431bcc42df8651e9658bfa928f5d7a1
4
- data.tar.gz: a9494da14ca77669a1f044250e421f3546dfeb791ea59c329f6ee43e6af535e5
3
+ metadata.gz: f29a96f18a7e7229c457c0a31cbb5211ae5d57a96e85193f47154131cbf42d43
4
+ data.tar.gz: 8fe3323981690e183585c80fe58a4ab44f1a2deffa72bbf14bd1a749cc4447e3
5
5
  SHA512:
6
- metadata.gz: 1f10abf7fb476c9f2ea652d7c0c68519edb11c9e5bf75e340dbd0e64f0816e84460e75b75380312c7b6911e5f23fe2cef8682b82f661411ef5f0e97ad7f8a7b4
7
- data.tar.gz: 502a71a1f9f275fe646285e641d6075463d157f66bf737154b8ae9c8b4609e4d8983759629bf5ef998e1f66621e4963bad5fc837eb295a2a08b26a55e1bfeabc
6
+ metadata.gz: a47be289a8a7c447732daf3f63624b913a81ee5a09e495c518c020d7c7b9efe9349ed6b09cb2e9ed4fc202ccedd0db650087e7788e9455ec52a3d287f37cf555
7
+ data.tar.gz: 275755990a89710775fab210479d916fc12e671f8de0f6a42ca94f0708ddcec2fc693dc585b4a00e123b84adaa628630a77f1721cc452a8616c893086030af2a
data/TemplateAPI.md CHANGED
@@ -631,11 +631,12 @@ secret('secret/foo', [force_ttl: intInSecond])
631
631
 
632
632
  ## remote_resource
633
633
 
634
- ### as_json(url, default_value, [refresh_delay_secs: intInSecond])
634
+ ### as_json(url, default_value, [refresh_delay_secs: intInSecond, default_value_on_error: bool])
635
635
 
636
636
  Fetch json data from any url. This allows to create templates with consul/vault data mixed in with data coming from other services/api.
637
637
  Polling interval can be controlled with `refresh_delay_secs` option.
638
638
  Request method (`GET`, `POST`, ...) can be controlled with `request_method` option.
639
+ To return default value on the case of error, set `default_value_on_error` to true.
639
640
 
640
641
  ```erb
641
642
  remote_resource.as_json('http://my-api.dev/fridge/list.json', [])
@@ -29,14 +29,14 @@ module Consul
29
29
  @endp_manager = endpoints_manager
30
30
  end
31
31
 
32
- def as_json(url, default_value, refresh_delay_secs: 10, **opts)
32
+ def as_json(url, default_value, refresh_delay_secs: 10, default_value_on_error: false, **opts)
33
33
  conf = JSONConfiguration.new(url: url, min_duration: refresh_delay_secs, retry_on_non_diff: refresh_delay_secs, **opts)
34
34
  endpoint_id = url + opts.hash.to_s
35
35
  @endp_manager.create_if_missing(url, {}, endpoint_id: endpoint_id) do
36
36
  if default_value.is_a?(Array)
37
- ConsulTemplateJSONArray.new(JSONEndpoint.new(conf, url, default_value))
37
+ ConsulTemplateJSONArray.new(JSONEndpoint.new(conf, url, default_value, default_value_on_error: default_value_on_error))
38
38
  else
39
- ConsulTemplateJSONObject.new(JSONEndpoint.new(conf, url, default_value))
39
+ ConsulTemplateJSONObject.new(JSONEndpoint.new(conf, url, default_value, default_value_on_error: default_value_on_error))
40
40
  end
41
41
  end
42
42
  end
@@ -93,9 +93,10 @@ module Consul
93
93
  # Endpoint (aka URL) of a remote API that might be called
94
94
  class JSONEndpoint
95
95
  attr_reader :conf, :url, :queue, :stats, :last_result, :enforce_json_200, :start_time, :default_value, :query_params
96
- def initialize(conf, url, default_value, enforce_json_200 = true, query_params = {})
96
+ def initialize(conf, url, default_value, enforce_json_200: true, query_params: {}, default_value_on_error: false)
97
97
  @conf = conf.create(url)
98
98
  @default_value = default_value
99
+ @default_value_on_error = default_value_on_error
99
100
  @url = url
100
101
  @queue = EM::Queue.new
101
102
  @s_callbacks = []
@@ -174,7 +175,7 @@ module Consul
174
175
  retry_in = _compute_retry_in([600, conf.retry_duration + 2**@consecutive_errors].min)
175
176
  ::Consul::Async::Debug.puts_error "[#{url}] - #{http.error} - Retry in #{retry_in}s #{stats.body_bytes_human}"
176
177
  @consecutive_errors += 1
177
- http_result = HttpResponse.new(http)
178
+ http_result = @default_value_on_error ? HttpResponse.new(http, @default_value.to_json) : HttpResponse.new(http)
178
179
  EventMachine.add_timer(retry_in) do
179
180
  yield
180
181
  queue.push(Object.new)
@@ -203,6 +204,7 @@ module Consul
203
204
  http = connection[:conn].send(request_method, build_request)
204
205
  http.callback do
205
206
  if enforce_json_200 && !(200..299).cover?(http.response_header.status) && http.response_header['Content-Type'] != 'application/json'
207
+ handle_default_on_error(http) if @default_value_on_error
206
208
  _handle_error(http) do
207
209
  warn "[RETRY][#{url}] (#{@consecutive_errors} errors)" if (@consecutive_errors % 10) == 1
208
210
  end
@@ -227,6 +229,7 @@ module Consul
227
229
  end
228
230
 
229
231
  http.errback do
232
+ handle_default_on_error(http) if @default_value_on_error
230
233
  unless @stopping
231
234
  _handle_error(http) do
232
235
  if (@consecutive_errors % 10) == 1
@@ -243,6 +246,15 @@ module Consul
243
246
  end
244
247
  queue.pop(&cb)
245
248
  end
249
+
250
+ def handle_default_on_error(http)
251
+ ::Consul::Async::Debug.puts_error "[#{url}] response status #{http.response_header.status}; using default value"
252
+ @consecutive_errors = 0
253
+ json_result = JSONResult.new(@default_value.to_json, false, HttpResponse.new(http, ''), stats, 10, fake: true)
254
+ @last_result = json_result
255
+ @ready = true
256
+ @s_callbacks.each { |c| c.call(json_result) }
257
+ end
246
258
  end
247
259
  end
248
260
  end
@@ -1,5 +1,5 @@
1
1
  module Consul
2
2
  module Async
3
- VERSION = '1.35.1'.freeze
3
+ VERSION = '1.36.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul-templaterb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.35.1
4
+ version: 1.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SRE Core Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-26 00:00:00.000000000 Z
11
+ date: 2024-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-http-request