1+ using Azure . Data . Tables ;
2+ using System ;
3+ using System . Linq ;
4+ using System . Threading . Tasks ;
5+ using Azure . Core ;
6+ using Azure . ResourceManager . Storage ;
7+ using Azure . ResourceManager ;
8+ using Azure . Identity ;
9+
10+ namespace Microsoft . OneFuzz . Service . OneFuzzLib . Orm ;
11+
12+
13+ public interface IStorageProvider
14+ {
15+ Task < TableClient > GetTableClient ( string table ) ;
16+ //IAsyncEnumerable<T> QueryAsync<T>(string filter) where T : EntityBase;
17+ //Task<bool> Replace<T>(T entity) where T : EntityBase;
18+
19+ }
20+
21+ public class StorageProvider : IStorageProvider
22+ {
23+ private readonly string _accountId ;
24+ private readonly EntityConverter _entityConverter ;
25+ private readonly ArmClient _armClient ;
26+
27+ public StorageProvider ( string accountId )
28+ {
29+ _accountId = accountId ;
30+ _entityConverter = new EntityConverter ( ) ;
31+ _armClient = new ArmClient ( new DefaultAzureCredential ( ) ) ;
32+ }
33+
34+ public async Task < TableClient > GetTableClient ( string table )
35+ {
36+ var ( name , key ) = GetStorageAccountNameAndKey ( _accountId ) ;
37+ var identifier = new ResourceIdentifier ( _accountId ) ;
38+ var tableClient = new TableServiceClient ( new Uri ( $ "https://{ identifier . Name } .table.core.windows.net") , new TableSharedKeyCredential ( name , key ) ) ;
39+ await tableClient . CreateTableIfNotExistsAsync ( table ) ;
40+ return tableClient . GetTableClient ( table ) ;
41+ }
42+
43+
44+ public ( string ? , string ? ) GetStorageAccountNameAndKey ( string accountId )
45+ {
46+ var resourceId = new ResourceIdentifier ( accountId ) ;
47+ var storageAccount = _armClient . GetStorageAccountResource ( resourceId ) ;
48+ var key = storageAccount . GetKeys ( ) . Value . Keys . FirstOrDefault ( ) ;
49+ return ( resourceId . Name , key ? . Value ) ;
50+ }
51+
52+
53+ }
0 commit comments