Skip to content

Commit

Permalink
Enable configuration override on each consul api call
Browse files Browse the repository at this point in the history
Using the configs hash target host or token can be
different at each call
  • Loading branch information
tionebsalocin committed Mar 7, 2019
1 parent 0f80781 commit 372b832
Show file tree
Hide file tree
Showing 31 changed files with 617 additions and 708 deletions.
1 change: 1 addition & 0 deletions diplomat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new 'diplomat', Diplomat::VERSION do |spec|
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'rubocop', '~> 0.49'
spec.add_development_dependency 'webmock'

spec.add_runtime_dependency 'deep_merge', '~> 1.0', '>= 1.0.1'
spec.add_runtime_dependency 'faraday', '~> 0.9'
Expand Down
49 changes: 20 additions & 29 deletions lib/diplomat/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ class Acl < Diplomat::RestClient

# Get Acl info by ID
# @param id [String] ID of the Acl to get
# @param options [Hash] options parameter hash
# @return [Hash]
# rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
def info(id, options = nil, not_found = :reject, found = :return)
@id = id
@options = options
url = ["/v1/acl/info/#{id}"]
url << check_acl_token
url << use_consistency(options)
custom_params = []
custom_params << use_consistency(options)

raw = send_get_request(@conn_no_err, ["/v1/acl/info/#{id}"], options, custom_params)

raw = @conn_no_err.get concat_url url
if raw.status == 200 && raw.body.chomp != 'null'
case found
when :reject
Expand All @@ -38,52 +39,42 @@ def info(id, options = nil, not_found = :reject, found = :return)
# rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize

# List all Acls
# @param options [Hash] options parameter hash
# @return [List] list of [Hash] of Acls
def list
url = ['/v1/acl/list']
url += check_acl_token
@raw = @conn_no_err.get concat_url url
def list(options = nil)
@raw = send_get_request(@conn_no_err, ['/v1/acl/list'], options)
parse_body
end

# Update an Acl definition, create if not present
# @param value [Hash] Acl definition, ID field is mandatory
# @param options [Hash] options parameter hash
# @return [Hash] The result Acl
def update(value)
raise Diplomat::IdParameterRequired unless value['ID']
def update(value, options = nil)
raise Diplomat::IdParameterRequired unless value['ID'] || value[:ID]

@raw = @conn.put do |req|
url = ['/v1/acl/update']
url += check_acl_token
url += use_cas(@options)
req.url concat_url url
req.body = value.to_json
end
custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/update'], options, value, custom_params)
parse_body
end

# Create an Acl definition
# @param value [Hash] Acl definition, ID field is mandatory
# @param options [Hash] options parameter hash
# @return [Hash] The result Acl
def create(value)
@raw = @conn.put do |req|
url = ['/v1/acl/create']
url += check_acl_token
url += use_cas(@options)
req.url concat_url url
req.body = value.to_json
end
def create(value, options = nil)
custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/create'], options, value, custom_params)
parse_body
end

# Destroy an ACl token by its id
# @param ID [String] the Acl ID
# @param options [Hash] options parameter hash
# @return [Bool]
def destroy(id)
def destroy(id, options = nil)
@id = id
url = ["/v1/acl/destroy/#{@id}"]
url << check_acl_token
@raw = @conn.put concat_url url
@raw = send_put_request(@conn, ["/v1/acl/destroy/#{@id}"], options, nil)
@raw.body.chomp == 'true'
end
end
Expand Down
52 changes: 12 additions & 40 deletions lib/diplomat/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,34 @@ class Agent < Diplomat::RestClient
@access_methods = %i[self checks services members]

# Get agent configuration
# @param options [Hash] options parameter hash
# @return [OpenStruct] all data associated with the node
def self
url = ['/v1/agent/self']

# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
raise Diplomat::PathNotFound
end
def self(options = nil)
ret = send_get_request(@conn, ['/v1/agent/self'], options)
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
end

# Get local agent checks
# @param options [Hash] options parameter hash
# @return [OpenStruct] all agent checks
def checks
url = ['/v1/agent/checks']

# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
raise Diplomat::PathNotFound
end
def checks(options = nil)
ret = send_get_request(@conn, ['/v1/agent/checks'], options)
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
end

# Get local agent services
# @param options [Hash] options parameter hash
# @return [OpenStruct] all agent services
def services
url = ['/v1/agent/services']

# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
raise Diplomat::PathNotFound
end
def services(options = nil)
ret = send_get_request(@conn, ['/v1/agent/services'], options)
JSON.parse(ret.body).tap { |node| OpenStruct.new node }
end

# Get cluster members (as seen by the agent)
# @param options [Hash] options parameter hash
# @return [OpenStruct] all members
def members
url = ['/v1/agent/members']

# If the request fails, it's probably due to a bad path
# so return a PathNotFound error.
begin
ret = @conn.get concat_url url
rescue Faraday::ClientError
raise Diplomat::PathNotFound
end
def members(options = nil)
ret = send_get_request(@conn, ['/v1/agent/members'], options)
JSON.parse(ret.body).map { |node| OpenStruct.new node }
end
end
Expand Down
77 changes: 46 additions & 31 deletions lib/diplomat/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,104 @@ class Check < Diplomat::RestClient

# Get registered checks
# @return [OpenStruct] all data associated with the service
def checks
ret = @conn.get '/v1/agent/checks'
def checks(options = nil)
ret = send_get_request(@conn, ['/v1/agent/checks'], options)
JSON.parse(ret.body)
end

