Skip to content

Commit 349c984

Browse files
author
MHD\nialan
committed
Moved data lake client createion into a factory to allow other auth types to be added simply, and allow mocking for unit tests
1 parent 1e00b93 commit 349c984

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Azure.Identity;
2+
using Azure.Storage.Files.DataLake;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Azure.Datafactory.Extensions.Functions
9+
{
10+
public static class DataLakeClientFactory
11+
{
12+
13+
public static DataLakeFileSystemClient GetDataLakeClient(DataLakeConfig settings, ILogger log)
14+
{
15+
// This works as long as the account accessing (managed identity or visual studio user) has both of the following IAM permissions on the storage account:
16+
// - Reader
17+
// - Storage Blob Data Reader
18+
var credential = new DefaultAzureCredential();
19+
log.LogInformation($"Using credential Type: {credential.GetType().Name}");
20+
21+
var client = new DataLakeFileSystemClient(new Uri(settings.BaseUrl), credential);
22+
23+
if (client == null || !client.Exists())
24+
throw new ArgumentException($"Container '{settings.Container}' not found in storage account '{settings.AccountUri}'. Check the names are correct, and that access is granted to the functions application service principal.");
25+
26+
return client;
27+
}
28+
}
29+
}

ADF.Functions/DataLakeHelpers.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Linq;
1616
using System.Reflection;
1717
using System.Linq.Dynamic.Core;
18+
using Azure.Storage.Files.DataLake.Models;
1819

1920
namespace Azure.Datafactory.Extensions.Functions
2021
{
@@ -34,10 +35,7 @@ public static async Task<IActionResult> DataLakeGetItems(
3435
if (string.IsNullOrWhiteSpace(settings.AccountUri))
3536
throw new ArgumentException($"Account Uri '{settings.AccountUri}' not found. Check the URI is correct.");
3637

37-
var client = GetDataLakeClient(settings, log);
38-
if (client == null || !client.Exists())
39-
throw new ArgumentException($"Container '{settings.Container}' not found in storage account '{settings.AccountUri}'. Check the names are correct, and that access is granted to the functions application service principal.");
40-
38+
var client = DataLakeClientFactory.GetDataLakeClient(settings, log);
4139
return await GetItemsAsync(client, settings, log);
4240
}
4341
catch (ArgumentException ex)
@@ -68,9 +66,7 @@ public static async Task<IActionResult> DataLakeCheckPathCase(
6866
if (string.IsNullOrWhiteSpace(settings.AccountUri))
6967
throw new ArgumentException($"Account Uri '{settings.AccountUri}' not found. Check the URI is correct.");
7068

71-
var client = GetDataLakeClient(settings, log);
72-
if (client == null || ! await client.ExistsAsync())
73-
throw new ArgumentException($"Container '{settings.Container}' not found in storage account '{settings.AccountUri}'. Check the names are correct, and that access is granted to the functions application service principal.");
69+
var client = DataLakeClientFactory.GetDataLakeClient(settings, log);
7470

7571
var paramsJsonFragment = GetParamsJsonFragment(settings);
7672
var validatedPath = await CheckPathAsync(client, settings.Path, true, log);
@@ -108,20 +104,20 @@ private static string GetParamsJsonFragment(DataLakeConfig settings)
108104
}
109105

110106

111-
private static DataLakeFileSystemClient GetDataLakeClient(DataLakeConfig settings, ILogger log)
112-
{
113-
// This works as long as the account accessing (managed identity or visual studio user) has both of the following IAM permissions on the storage account:
114-
// - Reader
115-
// - Storage Blob Data Reader
116-
var credential = new DefaultAzureCredential();
117-
log.LogInformation($"Using credential Type: {credential.GetType().Name}");
118-
119-
var client = new DataLakeFileSystemClient(new Uri(settings.BaseUrl), credential);
120-
if (!client.Exists())
121-
return null;
107+
//private static DataLakeFileSystemClient GetDataLakeClient(DataLakeConfig settings, ILogger log)
108+
//{
109+
// // This works as long as the account accessing (managed identity or visual studio user) has both of the following IAM permissions on the storage account:
110+
// // - Reader
111+
// // - Storage Blob Data Reader
112+
// var credential = new DefaultAzureCredential();
113+
// log.LogInformation($"Using credential Type: {credential.GetType().Name}");
122114

123-
return client;
124-
}
115+
// var client = new DataLakeFileSystemClient(new Uri(settings.BaseUrl), credential);
116+
// if (!client.Exists())
117+
// return null;
118+
119+
// return client;
120+
//}
125121

126122

127123
private async static Task<string> CheckPathAsync(DataLakeFileSystemClient client, string path, bool isDirectory, ILogger log)

0 commit comments

Comments
 (0)