Skip to content
53 changes: 49 additions & 4 deletions Consul.Test/HealthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Consul.Filtering;
using Xunit;

namespace Consul.Test
Expand Down Expand Up @@ -81,10 +82,10 @@ public async Task Health_GetServiceWithTaggedAddresses()
Name = svcID,
Port = 8000,
TaggedAddresses = new Dictionary<string, ServiceTaggedAddress>
{
{"lan", new ServiceTaggedAddress {Address = "127.0.0.1", Port = 80}},
{"wan", new ServiceTaggedAddress {Address = "192.168.10.10", Port = 8000}}
}
{
{"lan", new ServiceTaggedAddress {Address = "127.0.0.1", Port = 80}},
{"wan", new ServiceTaggedAddress {Address = "192.168.10.10", Port = 8000}}
}
};

await _client.Agent.ServiceRegister(registration);
Expand Down Expand Up @@ -116,6 +117,50 @@ private struct AggregatedStatusResult

}

[Fact]
public async Task Health_Connect()
{
var destinationServiceID = KVTest.GenerateTestKeyName();

var registration = new AgentServiceRegistration
{
ID = destinationServiceID,
Name = destinationServiceID,
Port = 8000,
Check = new AgentServiceCheck
{
TTL = TimeSpan.FromSeconds(15)
},
Connect = new AgentServiceConnect
{
SidecarService = new AgentServiceRegistration
{
Port = 8001
}
}
};

try
{
await _client.Agent.ServiceRegister(registration);

// Use the Health.Connect method to query health information for Connect-enabled services
var checks = await _client.Health.Connect(destinationServiceID, "", false, QueryOptions.Default, null); // Passing null for the filter parameter

Assert.NotNull(checks);
Assert.NotEqual((ulong)0, checks.LastIndex);
Assert.NotEmpty(checks.Response);
}
finally
{
await _client.Agent.ServiceDeregister(destinationServiceID);
}
}





Copy link
Contributor

Choose a reason for hiding this comment

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

nit: ```suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the whitespaces on the last commit.

[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>
/// Connect is equivalent to Service, except that it will only return services which are Connect-enabled
/// <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 connection address for Connect client's to use which may be a proxy in front of the named service</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