Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using specific consitency on all methods #165

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,3 +17,6 @@ Metrics/LineLength:
# Allow classes longer than 100 lines of code
ClassLength:
Max: 250

Naming/UncommunicativeMethodParamName:
Enabled: false
4 changes: 2 additions & 2 deletions lib/diplomat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/diplomat/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 0 additions & 4 deletions lib/diplomat/api_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/diplomat/datacenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment / Doc shall be updated to take into account the new param

# @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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use 'if use_consistency' everywhere while it could be integrated into use_consistency (or a wrapper of both...) it's not very conservative if you forget one use_consistency call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it in next patch


ret = @conn.get concat_url url

Expand Down
4 changes: 4 additions & 0 deletions lib/diplomat/health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/diplomat/kv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need to add a logging message

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FAR if someone wants to do it, I don't change the behaviour here, just fix rubicop since JSON parsing and base64 are StandardError

end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/diplomat/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/diplomat/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion lib/diplomat/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion lib/diplomat/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lib/diplomat/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def use_named_parameter(name, value)
value ? ["#{name}=#{value}"] : []
end

def use_consistency(options, default_value = [])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the point of having a default value if it's always set to nil, and checked if its value is not nil... Better just remove the if use_consistency(options, nil) and use the default value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was to avoid modifying lib/diplomat/kv.rb, but I did the other way around finally in next patch

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
Expand Down Expand Up @@ -143,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
Expand Down
3 changes: 2 additions & 1 deletion lib/diplomat/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +115,5 @@ def maintenance(service_id, options = { enable: true })
end
maintenance.status == 200
end
# rubocop:enable AbcSize
end
end