2023-05-09 08:38:07 +02:00
|
|
|
using Asp.Versioning;
|
2022-12-19 13:50:18 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Umbraco.Cms.Api.Management.ViewModels.HealthCheck;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.HealthChecks;
|
|
|
|
|
using Umbraco.Cms.Core.Mapping;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers.HealthCheck;
|
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
[ApiVersion("1.0")]
|
2022-12-19 13:50:18 +01:00
|
|
|
public class ExecuteActionHealthCheckController : HealthCheckControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly HealthCheckCollection _healthChecks;
|
|
|
|
|
private readonly IUmbracoMapper _umbracoMapper;
|
|
|
|
|
private readonly IList<Guid> _disabledCheckIds;
|
|
|
|
|
|
|
|
|
|
public ExecuteActionHealthCheckController(
|
|
|
|
|
HealthCheckCollection healthChecks,
|
|
|
|
|
IOptions<HealthChecksSettings> healthChecksSettings,
|
|
|
|
|
IUmbracoMapper umbracoMapper)
|
|
|
|
|
{
|
|
|
|
|
_healthChecks = healthChecks;
|
|
|
|
|
_disabledCheckIds = healthChecksSettings.Value
|
|
|
|
|
.DisabledChecks
|
|
|
|
|
.Select(x => x.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
_umbracoMapper = umbracoMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes a given action from a HealthCheck.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="action">The action to be executed.</param>
|
|
|
|
|
/// <returns>The result of a health check after the health check action is performed.</returns>
|
|
|
|
|
[HttpPost("execute-action")]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-03-13 10:49:21 +01:00
|
|
|
[ProducesResponseType(typeof(HealthCheckResultResponseModel), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<ActionResult<HealthCheckResultResponseModel>> ExecuteAction(HealthCheckActionRequestModel action)
|
2022-12-19 13:50:18 +01:00
|
|
|
{
|
2024-02-06 13:19:30 +01:00
|
|
|
Guid healthCheckKey = action.HealthCheck.Id;
|
2022-12-19 13:50:18 +01:00
|
|
|
|
|
|
|
|
Core.HealthChecks.HealthCheck? healthCheck = _healthChecks
|
|
|
|
|
.Where(x => _disabledCheckIds.Contains(healthCheckKey) == false)
|
|
|
|
|
.FirstOrDefault(x => x.Id == healthCheckKey);
|
|
|
|
|
|
|
|
|
|
if (healthCheck is null)
|
|
|
|
|
{
|
|
|
|
|
var invalidModelProblem = new ProblemDetails
|
|
|
|
|
{
|
|
|
|
|
Title = "Health Check Not Found",
|
2023-04-04 13:20:29 +02:00
|
|
|
Detail = $"No health check found with id = {healthCheckKey}",
|
2022-12-19 13:50:18 +01:00
|
|
|
Status = StatusCodes.Status400BadRequest,
|
|
|
|
|
Type = "Error",
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-08 14:16:21 +01:00
|
|
|
return BadRequest(invalidModelProblem);
|
2022-12-19 13:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HealthCheckStatus result = healthCheck.ExecuteAction(_umbracoMapper.Map<HealthCheckAction>(action)!);
|
|
|
|
|
|
2023-03-13 10:49:21 +01:00
|
|
|
return await Task.FromResult(Ok(_umbracoMapper.Map<HealthCheckResultResponseModel>(result)));
|
2022-12-19 13:50:18 +01:00
|
|
|
}
|
|
|
|
|
}
|