-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "List Service Instances for Service" call (#8)
This should be the minimum required for querying the list of healthy & unhealthy instances for a given service.
- Loading branch information
1 parent
42b5cad
commit d115001
Showing
5 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
119 changes: 119 additions & 0 deletions
119
spec/cassettes/Consulkit_Client_Health/health_list_service_instances/lists_the_instances.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
161 changes: 161 additions & 0 deletions
161
spec/cassettes/Consulkit_Client_Health/health_list_service_instances/passes_query_params.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |