Skip to content

Commit

Permalink
Support Listing and Deleting Configuration Entries (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammychinedu2ky authored Nov 24, 2023
1 parent 6a62fb5 commit 5da7a42
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Consul.Test/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,68 @@ public async Task Configuration_ApplyConfig(string protocol)
Assert.Equal(payload.Kind, queryResult.Response.Kind);
Assert.Equal(payload.Protocol, queryResult.Response.Protocol);
}

[Fact]
public async Task Configuration_ListConfig()
{
var firstPayload = new ServiceDefaultsEntry
{
Kind = "service-defaults",
Name = "web",
Protocol = "https"
};

var secondPayload = new ServiceDefaultsEntry
{
Kind = "service-defaults",
Name = "db",
Protocol = "https"
};
var writeResult = await _client.Configuration.ApplyConfig(firstPayload);
Assert.Equal(HttpStatusCode.OK, writeResult.StatusCode);
writeResult = await _client.Configuration.ApplyConfig(secondPayload);
Assert.Equal(HttpStatusCode.OK, writeResult.StatusCode);
var queryResult = await _client.Configuration.ListConfig<ServiceDefaultsEntry>(firstPayload.Kind);
var configurations = queryResult.Response;
var webConfig = configurations.SingleOrDefault(c => c.Name == firstPayload.Name);
var dbConfig = configurations.SingleOrDefault(c => c.Name == secondPayload.Name);
Assert.NotNull(dbConfig);
Assert.NotNull(webConfig);

Assert.Equal(firstPayload.Name, webConfig.Name);
Assert.Equal(firstPayload.Kind, webConfig.Kind);
Assert.Equal(firstPayload.Protocol, webConfig.Protocol);

Assert.Equal(secondPayload.Name, dbConfig.Name);
Assert.Equal(secondPayload.Kind, dbConfig.Kind);
Assert.Equal(secondPayload.Protocol, dbConfig.Protocol);
}

[Fact]
public async Task Configuration_DeleteConfig()
{
var payload = new ServiceDefaultsEntry
{
Kind = "service-defaults",
Name = "test-service",
Protocol = "http"
};

var writeResult = await _client.Configuration.ApplyConfig(payload);
Assert.Equal(HttpStatusCode.OK, writeResult.StatusCode);

var getConfigResult = await _client.Configuration.GetConfig<ServiceDefaultsEntry>(payload.Kind, payload.Name);
Assert.Equal(HttpStatusCode.OK, getConfigResult.StatusCode);
Assert.Equal(payload.Name, getConfigResult.Response.Name);
Assert.Equal(payload.Kind, getConfigResult.Response.Kind);
Assert.Equal(payload.Protocol, getConfigResult.Response.Protocol);

var deleteResult = await _client.Configuration.DeleteConfig(payload.Kind, payload.Name);
Assert.Equal(HttpStatusCode.OK, deleteResult.StatusCode);

var getDeletedConfigResult = await _client.Configuration.GetConfig<ServiceDefaultsEntry>(payload.Kind, payload.Name);
Assert.Equal(HttpStatusCode.NotFound, getDeletedConfigResult.StatusCode);
Assert.Null(getDeletedConfigResult.Response);
}
}
}
53 changes: 53 additions & 0 deletions Consul/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,59 @@ public Task<QueryResult<TConfig>> GetConfig<TConfig>(string kind, string name, C
{
return GetConfig<TConfig>(kind, name, QueryOptions.Default, ct);
}

/// <summary>
/// This Retrieves the list of config for an entry kind.
/// </summary>
/// <typeparam name="TConfig"></typeparam>
/// <param name="kind">The kind of config entry</param>
/// <param name="q">Query Options</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>A config entry</returns>
public Task<QueryResult<List<TConfig>>> ListConfig<TConfig>(string kind, QueryOptions q, CancellationToken ct = default) where TConfig : IConfigurationEntry
{
var req = _client.Get<List<TConfig>>($"/v1/config/{kind}", q);
return req.Execute(ct);
}
/// <summary>
/// This Retrieves the list of config for an entry kind.
/// </summary>
/// <typeparam name="TConfig"></typeparam>
/// <param name="kind">The kind of config entry</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>A list of config entries</returns>
public Task<QueryResult<List<TConfig>>> ListConfig<TConfig>(string kind, CancellationToken ct = default) where TConfig : IConfigurationEntry
{
return ListConfig<TConfig>(kind, QueryOptions.Default, ct);
}

/// <summary>
/// This Deletes the given config entry.
/// </summary>
/// <typeparam name="TConfig"></typeparam>
/// <param name="kind">The kind of config entry</param>
/// <param name="name">The name of config entry</param>
/// <param name="q">Write Options</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>A Write Result</returns>
public Task<WriteResult> DeleteConfig(string kind, string name, WriteOptions q, CancellationToken ct = default)
{
var req = _client.Delete($"/v1/config/{kind}/{name}", q);
return req.Execute(ct);
}

/// <summary>
/// This Deletes the given config entry.
/// </summary>
/// <typeparam name="TConfig"></typeparam>
/// <param name="kind">The kind of config entry</param>
/// <param name="name">The name of config entry</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>A config entry</returns>
public Task<WriteResult> DeleteConfig(string kind, string name, CancellationToken ct = default)
{
return DeleteConfig(kind, name, WriteOptions.Default, ct);
}
}
public partial class ConsulClient : IConsulClient
{
Expand Down
4 changes: 4 additions & 0 deletions Consul/Interfaces/IConfigurationEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ public interface IConfigurationEndpoint
Task<WriteResult> ApplyConfig<TConfig>(TConfig configurationEntry, CancellationToken ct = default) where TConfig : IConfigurationEntry;
Task<QueryResult<TConfig>> GetConfig<TConfig>(string kind, string name, QueryOptions q, CancellationToken ct = default) where TConfig : IConfigurationEntry;
Task<QueryResult<TConfig>> GetConfig<TConfig>(string kind, string name, CancellationToken ct = default) where TConfig : IConfigurationEntry;
Task<QueryResult<List<TConfig>>> ListConfig<TConfig>(string kind, QueryOptions q, CancellationToken ct = default) where TConfig : IConfigurationEntry;
Task<QueryResult<List<TConfig>>> ListConfig<TConfig>(string kind, CancellationToken ct = default) where TConfig : IConfigurationEntry;
Task<WriteResult> DeleteConfig(string kind, string name, WriteOptions q, CancellationToken ct = default);
Task<WriteResult> DeleteConfig(string kind, string name, CancellationToken ct = default);
}
}

0 comments on commit 5da7a42

Please sign in to comment.