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

Add support for replace-existing-checks parameter #155

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
* Add support for `replace-existing-checks` parameter of service registration endpoint that allows to replace existing checks when re-registering a service

## 1.6.10.6
* Enforce ConfigureAwait(false) on the whole library (#148)
Expand Down
58 changes: 58 additions & 0 deletions Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,5 +544,63 @@ public async Task Agent_FilterServices()
Assert.Equal(svcID1, (await _client.Agent.Services(metaSelector[uniqueMeta] == "bar1")).Response.Keys.Single());
Assert.Equal(svcID2, (await _client.Agent.Services(metaSelector[uniqueMeta] == "bar2")).Response.Keys.Single());
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Agent_ReRegister_ReplaceExistingChecks(bool replaceExistingChecks)
{
var svcID = KVTest.GenerateTestKeyName();
var check1Name = svcID + "1";
var check2Name = svcID + "2";
var check3Name = svcID + "3";
var registration1 = new AgentServiceRegistration
{
Name = svcID,
Port = 8000,
Checks = new[]
{
new AgentServiceCheck
{
Name = check1Name,
TTL = TimeSpan.FromSeconds(15)
},
new AgentServiceCheck
{
Name = check2Name,
TTL = TimeSpan.FromSeconds(15)
}
}
};
var registration2 = new AgentServiceRegistration
{
Name = svcID,
Port = 8000,
Check = new AgentServiceCheck
{
Name = check3Name,
TTL = TimeSpan.FromSeconds(15)
}
};

await _client.Agent.ServiceRegister(registration1);
await _client.Agent.ServiceRegister(registration2, replaceExistingChecks: replaceExistingChecks);

var checks = await _client.Agent.Checks();

if (replaceExistingChecks)
{
Assert.DoesNotContain(check1Name, checks.Response.Values.Select(c => c.Name));
Assert.DoesNotContain(check2Name, checks.Response.Values.Select(c => c.Name));
}
else
{
Assert.Contains(check1Name, checks.Response.Values.Select(c => c.Name));
Assert.Contains(check2Name, checks.Response.Values.Select(c => c.Name));
}
Assert.Contains(check3Name, checks.Response.Values.Select(c => c.Name));

await _client.Agent.ServiceDeregister(svcID);
}
}
}
18 changes: 17 additions & 1 deletion Consul/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,23 @@ public string NodeName
/// <returns>An empty write result</returns>
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, CancellationToken ct = default(CancellationToken))
{
return _client.Put("/v1/agent/service/register", service, null).Execute(ct);
return ServiceRegister(service, replaceExistingChecks: false, ct);
}

/// <summary>
/// ServiceRegister is used to register a new service with the local agent
/// </summary>
/// <param name="service">A service registration object</param>
/// <param name="replaceExistingChecks">Missing health checks from the request will be deleted from the agent.</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, bool replaceExistingChecks, CancellationToken ct = default(CancellationToken))
{
var req = _client.Put("/v1/agent/service/register", service, null);
if (replaceExistingChecks)
{
req.Params["replace-existing-checks"] = "true";
}
return req.Execute(ct);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Consul/Interfaces/IAgentEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface IAgentEndpoint
Task<QueryResult<Dictionary<string, Dictionary<string, dynamic>>>> Self(CancellationToken ct = default(CancellationToken));
Task<WriteResult> ServiceDeregister(string serviceID, CancellationToken ct = default(CancellationToken));
Task<WriteResult> ServiceRegister(AgentServiceRegistration service, CancellationToken ct = default(CancellationToken));
Task<WriteResult> ServiceRegister(AgentServiceRegistration service, bool replaceExistingChecks, CancellationToken ct = default(CancellationToken));
Task<QueryResult<Dictionary<string, AgentService>>> Services(CancellationToken ct = default(CancellationToken));
Task<QueryResult<Dictionary<string, AgentService>>> Services(Filter filter, CancellationToken ct = default(CancellationToken));
Task<WriteResult> UpdateTTL(string checkID, string output, TTLStatus status, CancellationToken ct = default(CancellationToken));
Expand Down