Skip to content

Commit

Permalink
Re-enabled Azure - requested in bagetter#28
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouz committed Jan 1, 2024
1 parent f9c02f5 commit 448dfbd
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 122 deletions.
11 changes: 7 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Azure.Search.Documents" Version="11.4.0" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.18.0" />
<PackageVersion Include="Azure.Search.Documents" Version="11.5.1" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="4.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.36.0" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.37.0" />
<PackageVersion Include="Microsoft.Azure.Search.Data" Version="10.1.0" />
<PackageVersion Include="Microsoft.Azure.Search.Service" Version="10.1.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
Expand All @@ -23,6 +25,7 @@
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0-preview-23503-02" />
<PackageVersion Include="Moq" Version="4.20.69" />
<PackageVersion Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageVersion Include="xunit" Version="2.5.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
Expand All @@ -41,4 +44,4 @@
<PackageVersion Include="Google.Cloud.Storage.V1" Version="4.6.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
</ItemGroup>
</Project>
</Project>
205 changes: 99 additions & 106 deletions src/BaGetter.Azure/AzureApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,70 @@
using System;
using BaGetter.Azure;
using BaGetter.Core;
//using Azure.Cosmos.Table;
//using Azure.Search;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
//using Azure.Storage.Blobs;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.Search;

