-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure DependencyInjection for DataLake.
- Loading branch information
1 parent
f543e69
commit f7e5619
Showing
11 changed files
with
170 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Seneca.Azure.Integration.DataLake; | ||
using Swashbuckle.AspNetCore.Annotations; | ||
|
||
namespace Seneca.Azure.Integration.Api; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
[Route("api/[controller]/{name}")] | ||
public class DirectoriesController : ControllerBase | ||
{ | ||
[HttpPost] | ||
public async Task<IActionResult> CreateFolder() | ||
private readonly IStorageService storageService; | ||
|
||
public DirectoriesController(IStorageService storageService) | ||
{ | ||
this.storageService = storageService; | ||
} | ||
|
||
[HttpPut] | ||
public async Task<IActionResult> CreateDirectory(string name) | ||
{ | ||
// TODO: add implementation. | ||
return await Task.FromResult(new OkObjectResult("Not implemented yet")); | ||
} | ||
|
||
[HttpPut("link")] | ||
public async Task<IActionResult> GenerateToken(string name) | ||
{ | ||
// TODO: add implementation. | ||
return await Task.FromResult(new OkObjectResult("Not implemented yet")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 62 additions & 62 deletions
124
src/Seneca.Azure.Integration.DataLake/DataLakeServiceV1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Storage; | ||
using Azure.Storage.Files.DataLake; | ||
using Azure.Storage.Files.DataLake.Models; | ||
using Azure.Storage.Sas; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Seneca.Azure.Integration.DataLake; | ||
|
||
[Obsolete("Will be removed. Use StorageService instead.")] | ||
public class DataLakeServiceV1 | ||
{ | ||
private const string FileSystemDefaultName = "default-file-system"; | ||
|
||
private readonly DataLakeServiceClient serviceClient; | ||
private readonly DataLakeSettings settings; | ||
|
||
public DataLakeServiceV1(IOptions<DataLakeSettings> options) | ||
{ | ||
this.settings = options.Value; | ||
var credential = new StorageSharedKeyCredential(this.settings.AccountName, this.settings.AccountKey); | ||
var serviceUri = new Uri($"https://{this.settings.AccountName}.dfs.core.windows.net"); | ||
this.serviceClient = new DataLakeServiceClient(serviceUri, credential); | ||
} | ||
|
||
public async Task<(DataLakeDirectoryClient directoryClient, Uri sasUri)> CreateDirectoryWithSas( | ||
string directoryName, | ||
string policyName = "mySasPolicy") | ||
{ | ||
var fileSystemClient = this.serviceClient.GetFileSystemClient(FileSystemDefaultName); | ||
await fileSystemClient.CreateIfNotExistsAsync(); | ||
|
||
var directoryClientResponse = await fileSystemClient.CreateDirectoryAsync(directoryName); | ||
var directoryClient = directoryClientResponse.HasValue | ||
? directoryClientResponse.Value | ||
: throw new InvalidOperationException(); | ||
|
||
var sasBuilder = new DataLakeSasBuilder | ||
{ | ||
FileSystemName = fileSystemClient.Name, | ||
Path = directoryClient.Path, | ||
// TODO: move to the settings. | ||
ExpiresOn = DateTimeOffset.UtcNow.AddDays(1), | ||
Protocol = SasProtocol.Https, | ||
// TODO: move to the settings? | ||
Resource = "d", // 'd' for directory | ||
StartsOn = DateTimeOffset.UtcNow, | ||
}; | ||
|
||
// TODO: manage permissions properly. | ||
sasBuilder.SetPermissions(DataLakeSasPermissions.Read | DataLakeSasPermissions.Write); | ||
|
||
var sasToken = sasBuilder | ||
.ToSasQueryParameters(new StorageSharedKeyCredential(this.settings.AccountName, this.settings.AccountKey)) | ||
.ToString(); | ||
|
||
var sasUri = new Uri($"{directoryClient.Uri}?{sasToken}"); | ||
|
||
return (directoryClient, sasUri); | ||
} | ||
} | ||
// using System; | ||
// using System.Threading.Tasks; | ||
// using Azure.Storage; | ||
// using Azure.Storage.Files.DataLake; | ||
// using Azure.Storage.Files.DataLake.Models; | ||
// using Azure.Storage.Sas; | ||
// using Microsoft.Extensions.Options; | ||
// | ||
// namespace Seneca.Azure.Integration.DataLake; | ||
// | ||
// [Obsolete("Will be removed. Use StorageService instead.")] | ||
// public class DataLakeServiceV1 | ||
// { | ||
// private const string FileSystemDefaultName = "default-file-system"; | ||
// | ||
// private readonly DataLakeServiceClient serviceClient; | ||
// private readonly DataLakeSettings settings; | ||
// | ||
// public DataLakeServiceV1(IOptions<DataLakeSettings> options) | ||
// { | ||
// this.settings = options.Value; | ||
// var credential = new StorageSharedKeyCredential(this.settings.AccountName, this.settings.AccountKey); | ||
// var serviceUri = new Uri($"https://{this.settings.AccountName}.dfs.core.windows.net"); | ||
// this.serviceClient = new DataLakeServiceClient(serviceUri, credential); | ||
// } | ||
// | ||
// public async Task<(DataLakeDirectoryClient directoryClient, Uri sasUri)> CreateDirectoryWithSas( | ||
// string directoryName, | ||
// string policyName = "mySasPolicy") | ||
// { | ||
// var fileSystemClient = this.serviceClient.GetFileSystemClient(FileSystemDefaultName); | ||
// await fileSystemClient.CreateIfNotExistsAsync(); | ||
// | ||
// var directoryClientResponse = await fileSystemClient.CreateDirectoryAsync(directoryName); | ||
// var directoryClient = directoryClientResponse.HasValue | ||
// ? directoryClientResponse.Value | ||
// : throw new InvalidOperationException(); | ||
// | ||
// var sasBuilder = new DataLakeSasBuilder | ||
// { | ||
// FileSystemName = fileSystemClient.Name, | ||
// Path = directoryClient.Path, | ||
// // TODO: move to the settings. | ||
// ExpiresOn = DateTimeOffset.UtcNow.AddDays(1), | ||
// Protocol = SasProtocol.Https, | ||
// // TODO: move to the settings? | ||
// Resource = "d", // 'd' for directory | ||
// StartsOn = DateTimeOffset.UtcNow, | ||
// }; | ||
// | ||
// // TODO: manage permissions properly. | ||
// sasBuilder.SetPermissions(DataLakeSasPermissions.Read | DataLakeSasPermissions.Write); | ||
// | ||
// var sasToken = sasBuilder | ||
// .ToSasQueryParameters(new StorageSharedKeyCredential(this.settings.AccountName, this.settings.AccountKey)) | ||
// .ToString(); | ||
// | ||
// var sasUri = new Uri($"{directoryClient.Uri}?{sasToken}"); | ||
// | ||
// return (directoryClient, sasUri); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Seneca.Azure.Integration.DataLake/DependencyInjection/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Seneca.Azure.Integration.DataLake.DependencyInjection; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDataLake(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.AddSettings(configuration); | ||
services.AddAzureClients(clientBuilder => | ||
{ | ||
clientBuilder.AddDataLakeServiceClient(configuration.GetSection(DataLakeSettings.SectionName)); | ||
}); | ||
|
||
services.AddScoped<IStorageService, StorageService>(); | ||
|
||
return services; | ||
} | ||
|
||
private static IServiceCollection AddSettings(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.Configure<DataLakeSettings>(configuration.GetSection(DataLakeSettings.SectionName)); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Seneca.Azure.Integration.DataLake; | ||
|
||
public interface IStorageService | ||
{ | ||
Task CreateDirectory(string name); | ||
Task GenerateToken(string name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Seneca.Azure.Integration.DataLake.UnitTests/StorageServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Azure.Storage.Files.DataLake; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Seneca.Azure.Integration.DataLake.UnitTests; | ||
|
||
public class StorageServiceTests | ||
{ | ||
[Fact] | ||
public void GivenNull_WhenCallCreateDirectory_ThenNotImplementedExceptionThrown() | ||
{ | ||
var logger = new NullLogger<StorageService>(); | ||
var settingsMock = new Mock<IOptions<DataLakeSettings>>(); | ||
var clientMock = new Mock<DataLakeServiceClient>(); | ||
var sut = new StorageService(logger, settingsMock.Object, clientMock.Object); | ||
var action = () => sut.CreateDirectory(default!); | ||
action.ShouldThrow<NotImplementedException>(); | ||
} | ||
|
||
[Fact] | ||
public void GivenNull_WhenCallGenerateToken_ThenNotImplementedExceptionThrown() | ||
{ | ||
var logger = new NullLogger<StorageService>(); | ||
var settingsMock = new Mock<IOptions<DataLakeSettings>>(); | ||
var clientMock = new Mock<DataLakeServiceClient>(); | ||
var sut = new StorageService(logger, settingsMock.Object, clientMock.Object); | ||
var action = () => sut.GenerateToken(default!); | ||
action.ShouldThrow<NotImplementedException>(); | ||
} | ||
} |