Skip to content

Commit

Permalink
feat: add "List Service Instances for Service" call (#8)
Browse files Browse the repository at this point in the history
This should be the minimum required for querying the list of healthy &
unhealthy instances for a given service.
  • Loading branch information
ericnorris authored Jun 6, 2024
1 parent 42b5cad commit d115001
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/consulkit/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'consulkit/configurable'
require 'consulkit/client/health'
require 'consulkit/client/kv'
require 'consulkit/client/session'

Expand All @@ -9,6 +10,7 @@ module Consulkit
class Client

include Consulkit::Configurable
include Consulkit::Client::Health
include Consulkit::Client::KV
include Consulkit::Client::Session

Expand Down
33 changes: 33 additions & 0 deletions lib/consulkit/client/health.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Consulkit
class Client
# Methods for querying health checks registered with Consul.
module Health

# Returns the list of service instances providing the given service, including health check
# information.
#
# @see https://developer.hashicorp.com/consul/api-docs/health#list-service-instances-for-service
#
# @param key [String] the key to read.
# @option query_params [Hash] optional query parameters.
# @option query_params [Boolean] :passing
# @option query_params [String] :filter
#
# @yield [Faraday::Response] The response from the underlying Faraday library.
#
# @return [Array<Hash>]
def health_list_service_instances(service, query_params = {})
response = get("/v1/health/service/#{service}", query_params)

if block_given?
yield response
else
response.body
end
end

end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions spec/consulkit/client/health_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

describe Consulkit::Client::Health, :vcr do
before do
@client = Consulkit.client
@service = 'consul'
end

describe 'health_list_service_instances' do
it 'lists the instances' do
instances = @client.health_list_service_instances(@service)

expect(instances.length).to be 1

first_instance = instances.first

expect(first_instance.dig('Node', 'Node')).to eq('localhost')
expect(first_instance.dig('Service', 'ID')).to eq('consul')
expect(first_instance.dig('Checks', 0, 'CheckID')).to eq('serfHealth')
end

it 'passes query params' do
instances = @client.health_list_service_instances(@service, filter: 'Checks.Status == "passing"')

expect(instances.length).to be 1

first_instance = instances.first

expect(first_instance.dig('Node', 'Node')).to eq('localhost')
expect(first_instance.dig('Service', 'ID')).to eq('consul')
expect(first_instance.dig('Checks', 0, 'CheckID')).to eq('serfHealth')

instances = @client.health_list_service_instances(@service, filter: 'Checks.Status != "passing"')

expect(instances.length).to be 0
end
end
end

0 comments on commit d115001

Please sign in to comment.