From 00101a7172ed4a6e53ed9ff1e5b358a14c36993c Mon Sep 17 00:00:00 2001 From: steveski Date: Fri, 7 Mar 2025 20:06:54 +1000 Subject: [PATCH] feat: added an overload for the AddS3 extension method to take the IServiceProvider --- .../S3HealthCheckBuilderExtensions.cs | 36 +++++++ .../DependencyInjection/RegistrationTests.cs | 96 +++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/src/HealthChecks.Aws.S3/DependencyInjection/S3HealthCheckBuilderExtensions.cs b/src/HealthChecks.Aws.S3/DependencyInjection/S3HealthCheckBuilderExtensions.cs index 55d4732361..58c947f867 100644 --- a/src/HealthChecks.Aws.S3/DependencyInjection/S3HealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.Aws.S3/DependencyInjection/S3HealthCheckBuilderExtensions.cs @@ -41,4 +41,40 @@ public static IHealthChecksBuilder AddS3( tags, timeout)); } + + /// + /// Add a health check for AWS S3, passing in the IServiceProvider. + /// The overload makes it easier to pull configuration from appsettings.json or environment variables as you can now call GetRequiredService in the passed in Action + /// + /// The . + /// The action to configure the S3 Configuration e.g. bucket, region etc. + /// The health check name. Optional. If null the type name 'aws s3' will be used for the name. + /// + /// The that should be reported when the health check fails. Optional. If null then + /// the default status of will be reported. + /// + /// A list of tags that can be used to filter sets of health checks. Optional. + /// An optional representing the timeout of the check. + /// The specified . + public static IHealthChecksBuilder AddS3( + this IHealthChecksBuilder builder, + Action? setup, + string? name = default, + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default) + { + return builder.Add(new HealthCheckRegistration( + name ?? NAME + " with sp", + sp => + { + var options = new S3BucketOptions(); + setup?.Invoke(sp, options); + + return new S3HealthCheck(options); + }, + failureStatus, + tags, + timeout)); + } } diff --git a/test/HealthChecks.Aws.S3.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.Aws.S3.Tests/DependencyInjection/RegistrationTests.cs index e3224fd7ea..e6271b3b50 100644 --- a/test/HealthChecks.Aws.S3.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.Aws.S3.Tests/DependencyInjection/RegistrationTests.cs @@ -1,4 +1,5 @@ using Amazon.S3; +using Amazon.Util; namespace HealthChecks.Aws.S3.Tests.DependencyInjection; @@ -87,4 +88,99 @@ public void add_named_health_check_when_properly_configured_2() registration.Name.ShouldBe("my-s3-group"); check.ShouldBeOfType(); } + + + /// + /// Interface representing some potential configuration class to be used by the AddS3 Health Check class. + /// + private interface IS3Config + { + string ServiceUrl { get; set; } + string AccessKey { get; set; } + string SecretKey { get; set; } + + } + + private class S3Config : IS3Config + { + public required string ServiceUrl { get; set; } + public required string AccessKey { get; set; } + public required string SecretKey { get; set; } + } + + + [Fact] + public void add_health_check_when_properly_configured_with_serviceprovider() + { + var services = new ServiceCollection(); + services.AddTransient(p => new S3Config + { + ServiceUrl = "http://localhost:9000", + AccessKey = "access-key", + SecretKey = "secret-key" + }); + + services.AddHealthChecks() + .AddS3((sp, options) => + { +#pragma warning disable CS0618 // Type or member is obsolete + var cfg = sp.GetRequiredService(); + options.AccessKey = cfg.AccessKey; + options.BucketName = "bucket-name"; + options.SecretKey = cfg.SecretKey; + options.S3Config = new AmazonS3Config + { + ServiceURL = cfg.ServiceUrl + }; +#pragma warning restore CS0618 // Type or member is obsolete + }); + + using var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + var check = registration.Factory(serviceProvider); + + registration.Name.ShouldBe("aws s3 with sp"); + check.ShouldBeOfType(); + } + + [Fact] + public void add_named_health_check_when_properly_configured_with_serviceprovider() + { + var services = new ServiceCollection(); + services.AddTransient(p => new S3Config + { + ServiceUrl = "http://localhost:9000", + AccessKey = "access-key", + SecretKey = "secret-key" + }); + + services.AddHealthChecks() + .AddS3((sp, options) => + { +#pragma warning disable CS0618 // Type or member is obsolete + var cfg = sp.GetRequiredService(); + options.AccessKey = cfg.AccessKey; + options.BucketName = "bucket-name"; + options.SecretKey = cfg.SecretKey; + options.S3Config = new AmazonS3Config + { + ServiceURL = cfg.ServiceUrl + }; +#pragma warning restore CS0618 // Type or member is obsolete + }, name: "aws s3 check"); + + using var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + var check = registration.Factory(serviceProvider); + + registration.Name.ShouldBe("aws s3 check"); + check.ShouldBeOfType(); + } + + + }