namespace BaGetter
{
//using CloudStorageAccount = Azure.Storage.CloudStorageAccount;
//using StorageCredentials = Azure.Storage.Auth.StorageCredentials;
using CloudStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount;
using StorageCredentials = Microsoft.WindowsAzure.Storage.Auth.StorageCredentials;

//using TableStorageAccount = Azure.Cosmos.Table.CloudStorageAccount;
using TableStorageAccount = Microsoft.Azure.Cosmos.Table.CloudStorageAccount;

public static class AzureApplicationExtensions
{
public static BaGetterApplication AddAzureTableDatabase(this BaGetterApplication app)
{
throw new NotImplementedException();
app.Services.AddBaGetterOptions<AzureTableOptions>(nameof(BaGetterOptions.Database));

//app.Services.AddBaGetterOptions<AzureTableOptions>(nameof(BaGetterOptions.Database));
app.Services.AddTransient<TablePackageDatabase>();
app.Services.AddTransient<TableOperationBuilder>();
app.Services.AddTransient<TableSearchService>();
app.Services.TryAddTransient<IPackageDatabase>(provider => provider.GetRequiredService<TablePackageDatabase>());
app.Services.TryAddTransient<ISearchService>(provider => provider.GetRequiredService<TableSearchService>());
app.Services.TryAddTransient<ISearchIndexer>(provider => provider.GetRequiredService<NullSearchIndexer>());

//app.Services.AddTransient<TablePackageDatabase>();
//app.Services.AddTransient<TableOperationBuilder>();
//app.Services.AddTransient<TableSearchService>();
//app.Services.TryAddTransient<IPackageDatabase>(provider => provider.GetRequiredService<TablePackageDatabase>());
//app.Services.TryAddTransient<ISearchService>(provider => provider.GetRequiredService<TableSearchService>());
//app.Services.TryAddTransient<ISearchIndexer>(provider => provider.GetRequiredService<NullSearchIndexer>());
app.Services.AddSingleton(provider =>
{
var options = provider.GetRequiredService<IOptions<AzureTableOptions>>().Value;
//app.Services.AddSingleton(provider =>
//{
// var options = provider.GetRequiredService<IOptions<AzureTableOptions>>().Value;
return TableStorageAccount.Parse(options.ConnectionString);
});

// return TableStorageAccount.Parse(options.ConnectionString);
//});
app.Services.AddTransient(provider =>
{
var account = provider.GetRequiredService<TableStorageAccount>();
//app.Services.AddTransient(provider =>
//{
// var account = provider.GetRequiredService<TableStorageAccount>();
return account.CreateCloudTableClient();
});

// return account.CreateCloudTableClient();
//});
app.Services.AddProvider<IPackageDatabase>((provider, config) =>
{
if (!config.HasDatabaseType("AzureTable")) return null;
//app.Services.AddProvider<IPackageDatabase>((provider, config) =>
//{
// if (!config.HasDatabaseType("AzureTable")) return null;
return provider.GetRequiredService<TablePackageDatabase>();
});

// return provider.GetRequiredService<TablePackageDatabase>();
//});
app.Services.AddProvider<ISearchService>((provider, config) =>
{
if (!config.HasSearchType("Database")) return null;
if (!config.HasDatabaseType("AzureTable")) return null;
//app.Services.AddProvider<ISearchService>((provider, config) =>
//{
// if (!config.HasSearchType("Database")) return null;
// if (!config.HasDatabaseType("AzureTable")) return null;
return provider.GetRequiredService<TableSearchService>();
});

// return provider.GetRequiredService<TableSearchService>();
//});
app.Services.AddProvider<ISearchIndexer>((provider, config) =>
{
if (!config.HasSearchType("Database")) return null;
if (!config.HasDatabaseType("AzureTable")) return null;
//app.Services.AddProvider<ISearchIndexer>((provider, config) =>
//{
// if (!config.HasSearchType("Database")) return null;
// if (!config.HasDatabaseType("AzureTable")) return null;
return provider.GetRequiredService<NullSearchIndexer>();
});

// return provider.GetRequiredService<NullSearchIndexer>();
//});

//return app;
return app;
}

public static BaGetterApplication AddAzureTableDatabase(
Expand All @@ -81,46 +78,44 @@ public static BaGetterApplication AddAzureTableDatabase(

public static BaGetterApplication AddAzureBlobStorage(this BaGetterApplication app)
{
throw new NotImplementedException();

//app.Services.AddBaGetterOptions<AzureBlobStorageOptions>(nameof(BaGetterOptions.Storage));
//app.Services.AddTransient<BlobStorageService>();
//app.Services.TryAddTransient<IStorageService>(provider => provider.GetRequiredService<BlobStorageService>());
app.Services.AddBaGetterOptions<AzureBlobStorageOptions>(nameof(BaGetterOptions.Storage));
app.Services.AddTransient<BlobStorageService>();
app.Services.TryAddTransient<IStorageService>(provider => provider.GetRequiredService<BlobStorageService>());

//app.Services.AddSingleton(provider =>
//{
// var options = provider.GetRequiredService<IOptions<AzureBlobStorageOptions>>().Value;
app.Services.AddSingleton(provider =>
{
var options = provider.GetRequiredService<IOptions<AzureBlobStorageOptions>>().Value;
// if (!string.IsNullOrEmpty(options.ConnectionString))
// {
// return CloudStorageAccount.Parse(options.ConnectionString);
// }
if (!string.IsNullOrEmpty(options.ConnectionString))
{
return CloudStorageAccount.Parse(options.ConnectionString);
}
// return new CloudStorageAccount(
// new StorageCredentials(
// options.AccountName,
// options.AccessKey),
// useHttps: true);
//});
return new CloudStorageAccount(
new StorageCredentials(
options.AccountName,
options.AccessKey),
useHttps: true);
});

//app.Services.AddTransient(provider =>
//{
// var options = provider.GetRequiredService<IOptionsSnapshot<AzureBlobStorageOptions>>().Value;
// var account = provider.GetRequiredService<CloudStorageAccount>();
app.Services.AddTransient(provider =>
{
var options = provider.GetRequiredService<IOptionsSnapshot<AzureBlobStorageOptions>>().Value;
var account = provider.GetRequiredService<CloudStorageAccount>();
// var client = account.CreateCloudBlobClient();
var client = account.CreateCloudBlobClient();
// return client.GetContainerReference(options.Container);
//});
return client.GetContainerReference(options.Container);
});

//app.Services.AddProvider<IStorageService>((provider, config) =>
//{
// if (!config.HasStorageType("AzureBlobStorage")) return null;
app.Services.AddProvider<IStorageService>((provider, config) =>
{
if (!config.HasStorageType("AzureBlobStorage")) return null;
// return provider.GetRequiredService<BlobStorageService>();
//});
return provider.GetRequiredService<BlobStorageService>();
});

//return app;
return app;
}

public static BaGetterApplication AddAzureBlobStorage(
Expand All @@ -134,48 +129,46 @@ public static BaGetterApplication AddAzureBlobStorage(

public static BaGetterApplication AddAzureSearch(this BaGetterApplication app)
{
throw new NotImplementedException();

//app.Services.AddBaGetterOptions<AzureSearchOptions>(nameof(BaGetterOptions.Search));
app.Services.AddBaGetterOptions<AzureSearchOptions>(nameof(BaGetterOptions.Search));

//app.Services.AddTransient<AzureSearchBatchIndexer>();
//app.Services.AddTransient<AzureSearchService>();
//app.Services.AddTransient<AzureSearchIndexer>();
//app.Services.AddTransient<IndexActionBuilder>();
//app.Services.TryAddTransient<ISearchService>(provider => provider.GetRequiredService<AzureSearchService>());
//app.Services.TryAddTransient<ISearchIndexer>(provider => provider.GetRequiredService<AzureSearchIndexer>());
app.Services.AddTransient<AzureSearchBatchIndexer>();
app.Services.AddTransient<AzureSearchService>();
app.Services.AddTransient<AzureSearchIndexer>();
app.Services.AddTransient<IndexActionBuilder>();
app.Services.TryAddTransient<ISearchService>(provider => provider.GetRequiredService<AzureSearchService>());
app.Services.TryAddTransient<ISearchIndexer>(provider => provider.GetRequiredService<AzureSearchIndexer>());

//app.Services.AddSingleton(provider =>
//{
// var options = provider.GetRequiredService<IOptions<AzureSearchOptions>>().Value;
// var credentials = new SearchCredentials(options.ApiKey);
app.Services.AddSingleton(provider =>
{
var options = provider.GetRequiredService<IOptions<AzureSearchOptions>>().Value;
var credentials = new SearchCredentials(options.ApiKey);
// return new SearchServiceClient(options.AccountName, credentials);
//});
return new SearchServiceClient(options.AccountName, credentials);
});

//app.Services.AddSingleton(provider =>
//{
// var options = provider.GetRequiredService<IOptions<AzureSearchOptions>>().Value;
// var credentials = new SearchCredentials(options.ApiKey);
app.Services.AddSingleton(provider =>
{
var options = provider.GetRequiredService<IOptions<AzureSearchOptions>>().Value;
var credentials = new SearchCredentials(options.ApiKey);
// return new SearchIndexClient(options.AccountName, PackageDocument.IndexName, credentials);
//});
return new SearchIndexClient(options.AccountName, PackageDocument.IndexName, credentials);
});

//app.Services.AddProvider<ISearchService>((provider, config) =>
//{
// if (!config.HasSearchType("AzureSearch")) return null;
app.Services.AddProvider<ISearchService>((provider, config) =>
{
if (!config.HasSearchType("AzureSearch")) return null;
// return provider.GetRequiredService<AzureSearchService>();
//});
return provider.GetRequiredService<AzureSearchService>();
});

//app.Services.AddProvider<ISearchIndexer>((provider, config) =>
//{
// if (!config.HasSearchType("AzureSearch")) return null;
app.Services.AddProvider<ISearchIndexer>((provider, config) =>
{
if (!config.HasSearchType("AzureSearch")) return null;
// return provider.GetRequiredService<AzureSearchIndexer>();
//});
return provider.GetRequiredService<AzureSearchIndexer>();
});

//return app;
return app;
}

public static BaGetterApplication AddAzureSearch(
Expand Down
8 changes: 5 additions & 3 deletions src/BaGetter.Azure/BaGetter.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
<PackageReference Include="Azure.Search.Documents" />
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="Microsoft.Azure.Cosmos.Table" />
<PackageReference Include="Microsoft.Azure.Search" />
<PackageReference Include="Microsoft.Azure.Search.Data" />
<PackageReference Include="Microsoft.Azure.Search.Service" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
<PackageReference Include="WindowsAzure.Storage" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BaGetter.Core\BaGetter.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="**/*.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;

// ReSharper disable once CheckNamespace
namespace BaGetter.Azure
{
using StorageException = Microsoft.WindowsAzure.Storage.StorageException;
Expand Down
6 changes: 3 additions & 3 deletions src/BaGetter.Azure/Storage/BlobStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<Stream> GetAsync(string path, CancellationToken cancellationTo
{
return await _container
.GetBlockBlobReference(path)
.OpenReadAsync(cancellationToken);
.OpenReadAsync();
}

public Task<Uri> GetDownloadUriAsync(string path, CancellationToken cancellationToken)
Expand Down Expand Up @@ -65,7 +65,7 @@ await blob.UploadFromStreamAsync(
}
catch (StorageException e) when (e.IsAlreadyExistsException())
{
using (var targetStream = await blob.OpenReadAsync(cancellationToken))
using (var targetStream = await blob.OpenReadAsync())
{
content.Position = 0;
return content.Matches(targetStream)
Expand All @@ -79,7 +79,7 @@ public async Task DeleteAsync(string path, CancellationToken cancellationToken)
{
await _container
.GetBlockBlobReference(path)
.DeleteIfExistsAsync(cancellationToken);
.DeleteIfExistsAsync();
}
}
}
6 changes: 3 additions & 3 deletions src/BaGetter/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void ConfigureServices(IServiceCollection services)
private void ConfigureBaGetterApplication(BaGetterApplication app)
{
// Add database providers.
//app.AddAzureTableDatabase();
app.AddAzureTableDatabase();
app.AddMySqlDatabase();
app.AddPostgreSqlDatabase();
app.AddSqliteDatabase();
Expand All @@ -67,11 +67,11 @@ private void ConfigureBaGetterApplication(BaGetterApplication app)
app.AddFileStorage();
app.AddAliyunOssStorage();
app.AddAwsS3Storage();
//app.AddAzureBlobStorage();
app.AddAzureBlobStorage();
app.AddGoogleCloudStorage();

// Add search providers.
//app.AddAzureSearch();
app.AddAzureSearch();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
5 changes: 2 additions & 3 deletions tests/BaGetter.Core.Tests/Upstream/V3UpstreamClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ public async Task ReturnsPackages()
var result = await _target.ListPackagesAsync(_id, _cancellation);

var package = Assert.Single(result);

Assert.Equal("Foo", package.Id);
Assert.Equal(new[] { "Author1", "Author2"}, package.Authors);
Assert.Equal<string[]>(new[] {"Author1", "Author2" }, package.Authors);
Assert.Equal("Description", package.Description);
Assert.False(package.HasReadme);
Assert.False(package.HasEmbeddedIcon);
Expand All @@ -169,7 +168,7 @@ public async Task ReturnsPackages()
Assert.Equal("", package.ProjectUrlString);
Assert.Equal("", package.RepositoryUrlString);
Assert.Null(package.RepositoryType);
Assert.Equal(new[] { "Tag1", "Tag2" }, package.Tags);
Assert.Equal<string[]>(new[] { "Tag1", "Tag2" }, package.Tags);
Assert.Equal("1.2.3-prerelease", package.NormalizedVersionString);
Assert.Equal("1.2.3-prerelease+semver2", package.OriginalVersionString);
}
Expand Down

0 comments on commit 448dfbd

Please sign in to comment.