From 5b661435071e8edc83c9626729eb77239e21759f Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Mon, 16 Apr 2018 23:54:54 +0200
Subject: [PATCH 1/5] Allow using specific consitency on all methods
This fixes https://github.com/WeAreFarmGeek/diplomat/issues/110
---
lib/diplomat/acl.rb | 2 +-
lib/diplomat/api_options.rb | 4 ----
lib/diplomat/datacenter.rb | 3 ++-
lib/diplomat/health.rb | 4 ++++
lib/diplomat/node.rb | 1 +
lib/diplomat/nodes.rb | 4 +++-
lib/diplomat/query.rb | 3 ++-
lib/diplomat/rest_client.rb | 13 +++++++++++++
lib/diplomat/service.rb | 2 ++
9 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/lib/diplomat/acl.rb b/lib/diplomat/acl.rb
index 8c0f599..e61655c 100644
--- a/lib/diplomat/acl.rb
+++ b/lib/diplomat/acl.rb
@@ -15,7 +15,7 @@ def info(id, options = nil, not_found = :reject, found = :return)
@options = options
url = ["/v1/acl/info/#{id}"]
url << check_acl_token
- url << use_consistency(options)
+ url << use_consistency(options) if use_consistency(options, nil)
raw = @conn_no_err.get concat_url url
if raw.status == 200 && raw.body.chomp != 'null'
diff --git a/lib/diplomat/api_options.rb b/lib/diplomat/api_options.rb
index 03ce558..c5f9ab2 100644
--- a/lib/diplomat/api_options.rb
+++ b/lib/diplomat/api_options.rb
@@ -9,10 +9,6 @@ def use_cas(options)
options ? use_named_parameter('cas', options[:cas]) : []
end
- def use_consistency(options)
- options && options[:consistency] ? [options[:consistency].to_s] : []
- end
-
# Mapping for valid key/value store transaction verbs and required parameters
#
# @return [Hash] valid key/store transaction verbs and required parameters
diff --git a/lib/diplomat/datacenter.rb b/lib/diplomat/datacenter.rb
index a4808c6..7e73fd5 100644
--- a/lib/diplomat/datacenter.rb
+++ b/lib/diplomat/datacenter.rb
@@ -6,8 +6,9 @@ class Datacenter < Diplomat::RestClient
# Get an array of all avaliable datacenters accessible by the local consul agent
# @param meta [Hash] output structure containing header information about the request (index)
# @return [OpenStruct] all datacenters avaliable to this consul agent
- def get(meta = nil)
+ def get(meta = nil, options = nil)
url = ['/v1/catalog/datacenters']
+ url << use_consistency(options) if use_consistency(options, nil)
ret = @conn.get concat_url url
diff --git a/lib/diplomat/health.rb b/lib/diplomat/health.rb
index 2dd18f2..6220be8 100644
--- a/lib/diplomat/health.rb
+++ b/lib/diplomat/health.rb
@@ -11,6 +11,7 @@ class Health < Diplomat::RestClient
def node(n, options = nil)
url = ["/v1/health/node/#{n}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -27,6 +28,7 @@ def node(n, options = nil)
def checks(s, options = nil)
url = ["/v1/health/checks/#{s}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -49,6 +51,7 @@ def service(s, options = nil)
url << 'passing' if options && options[:passing]
url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
url << use_named_parameter('near', options[:near]) if options && options[:near]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -68,6 +71,7 @@ def state(s, options = nil)
url = ["/v1/health/state/#{s}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
url << use_named_parameter('near', options[:near]) if options && options[:near]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
diff --git a/lib/diplomat/node.rb b/lib/diplomat/node.rb
index f29af90..4780aef 100644
--- a/lib/diplomat/node.rb
+++ b/lib/diplomat/node.rb
@@ -13,6 +13,7 @@ def get(key, options = nil)
url = ["/v1/catalog/node/#{key}"]
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
diff --git a/lib/diplomat/nodes.rb b/lib/diplomat/nodes.rb
index 62cee13..6100000 100644
--- a/lib/diplomat/nodes.rb
+++ b/lib/diplomat/nodes.rb
@@ -7,14 +7,16 @@ class Nodes < Diplomat::RestClient
# Get all nodes
# @deprecated Please use Diplomat::Node instead.
# @return [OpenStruct] all data associated with the nodes in catalog
- def get
+ def get(options = nil)
ret = @conn.get '/v1/catalog/nodes'
+ url << use_consistency(options) if use_consistency(options, nil)
JSON.parse(ret.body)
end
def get_all(options = nil)
url = ['/v1/catalog/nodes']
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |service| OpenStruct.new service }
diff --git a/lib/diplomat/query.rb b/lib/diplomat/query.rb
index da239ac..8f205f8 100644
--- a/lib/diplomat/query.rb
+++ b/lib/diplomat/query.rb
@@ -13,6 +13,7 @@ def get(key, options = nil)
url = ["/v1/query/#{key}"]
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |query| OpenStruct.new query }
@@ -27,7 +28,7 @@ def get_all(options = nil)
url = ['/v1/query']
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
-
+ url << use_consistency(options) if use_consistency(options, nil)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |query| OpenStruct.new query }
rescue Faraday::ClientError
diff --git a/lib/diplomat/rest_client.rb b/lib/diplomat/rest_client.rb
index efbee4a..dc08a0b 100644
--- a/lib/diplomat/rest_client.rb
+++ b/lib/diplomat/rest_client.rb
@@ -17,6 +17,19 @@ def use_named_parameter(name, value)
value ? ["#{name}=#{value}"] : []
end
+ def use_consistency(options, default_value = [])
+ return default_value unless options
+ case options[:consistency]
+ when 'consistent'
+ return use_named_parameter('consistent', 'consistent')
+ when 'stale'
+ return use_named_parameter('stale', 'stale')
+ when 'leader'
+ return use_named_parameter('leader', 'leader')
+ end
+ default_value
+ end
+
# Assemble a url from an array of parts.
# @param parts [Array] the url chunks to be assembled
# @return [String] the resultant url string
diff --git a/lib/diplomat/service.rb b/lib/diplomat/service.rb
index 463607c..2334589 100644
--- a/lib/diplomat/service.rb
+++ b/lib/diplomat/service.rb
@@ -23,6 +23,7 @@ def get(key, scope = :first, options = nil, meta = nil)
url << use_named_parameter('index', options[:index]) if options && options[:index]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
+ url << use_consistency(options) if use_consistency(options, nil)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -53,6 +54,7 @@ def get_all(options = nil)
url = ['/v1/catalog/services']
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
+ url << use_consistency(options) if use_consistency(options, nil)
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
From 7a0097b201a8da455b682360da74d8c64a42e86a Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Thu, 19 Apr 2018 01:00:42 +0200
Subject: [PATCH 2/5] Rubocop fixes
---
.rubocop.yml | 9 +++++++++
lib/diplomat.rb | 4 ++--
lib/diplomat/kv.rb | 2 +-
lib/diplomat/rest_client.rb | 2 +-
lib/diplomat/service.rb | 1 -
5 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/.rubocop.yml b/.rubocop.yml
index 7681dd0..2983292 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -2,6 +2,12 @@
AllCops:
DisplayCopNames: true
+Metrics/AbcSize:
+ Max: 17
+
+Metrics/CyclomaticComplexity:
+ Max: 7
+
Metrics/LineLength:
# This will disable the rule completely, regardless what other options you put
Enabled: true
@@ -11,3 +17,6 @@ Metrics/LineLength:
# Allow classes longer than 100 lines of code
ClassLength:
Max: 250
+
+Naming/UncommunicativeMethodParamName:
+ Enabled: false
diff --git a/lib/diplomat.rb b/lib/diplomat.rb
index e18f451..30ad53f 100644
--- a/lib/diplomat.rb
+++ b/lib/diplomat.rb
@@ -23,8 +23,8 @@ def require_libs(*libs)
raise 'Diplomat only supports ruby >= 2.0.0' unless RUBY_VERSION.to_f >= 2.0
- self.root_path = File.expand_path '..', __FILE__
- self.lib_path = File.expand_path '../diplomat', __FILE__
+ self.root_path = File.expand_path __dir__
+ self.lib_path = File.expand_path 'diplomat', __dir__
require_libs 'configuration', 'rest_client', 'api_options', 'kv', 'datacenter',
'service', 'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
diff --git a/lib/diplomat/kv.rb b/lib/diplomat/kv.rb
index 64c070b..9db65d6 100644
--- a/lib/diplomat/kv.rb
+++ b/lib/diplomat/kv.rb
@@ -256,7 +256,7 @@ def decode_transaction(transaction) # rubocop:disable Metrics/MethodLength
next unless resp['KV']['Value']
begin
resp['KV']['Value'] = Base64.decode64(resp['KV']['Value'])
- rescue # rubocop:disable RescueWithoutErrorClass
+ rescue StandardError
nil
end
end
diff --git a/lib/diplomat/rest_client.rb b/lib/diplomat/rest_client.rb
index dc08a0b..edee33e 100644
--- a/lib/diplomat/rest_client.rb
+++ b/lib/diplomat/rest_client.rb
@@ -156,7 +156,7 @@ def decode_values
@raw.each_with_object([]) do |acc, el|
begin
acc['Value'] = Base64.decode64(acc['Value'])
- rescue # rubocop:disable RescueWithoutErrorClass
+ rescue StandardError
nil
end
el << acc
diff --git a/lib/diplomat/service.rb b/lib/diplomat/service.rb
index 2334589..9221f67 100644
--- a/lib/diplomat/service.rb
+++ b/lib/diplomat/service.rb
@@ -115,6 +115,5 @@ def maintenance(service_id, options = { enable: true })
end
maintenance.status == 200
end
- # rubocop:enable AbcSize
end
end
From d77cc72b8f0e9e83280fd71fc660b696dc33a37c Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Thu, 19 Apr 2018 01:06:02 +0200
Subject: [PATCH 3/5] More rubocop fixes
---
lib/diplomat/lock.rb | 2 --
1 file changed, 2 deletions(-)
diff --git a/lib/diplomat/lock.rb b/lib/diplomat/lock.rb
index da70a9c..96a1925 100644
--- a/lib/diplomat/lock.rb
+++ b/lib/diplomat/lock.rb
@@ -11,7 +11,6 @@ class Lock < Diplomat::RestClient
# @param value [String] the value for the key
# @param options [Hash] :dc string for dc specific query
# @return [Boolean] If the lock was acquired
- # rubocop:disable AbcSize
def acquire(key, session, value = nil, options = nil)
raw = @conn.put do |req|
url = ["/v1/kv/#{key}"]
@@ -24,7 +23,6 @@ def acquire(key, session, value = nil, options = nil)
end
raw.body.chomp == 'true'
end
- # rubocop:enable AbcSize
# wait to aquire a lock
# @param key [String] the key
From 91ab964a04a391ee3733fc6e943298d144659bdc Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Wed, 2 May 2018 13:01:41 +0200
Subject: [PATCH 4/5] Updated documentation for all methods
---
lib/diplomat/acl.rb | 2 +-
lib/diplomat/datacenter.rb | 5 ++++-
lib/diplomat/health.rb | 25 +++++++++++++++++--------
lib/diplomat/kv.rb | 2 +-
lib/diplomat/node.rb | 10 +++++++---
lib/diplomat/nodes.rb | 4 ++--
lib/diplomat/query.rb | 34 ++++++++++++++++++++++------------
lib/diplomat/rest_client.rb | 4 +++-
lib/diplomat/service.rb | 9 ++++++---
9 files changed, 63 insertions(+), 32 deletions(-)
diff --git a/lib/diplomat/acl.rb b/lib/diplomat/acl.rb
index e61655c..383ea4b 100644
--- a/lib/diplomat/acl.rb
+++ b/lib/diplomat/acl.rb
@@ -15,7 +15,7 @@ def info(id, options = nil, not_found = :reject, found = :return)
@options = options
url = ["/v1/acl/info/#{id}"]
url << check_acl_token
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
raw = @conn_no_err.get concat_url url
if raw.status == 200 && raw.body.chomp != 'null'
diff --git a/lib/diplomat/datacenter.rb b/lib/diplomat/datacenter.rb
index 7e73fd5..c365390 100644
--- a/lib/diplomat/datacenter.rb
+++ b/lib/diplomat/datacenter.rb
@@ -5,10 +5,13 @@ class Datacenter < Diplomat::RestClient
# Get an array of all avaliable datacenters accessible by the local consul agent
# @param meta [Hash] output structure containing header information about the request (index)
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all datacenters avaliable to this consul agent
def get(meta = nil, options = nil)
url = ['/v1/catalog/datacenters']
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
diff --git a/lib/diplomat/health.rb b/lib/diplomat/health.rb
index 6220be8..dd1816e 100644
--- a/lib/diplomat/health.rb
+++ b/lib/diplomat/health.rb
@@ -6,12 +6,14 @@ class Health < Diplomat::RestClient
# Get node health
# @param n [String] the node
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the node
def node(n, options = nil)
url = ["/v1/health/node/#{n}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -23,12 +25,14 @@ def node(n, options = nil)
# Get service checks
# @param s [String] the service
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the node
def checks(s, options = nil)
url = ["/v1/health/checks/#{s}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -40,9 +44,12 @@ def checks(s, options = nil)
# Get service health
# @param s [String] the service
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @param options [Hash] :passing boolean to return only checks in passing state
# @param options [Hash] :tag string for specific tag
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the node
# rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize
def service(s, options = nil)
@@ -51,7 +58,7 @@ def service(s, options = nil)
url << 'passing' if options && options[:passing]
url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
url << use_named_parameter('near', options[:near]) if options && options[:near]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -64,14 +71,16 @@ def service(s, options = nil)
# Get service health
# @param s [String] the state ("any", "passing", "warning", or "critical")
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the node
# rubocop:disable AbcSize
def state(s, options = nil)
url = ["/v1/health/state/#{s}"]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
url << use_named_parameter('near', options[:near]) if options && options[:near]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
diff --git a/lib/diplomat/kv.rb b/lib/diplomat/kv.rb
index 9db65d6..45c2d7a 100644
--- a/lib/diplomat/kv.rb
+++ b/lib/diplomat/kv.rb
@@ -50,7 +50,7 @@ def get(key, options = nil, not_found = :reject, found = :return)
url = ["/v1/kv/#{@key}"]
url += recurse_get(@options)
url += check_acl_token
- url += use_consistency(@options)
+ url += use_consistency(@options, [])
url += dc(@options)
url += keys(@options)
url += separator(@options)
diff --git a/lib/diplomat/node.rb b/lib/diplomat/node.rb
index 4780aef..7f5028b 100644
--- a/lib/diplomat/node.rb
+++ b/lib/diplomat/node.rb
@@ -7,13 +7,15 @@ class Node < Diplomat::RestClient
# Get a node by it's key
# @param key [String] the key
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the node
def get(key, options = nil)
url = ["/v1/catalog/node/#{key}"]
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -24,7 +26,9 @@ def get(key, options = nil)
end
# Get all the nodes
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] the list of all nodes
def get_all(options = nil)
url = ['/v1/catalog/nodes']
diff --git a/lib/diplomat/nodes.rb b/lib/diplomat/nodes.rb
index 6100000..c331234 100644
--- a/lib/diplomat/nodes.rb
+++ b/lib/diplomat/nodes.rb
@@ -9,14 +9,14 @@ class Nodes < Diplomat::RestClient
# @return [OpenStruct] all data associated with the nodes in catalog
def get(options = nil)
ret = @conn.get '/v1/catalog/nodes'
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
JSON.parse(ret.body)
end
def get_all(options = nil)
url = ['/v1/catalog/nodes']
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |service| OpenStruct.new service }
diff --git a/lib/diplomat/query.rb b/lib/diplomat/query.rb
index 8f205f8..4153fbe 100644
--- a/lib/diplomat/query.rb
+++ b/lib/diplomat/query.rb
@@ -7,13 +7,15 @@ class Query < Diplomat::RestClient
# Get a prepared query by it's key
# @param key [String] the prepared query ID
- # @param options [Hash] :dc string for dc specific query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] all data associated with the prepared query
def get(key, options = nil)
url = ["/v1/query/#{key}"]
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |query| OpenStruct.new query }
@@ -22,13 +24,15 @@ def get(key, options = nil)
end
# Get all prepared queries
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] the list of all prepared queries
def get_all(options = nil)
url = ['/v1/query']
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
JSON.parse(ret.body).map { |query| OpenStruct.new query }
rescue Faraday::ClientError
@@ -37,7 +41,8 @@ def get_all(options = nil)
# Create a prepared query or prepared query template
# @param definition [Hash] Hash containing definition of prepared query
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
# @return [String] the ID of the prepared query created
def create(definition, options = nil)
url = ['/v1/query']
@@ -55,7 +60,8 @@ def create(definition, options = nil)
# Delete a prepared query or prepared query template
# @param key [String] the prepared query ID
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
# @return [Boolean]
def delete(key, options = nil)
url = ["/v1/query/#{key}"]
@@ -69,7 +75,8 @@ def delete(key, options = nil)
# Update a prepared query or prepared query template
# @param key [String] the prepared query ID
# @param definition [Hash] Hash containing updated definition of prepared query
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
# @return [Boolean]
def update(key, definition, options = nil)
url = ["/v1/query/#{key}"]
@@ -86,8 +93,9 @@ def update(key, definition, options = nil)
# Execute a prepared query or prepared query template
# @param key [String] the prepared query ID or name
- # @param options [Hash] prepared query execution options
- # @option dc [String] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @option near [String] node name to sort the resulting list in ascending order based on the
# estimated round trip time from that node
# @option limit [Integer] to limit the size of the return list to the given number of results
@@ -99,7 +107,7 @@ def execute(key, options = nil)
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
url << use_named_parameter('near', options[:near]) if options && options[:near]
url << use_named_parameter('limit', options[:limit]) if options && options[:limit]
-
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
OpenStruct.new JSON.parse(ret.body)
rescue Faraday::ClientError
@@ -109,13 +117,15 @@ def execute(key, options = nil)
# Get the fully rendered query template
# @param key [String] the prepared query ID or name
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] the list of results from the prepared query or prepared query template
def explain(key, options = nil)
url = ["/v1/query/#{key}/explain"]
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
-
+ url << use_consistency(options) if use_consistency(options)
ret = @conn.get concat_url url
OpenStruct.new JSON.parse(ret.body)
rescue Faraday::ClientError
diff --git a/lib/diplomat/rest_client.rb b/lib/diplomat/rest_client.rb
index edee33e..d3019de 100644
--- a/lib/diplomat/rest_client.rb
+++ b/lib/diplomat/rest_client.rb
@@ -17,7 +17,9 @@ def use_named_parameter(name, value)
value ? ["#{name}=#{value}"] : []
end
- def use_consistency(options, default_value = [])
+ # Parse options and return consistency.
+ # If consistency is not found, will return default_value
+ def use_consistency(options, default_value = nil)
return default_value unless options
case options[:consistency]
when 'consistent'
diff --git a/lib/diplomat/service.rb b/lib/diplomat/service.rb
index 9221f67..70bf75f 100644
--- a/lib/diplomat/service.rb
+++ b/lib/diplomat/service.rb
@@ -12,6 +12,7 @@ class Service < Diplomat::RestClient
# @option wait [Integer] :wait string for wait time
# @option index [String] :index for index of last query
# @option dc [String] :dc data center to make request for
+ # @option options [String] :consistency The read consistency type
# @option tag [String] :tag service tag to get
# @param meta [Hash] output structure containing header information about the request (index)
# @return [OpenStruct] all data associated with the service
@@ -23,7 +24,7 @@ def get(key, scope = :first, options = nil, meta = nil)
url << use_named_parameter('index', options[:index]) if options && options[:index]
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
@@ -48,13 +49,15 @@ def get(key, scope = :first, options = nil, meta = nil)
# rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
# Get all the services
- # @param options [Hash] :dc Consul datacenter to query
+ # @param options [Hash] Options to use when performing request
+ # @option options [String] :dc string for dc specific query
+ # @option options [String] :consistency The read consistency type
# @return [OpenStruct] the list of all services
def get_all(options = nil)
url = ['/v1/catalog/services']
url += check_acl_token
url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
- url << use_consistency(options) if use_consistency(options, nil)
+ url << use_consistency(options) if use_consistency(options)
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
From 8241bdbbd053117cfd2d1ffa5b0dbdae4790377e Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Wed, 9 May 2018 23:55:26 +0200
Subject: [PATCH 5/5] Fixed unicode space in doc
---
lib/diplomat/node.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/diplomat/node.rb b/lib/diplomat/node.rb
index 7f5028b..6188fb7 100644
--- a/lib/diplomat/node.rb
+++ b/lib/diplomat/node.rb
@@ -27,7 +27,7 @@ def get(key, options = nil)
# Get all the nodes
# @param options [Hash] Options to use when performing request
- # @option options [String] :dc string for dc specific query
+ # @option options [String] :dc string for dc specific query
# @option options [String] :consistency The read consistency type
# @return [OpenStruct] the list of all nodes
def get_all(options = nil)