Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Azure.Identity" Version="1.17.0" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageVersion Include="Basic.Reference.Assemblies.Net472" Version="1.7.9" />
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\apisof.net\apisof.net.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
7 changes: 6 additions & 1 deletion src/NetUpgradePlannerTelemetry/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using ApisOfDotNet.Shared;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddSingleton<AzureBlobClientManager>();
})
.Build();

host.Run();
14 changes: 6 additions & 8 deletions src/NetUpgradePlannerTelemetry/StoreTelemetryFunction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Net;
using System.Web;

using Azure.Storage.Blobs;

using System.Web;
using ApisOfDotNet.Shared;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Configuration;
Expand All @@ -14,11 +12,13 @@ public sealed class StoreTelemetryFunction
{
private readonly ILogger _logger;
private readonly IConfiguration _configuration;
private readonly AzureBlobClientManager _blobClientManager;

public StoreTelemetryFunction(IConfiguration configuration, ILoggerFactory loggerFactory)
public StoreTelemetryFunction(IConfiguration configuration, ILoggerFactory loggerFactory, AzureBlobClientManager blobClientManager)
{
_logger = loggerFactory.CreateLogger<StoreTelemetryFunction>();
_configuration = configuration;
_blobClientManager = blobClientManager;
}

[Function("store-telemetry")]
Expand Down Expand Up @@ -55,11 +55,9 @@ public async Task<HttpResponseData> Run(
if (apis.Count == 0)
return request.CreateResponse(HttpStatusCode.BadRequest);

var connectionString = _configuration["AzureStorageConnectionString"];

var container = "planner";
var blobName = fingerprint;
var blobClient = new BlobClient(connectionString, container, blobName);
var blobClient = _blobClientManager.GetBlobContainerClient(container).GetBlobClient(blobName);
var blobText = string.Join(Environment.NewLine, apis);
var blobData = BinaryData.FromString(blobText);
await blobClient.UploadAsync(blobData, overwrite: true);
Expand Down
44 changes: 13 additions & 31 deletions src/Terrajobst.ApiCatalog.ActionsRunner/ApisOfDotNetStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Azure.Core;
using Azure.Storage.Blobs;
using ApisOfDotNet.Shared;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -11,19 +10,23 @@ public sealed class ApisOfDotNetStore
private readonly IHostEnvironment _hostEnvironment;
private readonly IOptions<ApisOfDotNetStoreOptions> _options;
private readonly ILogger<ApisOfDotNetStore> _logger;
private readonly AzureBlobClientManager _blobClientManager;

private const string BlobMetadataKeyIndexTimestamp = "IndexTimestamp";

public ApisOfDotNetStore(IHostEnvironment hostEnvironment,
IOptions<ApisOfDotNetStoreOptions> options,
ILogger<ApisOfDotNetStore> logger)
ILogger<ApisOfDotNetStore> logger,
AzureBlobClientManager blobClientManager)
{
ThrowIfNull(options);
ThrowIfNull(logger);
ThrowIfNull(blobClientManager);

_hostEnvironment = hostEnvironment;
_options = options;
_logger = logger;
_blobClientManager = blobClientManager;
}

public async Task SetTimestampAsync(string blobContainer, string blobName, DateTimeOffset timestamp)
Expand All @@ -36,8 +39,7 @@ public async Task SetTimestampAsync(string blobContainer, string blobName, DateT

Console.WriteLine($"Setting timestamp for {blobContainer}/{blobName}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);
await blobClient.SetMetadataAsync(new Dictionary<string, string> {
[BlobMetadataKeyIndexTimestamp] = timestamp.ToString("O")
});
Expand All @@ -47,8 +49,7 @@ await blobClient.SetMetadataAsync(new Dictionary<string, string> {
{
Console.WriteLine($"Getting timestamp for {blobContainer}/{blobName}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);

var properties = await blobClient.GetPropertiesAsync();
if (properties.HasValue &&
Expand All @@ -71,8 +72,7 @@ public async Task UploadAsync(string blobContainer, string blobName, string file

Console.WriteLine($"Uploading {blobContainer}/{blobName}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);
await blobClient.UploadAsync(fileName, overwrite: true);
}

Expand All @@ -86,17 +86,15 @@ public async Task UploadAsync(string blobContainer, string blobName, Stream stre

Console.WriteLine($"Uploading {blobContainer}/{blobName}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);
await blobClient.UploadAsync(stream, overwrite: true);
}

public async Task<IReadOnlyList<string>> GetBlobNamesAsync(string blobContainer, DateTimeOffset? since = null)
{
Console.WriteLine($"Enumerating {blobContainer}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var containerClient = new BlobContainerClient(connectionString, blobContainer, options: GetBlobOptions());
var containerClient = _blobClientManager.GetBlobContainerClient(blobContainer);

var result = new List<string>();
await foreach (var blob in containerClient.GetBlobsAsync())
Expand All @@ -114,8 +112,7 @@ public async Task DownloadToAsync(string blobContainer, string blobName, string
{
Console.WriteLine($"Downloading {Path.GetFileName(fileName)}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);
var props = await blobClient.GetPropertiesAsync();
var lastModified = props.Value.LastModified;
await blobClient.DownloadToAsync(fileName);
Expand All @@ -126,22 +123,7 @@ public async Task<Stream> OpenReadAsync(string blobContainer, string blobName)
{
Console.WriteLine($"Opening blob {blobContainer}/{blobName}...");

var connectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(connectionString, blobContainer, blobName, options: GetBlobOptions());
var blobClient = _blobClientManager.GetBlobContainerClient(blobContainer).GetBlobClient(blobName);
return await blobClient.OpenReadAsync();
}

private static BlobClientOptions GetBlobOptions()
{
return new BlobClientOptions
{
Retry =
{
Mode = RetryMode.Exponential,
Delay = TimeSpan.FromSeconds(90),
MaxRetries = 10,
NetworkTimeout = TimeSpan.FromMinutes(5),
}
};
}
}
4 changes: 2 additions & 2 deletions src/Terrajobst.ApiCatalog.ActionsRunner/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using ApisOfDotNet.Shared;
using Serilog;

namespace Terrajobst.ApiCatalog.ActionsRunner;
Expand Down Expand Up @@ -33,7 +33,7 @@ public static HostApplicationBuilder CreateApplicationBuilder()
builder.Services.AddSingleton<GitHubActionsEnvironment>();
builder.Services.AddSingleton<GitHubActionsLog>();
builder.Services.AddSingleton<GitHubActionsSummaryTable>();

builder.Services.AddSingleton<AzureBlobClientManager>();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<PackageReference Include="Serilog.Sinks.File" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\apisof.net\apisof.net.csproj" />
</ItemGroup>

</Project>
15 changes: 9 additions & 6 deletions src/apisof.net/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ namespace ApisOfDotNet.Controllers;
public sealed class DownloadController : Controller
{
private readonly IOptions<ApisOfDotNetOptions> _options;
private readonly AzureBlobClientManager _blobClientManager;

public DownloadController(IOptions<ApisOfDotNetOptions> options)

public DownloadController(IOptions<ApisOfDotNetOptions> options, AzureBlobClientManager azureBlobClientManager)
{
ThrowIfNull(options);

ThrowIfNull(azureBlobClientManager);
_blobClientManager = azureBlobClientManager;
_options = options;
}

[HttpGet]
public async Task<FileStreamResult> Get()
{
var azureConnectionString = _options.Value.AzureStorageConnectionString;
var blobClient = new BlobClient(azureConnectionString, "catalog", "apicatalog.dat");
var stream = await blobClient.OpenReadAsync();
{
var containerClient = _blobClientManager.GetBlobContainerClient("catalog");
var stream = await containerClient.GetBlobClient("apicatalog.dat").OpenReadAsync();

return new FileStreamResult(stream, "application/octet-stream") {
FileDownloadName = "apicatalog.dat"
};
Expand Down
1 change: 1 addition & 0 deletions src/apisof.net/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
builder.Services.AddServerSideBlazor();
builder.Services.AddControllers();
builder.Services.AddHotKeys2();
builder.Services.AddSingleton<AzureBlobClientManager>();
builder.Services.AddSingleton<CatalogService>();
builder.Services.AddSingleton<DocumentationResolverService>();
builder.Services.AddHttpClient<DocumentationResolverService>();
Expand Down
20 changes: 14 additions & 6 deletions src/apisof.net/Services/CatalogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class CatalogService
private readonly IOptions<ApisOfDotNetOptions> _options;
private readonly IWebHostEnvironment _environment;
private readonly ILogger<CatalogService> _logger;
private readonly AzureBlobClientManager _blobClientManager;

private readonly BlobSource<ApiCatalogModel> _catalogBlobSource;
private readonly BlobSource<SuffixTree> _suffixTreeBlobSource;
Expand All @@ -23,15 +24,18 @@ public sealed class CatalogService

public CatalogService(IOptions<ApisOfDotNetOptions> options,
IWebHostEnvironment environment,
ILogger<CatalogService> logger)
ILogger<CatalogService> logger,
AzureBlobClientManager blobClientManager)
{
ThrowIfNull(options);
ThrowIfNull(environment);
ThrowIfNull(logger);
ThrowIfNull(blobClientManager);

_options = options;
_environment = environment;
_logger = logger;
_blobClientManager = blobClientManager;

_catalogBlobSource = CreateBlobSource("catalog", "apicatalog.dat", ApiCatalogModel.LoadAsync);
_suffixTreeBlobSource = CreateBlobSource("catalog", "suffixtree.dat.deflate", SuffixTree.LoadDeflate);
Expand Down Expand Up @@ -73,12 +77,12 @@ public async void InvalidateUsageData()

private BlobSource<T> CreateBlobSource<T>(string containerName, string blobName, Func<string, T> loader)
{
return new BlobSource<T>(_logger, _options, containerName, blobName, s => Task.FromResult(loader(s)));
return new BlobSource<T>(_logger, _options, _blobClientManager, containerName, blobName, s => Task.FromResult(loader(s)));
}

private BlobSource<T> CreateBlobSource<T>(string containerName, string blobName, Func<string, Task<T>> loader)
{
return new BlobSource<T>(_logger, _options, containerName, blobName, loader);
return new BlobSource<T>(_logger, _options, _blobClientManager, containerName, blobName, loader);
}

public ApiCatalogModel Catalog => _data.Catalog;
Expand Down Expand Up @@ -131,21 +135,25 @@ private sealed class BlobSource<T> : BlobSource
{
private readonly ILogger<CatalogService> _logger;
private readonly IOptions<ApisOfDotNetOptions> _options;
private readonly AzureBlobClientManager _blobClientManager;
private readonly Func<string, Task<T>> _loader;

public BlobSource(ILogger<CatalogService> logger,
IOptions<ApisOfDotNetOptions> options,
AzureBlobClientManager blobClientManager,
string containerName,
string blobName,
Func<string, Task<T>> loader)
: base(containerName, blobName)
{
ThrowIfNull(logger);
ThrowIfNull(options);
ThrowIfNull(blobClientManager);
ThrowIfNull(loader);

_logger = logger;
_options = options;
_blobClientManager = blobClientManager;
_loader = loader;
}

Expand All @@ -161,10 +169,10 @@ public async Task<T> DownloadAsync(bool invalidateCachedDownload)
{
_logger.LogInformation($"Downloading {BlobName}...");

await Task.Run(() =>
await Task.Run(async () =>
{
var blobClient = new BlobClient(_options.Value.AzureStorageConnectionString, ContainerName, BlobName);
return blobClient.DownloadToAsync(localPath);
var containerClient = _blobClientManager.GetBlobContainerClient(ContainerName);
await containerClient.GetBlobClient(BlobName).DownloadToAsync(localPath);
});

_logger.LogInformation($"Downloading {BlobName} complete.");
Expand Down
32 changes: 32 additions & 0 deletions src/apisof.net/Shared/AzureBlobClientManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Concurrent;
using Azure.Identity;
using Azure.Storage.Blobs;
namespace ApisOfDotNet.Shared
{
public sealed class AzureBlobClientManager
{
private readonly IConfiguration _configuration;
private readonly BlobServiceClient blobServiceClient;
private readonly ConcurrentDictionary<string, BlobContainerClient> containerClients = new();

public AzureBlobClientManager(IConfiguration configuration)
{
_configuration = configuration;
#if DEBUG
var credential = new DefaultAzureCredential();
#else
var credential = new ManagedIdentityCredential();
#endif
string blobStorageUrl = _configuration["AzureBlobStorageUri"];

Check warning on line 20 in src/apisof.net/Shared/AzureBlobClientManager.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 20 in src/apisof.net/Shared/AzureBlobClientManager.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
blobServiceClient = new BlobServiceClient(new Uri(blobStorageUrl), credential);

Check warning on line 21 in src/apisof.net/Shared/AzureBlobClientManager.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString)'.

Check warning on line 21 in src/apisof.net/Shared/AzureBlobClientManager.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString)'.
}

public BlobContainerClient GetBlobContainerClient(string containerName)
{
return containerClients.GetOrAdd(containerName, name =>
{
return blobServiceClient.GetBlobContainerClient(name);
});
}
}
}
1 change: 1 addition & 0 deletions src/apisof.net/apisof.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Humanizer.Core" />
<PackageReference Include="Markdig" />
Expand Down
3 changes: 2 additions & 1 deletion src/apisof.net/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"System.Net.Http.HttpClient.SourceResolverService": "Warning",
"System.Net.Http.HttpClient.DocumentationResolverService": "Warning",
"System.Net.Http.HttpClient.DocumentationResolverService": "Warning"
}
},
"AzureBlobStorageUri": "https://dotnetstoragedev.blob.core.windows.net",
"AllowedHosts": "*"
}
Loading