-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathAzureTableDataProvider.cs
More file actions
218 lines (194 loc) · 8.33 KB
/
AzureTableDataProvider.cs
File metadata and controls
218 lines (194 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using Audit.AzureStorageTables.ConfigurationApi;
using Audit.Core;
using Azure;
using Azure.Core;
using Azure.Data.Tables;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Audit.AzureStorageTables.Providers
{
public class AzureTableDataProvider : AuditDataProvider
{
/// <summary>
/// Azure Tables connection string
/// </summary>
public string ConnectionString { get; set; }
/// <summary>
/// Azure Tables table name
/// </summary>
public Setting<string> TableName { get; set; }
/// <summary>
/// The Azure.Data.Tables Client Options to use
/// </summary>
public TableClientOptions ClientOptions { get; set; }
/// <summary>
/// The Service endpoint to connect to. Alternative to ConnectionString.
/// </summary>
public Uri ServiceEndpoint { get; set; }
/// <summary>
/// The Shared Key credential to use to connect to the Service.
/// </summary>
public TableSharedKeyCredential SharedKeyCredential { get; set; }
/// <summary>
/// The Sas credential to use to connect to the Service.
/// </summary>
public AzureSasCredential SasCredential { get; set; }
/// <summary>
/// The Token credential to use to connect to the Service.
/// </summary>
public TokenCredential TokenCredential { get; set; }
/// <summary>
/// Gets or sets a function that returns a Table Entity from an Audit Event.
/// </summary>
public Func<AuditEvent, ITableEntity> TableEntityMapper { get; set; }
/// <summary>
/// Provides a factory to create the TableClient. Alternative to customize the table client creation.
/// </summary>
public Func<AuditEvent, TableClient> TableClientFactory { get; set; }
private static readonly ConcurrentDictionary<string, TableClient> TableClientCache = new ConcurrentDictionary<string, TableClient>();
public AzureTableDataProvider()
{
}
public AzureTableDataProvider(Action<IAzureTableConnectionConfigurator> config)
{
var cfg = new AzureTableConnectionConfigurator();
config.Invoke(cfg);
if (cfg._clientFactory != null)
{
// Factory provided
TableClientFactory = cfg._clientFactory;
}
else if (cfg._connectionString != null)
{
// By connection string
ConnectionString = cfg._connectionString;
ClientOptions = cfg._tableConfig._clientOptions;
TableName = cfg._tableConfig._tableName;
}
else if (cfg._endpointUri != null)
{
// By endpoint
ServiceEndpoint = cfg._endpointUri;
ClientOptions = cfg._tableConfig._clientOptions;
TableName = cfg._tableConfig._tableName;
SharedKeyCredential = cfg._sharedKeyCredential;
SasCredential = cfg._sasCredential;
TokenCredential = cfg._tokenCredential;
}
TableEntityMapper = cfg._tableConfig._tableEntityBuilder;
}
public override object InsertEvent(AuditEvent auditEvent)
{
var client = GetTableClient(auditEvent);
var entity = CreateTableEntity(auditEvent);
client.AddEntity(entity);
return new [] { entity.PartitionKey, entity.RowKey };
}
public override async Task<object> InsertEventAsync(AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
var client = await GetTableClientAsync(auditEvent, cancellationToken);
var entity = CreateTableEntity(auditEvent);
await client.AddEntityAsync(entity, cancellationToken);
return new[] { entity.PartitionKey, entity.RowKey };
}
public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
{
var fields = eventId as string[];
var partKey = fields[0];
var rowKey = fields[1];
var client = GetTableClient(auditEvent);
var entity = CreateTableEntity(auditEvent);
entity.PartitionKey = partKey;
entity.RowKey = rowKey;
client.UpdateEntity(entity, ETag.All, TableUpdateMode.Replace);
}
public override async Task ReplaceEventAsync(object eventId, AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
var fields = eventId as string[];
var partKey = fields[0];
var rowKey = fields[1];
var client = await GetTableClientAsync(auditEvent, cancellationToken);
var entity = CreateTableEntity(auditEvent);
entity.PartitionKey = partKey;
entity.RowKey = rowKey;
await client.UpdateEntityAsync(entity, ETag.All, TableUpdateMode.Replace, cancellationToken);
}
private ITableEntity CreateTableEntity(AuditEvent auditEvent)
{
return TableEntityMapper?.Invoke(auditEvent) ?? new AuditEventTableEntity(auditEvent);
}
/// <summary>
/// Returns a cached instance of a TableClient for the table related to the Audit Event. Creates the Table if it does not exists.
/// </summary>
/// <param name="auditEvent">The audit event</param>
public TableClient GetTableClient(AuditEvent auditEvent)
{
// From custom factory
if (TableClientFactory != null)
{
return TableClientFactory.Invoke(auditEvent);
}
var tableName = TableName.GetValue(auditEvent) ?? "Audit";
if (TableClientCache.TryGetValue(tableName, out TableClient client))
{
// From Cache
return client;
}
// New client
var newClient = CreateTableclient(tableName);
newClient.CreateIfNotExists();
TableClientCache[tableName] = newClient;
return newClient;
}
/// <summary>
/// Returns a cached instance of a TableClient for the table related to the Audit Event. Creates the Table if it does not exists.
/// </summary>
/// <param name="auditEvent">The audit event</param>
/// <param name="cancellationToken">The Cancellation Token.</param>
public async Task<TableClient> GetTableClientAsync(AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
// From custom factory
if (TableClientFactory != null)
{
return TableClientFactory.Invoke(auditEvent);
}
var tableName = TableName.GetValue(auditEvent) ?? "Audit";
if (TableClientCache.TryGetValue(tableName, out TableClient client))
{
// From Cache
return client;
}
// New client
var newClient = CreateTableclient(tableName);
await newClient.CreateIfNotExistsAsync(cancellationToken);
TableClientCache[tableName] = newClient;
return newClient;
}
private TableClient CreateTableclient(string tableName)
{
if (ConnectionString != null)
{
return new TableClient(ConnectionString, tableName, ClientOptions);
}
if (ServiceEndpoint != null)
{
if (SharedKeyCredential != null)
{
return new TableClient(ServiceEndpoint, tableName, SharedKeyCredential, ClientOptions);
}
if (SasCredential != null)
{
return new TableClient(ServiceEndpoint, SasCredential, ClientOptions);
}
if (TokenCredential != null)
{
return new TableClient(ServiceEndpoint, tableName, TokenCredential, ClientOptions);
}
return new TableClient(ServiceEndpoint, ClientOptions);
}
throw new InvalidOperationException("The Azure Tables connection string or endpoint must be provided.");
}
}
}