-
Notifications
You must be signed in to change notification settings - Fork 2k
/
AzureStorageOperationOptions.cs
158 lines (140 loc) · 6.1 KB
/
AzureStorageOperationOptions.cs
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
using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Data.Tables;
using Orleans.Runtime;
#if ORLEANS_CLUSTERING
namespace Orleans.Clustering.AzureStorage
#elif ORLEANS_PERSISTENCE
namespace Orleans.Persistence.AzureStorage
#elif ORLEANS_REMINDERS
namespace Orleans.Reminders.AzureStorage
#elif ORLEANS_STREAMING
namespace Orleans.Streaming.AzureStorage
#elif ORLEANS_EVENTHUBS
namespace Orleans.Streaming.EventHubs
#elif TESTER_AZUREUTILS
namespace Orleans.Tests.AzureUtils
#elif ORLEANS_TRANSACTIONS
namespace Orleans.Transactions.AzureStorage
#elif ORLEANS_DIRECTORY
namespace Orleans.GrainDirectory.AzureStorage
#else
// No default namespace intentionally to cause compile errors if something is not defined
#endif
{
public class AzureStorageOperationOptions
{
private TableServiceClient _tableServiceClient;
/// <summary>
/// Table name for Azure Storage
/// </summary>
public virtual string TableName { get; set; }
/// <summary>
/// Azure Storage Policy Options
/// </summary>
public AzureStoragePolicyOptions StoragePolicyOptions { get; } = new AzureStoragePolicyOptions();
/// <summary>
/// Options to be used when configuring the table storage client, or <see langword="null"/> to use the default options.
/// </summary>
public TableClientOptions ClientOptions { get; set; }
/// <summary>
/// The delegate used to create a <see cref="TableServiceClient"/> instance.
/// </summary>
internal Func<Task<TableServiceClient>> CreateClient { get; private set; }
/// <summary>
/// Gets or sets the client used to access the Azure Table Service.
/// </summary>
public TableServiceClient TableServiceClient
{
get => _tableServiceClient;
set
{
_tableServiceClient = value;
CreateClient = () => Task.FromResult(value);
}
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using a connection string.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(string connectionString)
{
if (string.IsNullOrWhiteSpace(connectionString)) throw new ArgumentNullException(nameof(connectionString));
TableServiceClient = new TableServiceClient(connectionString, ClientOptions);
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using an authenticated service URI.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(Uri serviceUri)
{
if (serviceUri is null) throw new ArgumentNullException(nameof(serviceUri));
TableServiceClient = new TableServiceClient(serviceUri, ClientOptions);
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using the provided callback.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(Func<Task<TableServiceClient>> createClientCallback)
{
CreateClient = createClientCallback ?? throw new ArgumentNullException(nameof(createClientCallback));
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using an authenticated service URI and a <see cref="Azure.Core.TokenCredential"/>.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(Uri serviceUri, TokenCredential tokenCredential)
{
TableServiceClient = new TableServiceClient(serviceUri, tokenCredential, ClientOptions);
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using an authenticated service URI and a <see cref="Azure.AzureSasCredential"/>.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(Uri serviceUri, AzureSasCredential azureSasCredential)
{
TableServiceClient = new TableServiceClient(serviceUri, azureSasCredential, ClientOptions);
}
/// <summary>
/// Configures the <see cref="TableServiceClient"/> using an authenticated service URI and a <see cref="TableSharedKeyCredential"/>.
/// </summary>
[Obsolete($"Set the {nameof(TableServiceClient)} property directly.")]
public void ConfigureTableServiceClient(Uri serviceUri, TableSharedKeyCredential sharedKeyCredential)
{
TableServiceClient = new TableServiceClient(serviceUri, sharedKeyCredential, ClientOptions);
}
internal void Validate(string name)
{
if (CreateClient is null)
{
throw new OrleansConfigurationException($"No credentials specified. Use the {GetType().Name}.{nameof(ConfigureTableServiceClient)} method to configure the Azure Table Service client.");
}
try
{
AzureTableUtils.ValidateTableName(TableName);
}
catch (Exception ex)
{
throw GetException($"{nameof(TableName)} is not valid.", ex);
}
Exception GetException(string message, Exception inner = null) =>
new OrleansConfigurationException($"Configuration for {GetType().Name} {name} is invalid. {message}", inner);
}
}
public class AzureStorageOperationOptionsValidator<TOptions> : IConfigurationValidator where TOptions : AzureStorageOperationOptions
{
public AzureStorageOperationOptionsValidator(TOptions options, string name = null)
{
Options = options;
Name = name;
}
public TOptions Options { get; }
public string Name { get; }
public virtual void ValidateConfiguration()
{
Options.Validate(Name);
}
}
}