Skip to content

Commit

Permalink
Idsvr uriprovider (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
AroglDarthu authored Jul 5, 2022
1 parent c36cda2 commit b3efef8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,41 @@ public static IHealthChecksBuilder AddIdentityServer(

builder.Services.AddHttpClient(registrationName, client => client.BaseAddress = idSvrUri);

return builder.Add(new HealthCheckRegistration(
registrationName,
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName)),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for Identity Server.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="uriProvider">Factory for providing the uri of the Identity Server to check.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'idsvr' will be used for the name.</param>
/// <param name="failureStatus"></param>
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddIdentityServer(
this IHealthChecksBuilder builder,
Func<IServiceProvider, Uri> uriProvider,
string? name = null,
HealthStatus? failureStatus = null,
IEnumerable<string>? tags = null,
TimeSpan? timeout = null)
{
var registrationName = name ?? NAME;

builder.Services.AddHttpClient(registrationName, (sp, client) =>
{
var idSvrUri = uriProvider(sp);
client.BaseAddress = idSvrUri;
});

return builder.Add(new HealthCheckRegistration(
registrationName,
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ public void add_named_health_check_when_properly_configured()
var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("my-idsvr-group");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
[Fact]
public void add_health_check_when_properly_configured_with_uri_provider()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddIdentityServer(sp => new Uri("http://myidsvr"));

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("idsvr");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
[Fact]
public void add_named_health_check_when_properly_configured_with_uri_provider()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddIdentityServer(sp => new Uri("http://myidsvr"), name: "my-idsvr-group");

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("my-idsvr-group");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class IdSvrHealthCheckBuilderExtensions
{
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func<System.IServiceProvider, System.Uri> uriProvider, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Uri idSvrUri, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
}
}

0 comments on commit b3efef8

Please sign in to comment.