# Register a check
# @param check_id [String] the unique id of the check
# @param name [String] the name
# @param notes [String] notes about the check
# @param script [String] command to be run for check
# @param args [String[]] command to be run for check
# @param interval [String] frequency (with units) of the check execution
# @param ttl [String] time (with units) to mark a check down
# @param options [Hash] options parameter hash
# @return [Integer] Status code
#
def register_script(check_id, name, notes, script, interval)
ret = @conn.put do |req|
req.url '/v1/agent/check/register'
req.body = JSON.generate(
'ID' => check_id, 'Name' => name, 'Notes' => notes, 'Script' => script, 'Interval' => interval
)
# rubocop:disable MethodLength, ParameterLists
def register_script(check_id, name, notes, args, interval, options = nil)
unless args.is_a?(Array)
raise(Diplomat::DeprecatedArgument, 'Script usage is deprecated, replace by an array of args')
end

definition = {
'ID' => check_id,
'Name' => name,
'Notes' => notes,
'Args' => args,
'Interval' => interval
}
ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
ret.status == 200
end
# rubocop:enable MethodLength, ParameterLists

# Register a TTL check
# @param check_id [String] the unique id of the check
# @param name [String] the name
# @param notes [String] notes about the check
# @param ttl [String] time (with units) to mark a check down
# @param options [Hash] options parameter hash
# @return [Boolean] Success
def register_ttl(check_id, name, notes, ttl)
ret = @conn.put do |req|
req.url '/v1/agent/check/register'
req.body = JSON.generate(
'ID' => check_id, 'Name' => name, 'Notes' => notes, 'TTL' => ttl
)
end
def register_ttl(check_id, name, notes, ttl, options = nil)
definition = {
'ID' => check_id,
'Name' => name,
'Notes' => notes,
'TTL' => ttl
}
ret = send_put_request(@conn, ['/v1/agent/check/register'], options, definition)
ret.status == 200
end

# Deregister a check
# @param check_id [String] the unique id of the check
# @param options [Hash] options parameter hash
# @return [Integer] Status code
def deregister(check_id)
ret = @conn.put "/v1/agent/check/deregister/#{check_id}"
def deregister(check_id, options = nil)
ret = send_put_request(@conn, ["/v1/agent/check/deregister/#{check_id}"], options, nil)
ret.status == 200
end

# Update a TTL check
# @param check_id [String] the unique id of the check
# @param status [String] status of the check. Valid values are "passing", "warning", and "critical"
# @param output [String] human-readable message will be passed through to the check's Output field
# @param options [Hash] options parameter hash
# @return [Integer] Status code
def update_ttl(check_id, status, output = nil)
ret = @conn.put do |req|
req.url "/v1/agent/check/update/#{check_id}"
req.body = JSON.generate('Status' => status, 'Output' => output)
end
def update_ttl(check_id, status, output = nil, options = nil)
definition = {
'Status' => status,
'Output' => output
}
ret = send_put_request(@conn, ["/v1/agent/check/update/#{check_id}"], options, definition)
ret.status == 200
end

# Pass a check
# @param check_id [String] the unique id of the check
# @param output [String] human-readable message will be passed through to the check's Output field
# @param options [Hash] options parameter hash
# @return [Integer] Status code
def pass(check_id, output = nil)
update_ttl(check_id, 'passing', output)
def pass(check_id, output = nil, options = nil)
update_ttl(check_id, 'passing', output, options)
end

# Warn a check
# @param check_id [String] the unique id of the check
# @param output [String] human-readable message will be passed through to the check's Output field
# @param options [Hash] options parameter hash
# @return [Integer] Status code
def warn(check_id, output = nil)
update_ttl(check_id, 'warning', output)
def warn(check_id, output = nil, options = nil)
update_ttl(check_id, 'warning', output, options)
end

# Fail a check
# @param check_id [String] the unique id of the check
# @param output [String] human-readable message will be passed through to the check's Output field
# @param options [Hash] options parameter hash
# @return [Integer] Status code
def fail(check_id, output = nil)
update_ttl(check_id, 'critical', output)
def fail(check_id, output = nil, options = nil)
update_ttl(check_id, 'critical', output, options)
end
end
end
18 changes: 9 additions & 9 deletions lib/diplomat/datacenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ class Datacenter < Diplomat::RestClient
@access_methods = [:get]

# 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 parameter hash
# @return [OpenStruct] all datacenters avaliable to this consul agent
def get(meta = nil)
url = ['/v1/catalog/datacenters']
# rubocop:disable AbcSize, CyclomaticComplexity
def get(options = nil)
ret = send_get_request(@conn, ['/v1/catalog/datacenters'], options)

ret = @conn.get concat_url url

if meta && ret.headers
meta[:index] = ret.headers['x-consul-index']
meta[:knownleader] = ret.headers['x-consul-knownleader']
meta[:lastcontact] = ret.headers['x-consul-lastcontact']
if options && options[:meta] && ret.headers
options[:meta][:index] = ret.headers['x-consul-index'] if ret.headers['x-consul-index']
options[:meta][:knownleader] = ret.headers['x-consul-knownleader'] if ret.headers['x-consul-knownleader']
options[:meta][:lastcontact] = ret.headers['x-consul-lastcontact'] if ret.headers['x-consul-lastcontact']
end
JSON.parse(ret.body)
end
# rubocop:enable AbcSize, CyclomaticComplexity
end
end
1 change: 1 addition & 0 deletions lib/diplomat/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class QueryAlreadyExists < StandardError; end
class UnknownStatus < StandardError; end
class IdParameterRequired < StandardError; end
class InvalidTransaction < StandardError; end
class DeprecatedArgument < StandardError; end
end
Loading

0 comments on commit 372b832

Please sign in to comment.