59 lines
2.1 KiB
C#
59 lines
2.1 KiB
C#
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using StellaOps.DependencyInjection;
|
|
using StellaOps.Notify.Engine;
|
|
using StellaOps.Notify.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StellaOps.Notify.Connectors.Teams;
|
|
|
|
[ServiceBinding(typeof(INotifyChannelHealthProvider), ServiceLifetime.Singleton)]
|
|
public sealed class TeamsChannelHealthProvider : INotifyChannelHealthProvider
|
|
{
|
|
public NotifyChannelType ChannelType => NotifyChannelType.Teams;
|
|
|
|
public Task<ChannelHealthResult> CheckAsync(ChannelHealthContext context, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(context);
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var builder = TeamsMetadataBuilder.CreateBuilder(context)
|
|
.Add("teams.channel.enabled", context.Channel.Enabled ? "true" : "false")
|
|
.Add("teams.validation.targetPresent", HasConfiguredTarget(context.Channel) ? "true" : "false");
|
|
|
|
var metadata = builder.Build();
|
|
var status = ResolveStatus(context.Channel);
|
|
var message = status switch
|
|
{
|
|
ChannelHealthStatus.Healthy => "Teams channel configuration validated.",
|
|
ChannelHealthStatus.Degraded => "Teams channel is disabled; enable it to resume deliveries.",
|
|
ChannelHealthStatus.Unhealthy => "Teams channel is missing a target/endpoint configuration.",
|
|
_ => "Teams channel diagnostics completed."
|
|
};
|
|
|
|
return Task.FromResult(new ChannelHealthResult(status, message, metadata));
|
|
}
|
|
|
|
private static ChannelHealthStatus ResolveStatus(NotifyChannel channel)
|
|
{
|
|
if (!HasConfiguredTarget(channel))
|
|
{
|
|
return ChannelHealthStatus.Unhealthy;
|
|
}
|
|
|
|
if (!channel.Enabled)
|
|
{
|
|
return ChannelHealthStatus.Degraded;
|
|
}
|
|
|
|
return ChannelHealthStatus.Healthy;
|
|
}
|
|
|
|
private static bool HasConfiguredTarget(NotifyChannel channel)
|
|
=> !string.IsNullOrWhiteSpace(channel.Config.Endpoint) ||
|
|
!string.IsNullOrWhiteSpace(channel.Config.Target);
|
|
}
|