Skip to content
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
5 changes: 5 additions & 0 deletions src/Plugins/AWSS3/StorageAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.SecurityToken.Model;
using Monai.Deploy.Storage.API;
Expand All @@ -28,6 +29,10 @@ public class StorageAdminService : IStorageAdminService

public Task<Credentials> CreateUserAsync(string username, PolicyRequest[] policyRequests) => throw new NotImplementedException();

public Task<List<string>> GetConnectionAsync() => throw new NotImplementedException();

public Task<bool> HasConnectionAsync() => throw new NotImplementedException();

public Task RemoveUserAsync(string username) => throw new NotImplementedException();
}
}
14 changes: 14 additions & 0 deletions src/Plugins/MinIO/HealthCheckBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Monai.Deploy.Storage.API;

namespace Monai.Deploy.Storage.MinIO
{
Expand All @@ -39,6 +40,19 @@ public override IHealthChecksBuilder Configure(
failureStatus,
tags,
timeout));

builder.Add(new HealthCheckRegistration(
$"{ConfigurationKeys.StorageServiceName}-admin",
serviceProvider =>
{
var logger = serviceProvider.GetRequiredService<ILogger<MinIoAdminHealthCheck>>();
var storageAdminService = serviceProvider.GetRequiredService<IStorageAdminService>();
return new MinIoAdminHealthCheck(storageAdminService, logger);
},
failureStatus,
tags,
timeout));

return builder;
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/Plugins/MinIO/MinIoAdminHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.ObjectModel;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Monai.Deploy.Storage.API;

namespace Monai.Deploy.Storage.MinIO
{
internal class MinIoAdminHealthCheck : IHealthCheck
{
private readonly IStorageAdminService _storageAdminService;
private readonly ILogger<MinIoAdminHealthCheck> _logger;

public MinIoAdminHealthCheck(IStorageAdminService storageAdminService, ILogger<MinIoAdminHealthCheck> logger)
{
_storageAdminService = storageAdminService ?? throw new ArgumentNullException(nameof(storageAdminService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new())
{
try
{
var hasConnection = await _storageAdminService.HasConnectionAsync();
var connectionResult = await _storageAdminService.GetConnectionAsync();
var joinedResult = string.Join("\n", connectionResult);

var roDict = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>() { { "MinoAdminResult", joinedResult } });

if (hasConnection)
{
return HealthCheckResult.Healthy(data: roDict);
}

return HealthCheckResult.Unhealthy(data: roDict);
}
catch (Exception exception)
{
_logger.HealthCheckError(exception);
return HealthCheckResult.Unhealthy(exception: exception);
}
}
}
}
4 changes: 3 additions & 1 deletion src/Plugins/MinIO/StorageAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ private Process CreateProcess(string cmd)

public async Task<bool> HasConnectionAsync()
{
var result = await ExecuteAsync(_get_connections_cmd).ConfigureAwait(false);
var result = await GetConnectionAsync().ConfigureAwait(false);
return result.Any(r => r.Equals(_serviceName));
}

public async Task<List<string>> GetConnectionAsync() => await ExecuteAsync(_get_connections_cmd).ConfigureAwait(false);

public async Task<bool> SetConnectionAsync()
{
if (await HasConnectionAsync().ConfigureAwait(false))
Expand Down
61 changes: 61 additions & 0 deletions src/Plugins/MinIO/Tests/MinIoAdminHealthCheckTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Monai.Deploy.Storage.API;
using Moq;
using Xunit;

namespace Monai.Deploy.Storage.MinIO.Tests
{
public class MinIoAdminHealthCheckTest
{
private readonly Mock<IStorageAdminService> _storageAdminService;
private readonly Mock<ILogger<MinIoAdminHealthCheck>> _logger;

public MinIoAdminHealthCheckTest()
{
_storageAdminService = new Mock<IStorageAdminService>();
_logger = new Mock<ILogger<MinIoAdminHealthCheck>>();
}

[Fact]
public async Task CheckHealthAsync_WhenConnectionThrows_ReturnUnhealthy()
{
_storageAdminService.Setup(p => p.HasConnectionAsync()).Throws(new Exception("error"));

var healthCheck = new MinIoAdminHealthCheck(_storageAdminService.Object, _logger.Object);
var results = await healthCheck.CheckHealthAsync(new HealthCheckContext()).ConfigureAwait(false);

Assert.Equal(HealthStatus.Unhealthy, results.Status);
Assert.NotNull(results.Exception);
Assert.Equal("error", results.Exception.Message);
}

[Fact]
public async Task CheckHealthAsync_WhenConnectionSucceeds_ReturnHealthy()
{
_storageAdminService.Setup(p => p.HasConnectionAsync()).ReturnsAsync(true);
_storageAdminService.Setup(p => p.GetConnectionAsync()).ReturnsAsync(new List<string>() { "strings" });
var healthCheck = new MinIoAdminHealthCheck(_storageAdminService.Object, _logger.Object);
var results = await healthCheck.CheckHealthAsync(new HealthCheckContext()).ConfigureAwait(false);

Assert.Equal(HealthStatus.Healthy, results.Status);
Assert.Null(results.Exception);
}
}
}
12 changes: 12 additions & 0 deletions src/Storage/API/IStorageAdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,17 @@ public interface IStorageAdminService
/// </summary>
/// <param name="username">Username</param>
Task RemoveUserAsync(string username);

/// <summary>
/// Gets list of alias connections.
/// </summary>
/// <returns></returns>
Task<List<string>> GetConnectionAsync();

/// <summary>
/// If connection contains configured service name.
/// </summary>
/// <returns></returns>
Task<bool> HasConnectionAsync();
}
}