Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding features #219

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,5 +793,42 @@ public async Task Agent_Service_Register_With_Connect()
Assert.Null(destinationProxyService.Proxy.Upstreams);
Assert.Equal(ServiceKind.ConnectProxy, destinationProxyService.Kind);
}

[Fact]
public async Task Agent_FilterChecks()
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

{
// Arrange
string svcName = KVTest.GenerateTestKeyName();
string svcID = $"{svcName}-1";
string checkName = $"Check {svcID}";

await _client.Agent.ServiceRegister(new AgentServiceRegistration
{
Name = svcName,
ID = svcID,
Check = new AgentServiceCheck
{
TTL = TimeSpan.FromSeconds(15),
Name = checkName
}
});

// mass service
await _client.Agent.ServiceRegister(new AgentServiceRegistration
{
Name = KVTest.GenerateTestKeyName(),
Check = new AgentServiceCheck
{
TTL = TimeSpan.FromSeconds(15),
}
});

// Act
Dictionary<string, AgentCheck> actual = (await _client.Agent.Checks(Selectors.ServiceName == svcName)).Response;

// Assert
Assert.Single(actual);
Assert.Equal(checkName, actual.Values.First().Name);
}
}
}
12 changes: 12 additions & 0 deletions Consul.Test/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ public void Filter_Selectors_Service()
CheckEncoded("Service", S.Service);
}

[Fact]
public void Filter_ServiceSelector_Equals()
{
CheckEncoded("Service == \"serviceName\"", S.Service == ServiceConstants.ServiceName);
}

[Fact]
public void Filter_ServiceSelector_NotEquals()
{
CheckEncoded("Service != \"serviceName\"", S.Service != ServiceConstants.ServiceName);
}

[Fact]
public void Filter_ServiceMetaEntrySelector()
{
Expand Down
12 changes: 11 additions & 1 deletion Consul/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,17 @@ public string NodeName
/// <returns>A map of the registered check names and check data</returns>
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(CancellationToken ct = default(CancellationToken))
{
return _client.Get<Dictionary<string, AgentCheck>>("/v1/agent/checks").Execute(ct);
return Checks(null, ct);
}

/// <summary>
/// Checks returns the locally registered checks
/// </summary>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <returns>A map of the registered check names and check data</returns>
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(Filter filter, CancellationToken ct = default(CancellationToken))
{
return _client.Get<Dictionary<string, AgentCheck>>("/v1/agent/checks", filter: filter).Execute(ct);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Consul/Filtering/Selectors/Selectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ namespace Consul.Filtering
public static class Selectors
{
public static ServiceSelector Service { get; } = new ServiceSelector();
public static StringFieldSelector ServiceName { get; } = new StringFieldSelector(nameof(ServiceName));
}
}
5 changes: 4 additions & 1 deletion Consul/Filtering/Selectors/ServiceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#pragma warning disable CS0661 // Type defines operator == or operator != but does not override Object.GetHashCode()
namespace Consul.Filtering
{
public sealed class ServiceSelector : Selector
public sealed class ServiceSelector : Selector, IEqualsApplicableConstraint
{
private static readonly string Self = "Service";

Expand All @@ -29,6 +29,9 @@ public sealed class ServiceSelector : Selector
public MetaSelector Meta { get; } = new MetaSelector(Self);

public override string Encode() => Self;

public static Filter operator ==(ServiceSelector selector, string value) => Filters.Eq(selector, value);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think a "service" is a valid selector that can be used for anything. Also, this is not being used in an actual test (apart from encoding tests). I think that it should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I see. It would be great though if you implement at least one test case which uses this APIs.

public static Filter operator !=(ServiceSelector selector, string value) => Filters.NotEq(selector, value);
}
}
#pragma warning restore CS0661 // Type defines operator == or operator != but does not override Object.GetHashCode()
Expand Down
1 change: 1 addition & 0 deletions Consul/Interfaces/IAgentEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface IAgentEndpoint
Task<WriteResult> CheckDeregister(string checkID, CancellationToken ct = default(CancellationToken));
Task<WriteResult> CheckRegister(AgentCheckRegistration check, CancellationToken ct = default(CancellationToken));
Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(CancellationToken ct = default(CancellationToken));
Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(Filter filter, CancellationToken ct = default(CancellationToken));
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Task<WriteResult> DisableNodeMaintenance(CancellationToken ct = default(CancellationToken));
Task<WriteResult> DisableServiceMaintenance(string serviceID, CancellationToken ct = default(CancellationToken));
Task<WriteResult> EnableNodeMaintenance(string reason, CancellationToken ct = default(CancellationToken));
Expand Down