Skip to content
27 changes: 27 additions & 0 deletions Consul.Test/HealthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ private struct AggregatedStatusResult

}

[Fact]
public async Task Health_Connect()
{
var svcID = KVTest.GenerateTestKeyName();
var registration = new AgentServiceRegistration
{
Name = svcID,
Tags = new[] { "bar", "baz" },
Port = 8000,
Check = new AgentServiceCheck
{
TTL = TimeSpan.FromSeconds(15)
}
};
try
{
await _client.Agent.ServiceRegister(registration);
var checks = await _client.Health.Connect(svcID, "", false, QueryOptions.Default);
Assert.NotEqual((ulong)0, checks.LastIndex);
Assert.NotEmpty(checks.Response);
}
finally
{
await _client.Agent.ServiceDeregister(svcID);
}
}

[Fact]
public void Health_GetAggregatedStatus()
{
Expand Down
37 changes: 37 additions & 0 deletions Consul/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,43 @@ public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, boo
return req.Execute(ct);
}

/// <summary>
/// Service is used to query health information along with service info for a given service. It can optionally do server-side filtering on a tag or nodes with passing health checks only.
/// <param name="service">The service ID</param>
/// <param name="tag">The service member tag</param>
/// <param name="passingOnly">Only return if the health check is in the Passing state</param>
/// <param name="q">Customized query options</param>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>This endpoint returns the nodes providing a Connect-capable service in a given datacenter, or a query result with a null response</returns>
public Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default)
{
var req = _client.Get<ServiceEntry[]>(string.Format("/v1/health/connect/{0}", service), q, filter);
if (!string.IsNullOrEmpty(tag))
{
req.Params["tag"] = tag;
}
if (passingOnly)
{
req.Params["passing"] = "1";
}
return req.Execute(ct);
}

/// <summary>
/// Service is used to query health information along with service info for a given service. It can optionally do server-side filtering on a tag or nodes with passing health checks only.
/// <param name="service">The service ID</param>
/// <param name="tag">The service member tag</param>
/// <param name="passingOnly">Only return if the health check is in the Passing state</param>
/// <param name="q">Customized query options</param>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>This endpoint returns the nodes providing a Connect-capable service in a given datacenter, or a query result with a null response</returns>
public Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default)
{
return Connect(service, tag, passingOnly, q, null, ct);
}

/// <summary>
/// State is used to retrieve all the checks in a given state. The wildcard "any" state can also be used for all checks.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Consul/Interfaces/IHealthEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface IHealthEndpoint
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<HealthCheck[]>> State(HealthStatus status, CancellationToken ct = default);
Task<QueryResult<HealthCheck[]>> State(HealthStatus status, QueryOptions q, CancellationToken ct = default);
}
Expand Down