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 overload for SFTP Service Provider #2134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ public static IHealthChecksBuilder AddSftpHealthCheck(
timeout));
}


/// <summary>
/// Add a health check for network SFTP.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="sftpConfigurationFactory">A factory to build the SftpConfiguration to use.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'sftp' will be used for the name.</param>
/// <param name="failureStatus">
/// 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>
/// <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 AddSftpHealthCheck(
this IHealthChecksBuilder builder,
Func<IServiceProvider, SftpConfiguration> sftpConfigurationFactory,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? SFTP_NAME,
sp =>
{
var options = new SftpHealthCheckOptions();
var sftpConfiguration = sftpConfigurationFactory.Invoke(sp);
options.AddHost(sftpConfiguration);
return new SftpHealthCheck(options);
},
failureStatus,
tags,
timeout));
}

/// <summary>
/// Add a health check for network FTP.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Renci.SshNet;

namespace HealthChecks.Network.Tests.DependencyInjection;

public class network_registration_should
Expand Down Expand Up @@ -66,6 +68,30 @@ public void add_named_sftp_health_check_when_properly_configured()
registration.Name.ShouldBe("my-sftp-1");
check.ShouldBeOfType<SftpHealthCheck>();
}

[Fact]
public void add_health_check_with_sftp_configuration_factory_when_properly_configured()
{
var services = new ServiceCollection();
bool factoryCalled = false;
services.AddHealthChecks()
.AddSftpHealthCheck(_ =>
{
factoryCalled = true;
return new SftpConfiguration("host",22,"uName", new List<AuthenticationMethod>());
});

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

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

registration.Name.ShouldBe("sftp");
check.ShouldBeOfType<SftpHealthCheck>();
factoryCalled.ShouldBeTrue();
}

[Fact]
public void add_ftp_check_when_properly_configured()
{
Expand Down