This commit is contained in:
2024-10-13 20:49:50 -05:00
parent 1e9c269f07
commit cd9682379d
9 changed files with 118 additions and 39 deletions

View File

@@ -1,32 +1,56 @@
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ProperDI.Azure.Endpoints.ResourceGroup.LogLooker;
using Microsoft.Extensions.Configuration;
using ProperDI.Azure.Endpoints.ResourceGroup;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHttpClient();
builder.Services.AddTransient<IActivityLogReader, ActivityLogReader>();
builder.Services.AddAzureClients(clientBuilder =>
public class Program
{
// var creds = new ClientSecretCredential(
// "",
// "",
// "");
clientBuilder.UseCredential(new DefaultAzureCredential());
clientBuilder.AddArmClient("2690a5f1-155b-4fa8-896f-92c6bcb62bee");
clientBuilder.ConfigureDefaults(client =>
public static async Task Main(string[] args)
{
client.Retry.MaxRetries = 3;
});
});
var host = CreateHostBuilder(args).Build();
await host.RunAsync();
}
builder.Services.Configure<RoleAssesorBackgroundServiceOptions>(builder.Configuration.GetSection("RoleAssessorBackgroundService"));
builder.Services.AddHostedService<RoleAssesorBackgroundService>();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
// Optionally, you can add JSON configuration if needed
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient();
services.AddTransient<IActivityLogReader, ActivityLogReader>();
var host = builder.Build();
services.AddAzureClients(clientBuilder =>
{
var configuration = hostContext.Configuration;
// Read Azure credentials from environment variables
var tenantId = configuration["AzureTenantId"];
var clientId = configuration["AzureClientId"];
var clientSecret = configuration["AzureClientSecret"];
var subscriptionId = configuration["AzureSubscriptionId"];
System.Console.WriteLine($"Tenant id: {tenantId}\nClientID: {clientId}\nClientSecret: {clientSecret}");
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
await host.RunAsync();
clientBuilder.UseCredential(credential);
clientBuilder.AddArmClient(subscriptionId);
clientBuilder.ConfigureDefaults(client =>
{
client.Retry.MaxRetries = 3;
});
clientBuilder.AddLogsQueryClient();
});
services.Configure<RoleAssesorBackgroundServiceOptions>(
hostContext.Configuration.GetSection("RoleAssessorBackgroundService"));
services.AddHostedService<RoleAssesorBackgroundService>();
});
}