75 lines
2.3 KiB
C#
75 lines
2.3 KiB
C#
using Azure;
|
|
using Azure.Monitor.Query;
|
|
using Azure.Monitor.Query.Models;
|
|
using Azure.ResourceManager;
|
|
using Azure.ResourceManager.Resources;
|
|
using Microsoft.Extensions.Logging;
|
|
using ProperDI.Azure.Endpoints.ResourceGroup.Models;
|
|
|
|
namespace ProperDI.Azure.Endpoints.ResourceGroup;
|
|
|
|
public interface IActivityLogReader
|
|
{
|
|
Task ScanAppAsync(string appName, CancellationToken cancellationToken, QueryTimeRange? givenTimeRange);
|
|
Task ScanAllAsync(CancellationToken cancellationToken);
|
|
}
|
|
|
|
public class ActivityLogReader : IActivityLogReader
|
|
{
|
|
private readonly ILogger<ActivityLogReader> _logger;
|
|
private readonly ArmClient _armClient;
|
|
private readonly LogsQueryClient _logsQueryClient;
|
|
|
|
public ActivityLogReader(ILogger<ActivityLogReader> logger, ArmClient armClient, LogsQueryClient logsQueryClient)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_armClient = armClient ?? throw new ArgumentNullException(nameof(armClient));
|
|
_logsQueryClient = logsQueryClient ?? throw new ArgumentNullException(nameof(logsQueryClient));
|
|
}
|
|
|
|
public async Task ScanAppAsync(string appName, CancellationToken cancellationToken, QueryTimeRange? givenTimeRange = default)
|
|
{
|
|
var timeRange = givenTimeRange ?? new QueryTimeRange(DateTime.UtcNow.AddDays(-7), DateTime.UtcNow);
|
|
|
|
try
|
|
{
|
|
_logger.LogInformation("Scanning for resources");
|
|
await ScanLogsForAppActivityAsync();
|
|
}
|
|
catch (TaskCanceledException)
|
|
{
|
|
_logger.LogWarning("Task canceled");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Http request failed");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task ScanAllAsync(CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task ScanLogsForAppActivityAsync()
|
|
{
|
|
}
|
|
|
|
|
|
private async Task<SubscriptionResource> GetDefaultSubscriptionAsync(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var sub = await _armClient.GetDefaultSubscriptionAsync(cancellationToken);
|
|
|
|
|
|
return sub;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to retrieve default subscription");
|
|
throw;
|
|
}
|
|
}
|
|
} |