-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ConsulClusteringOptions.cs
74 lines (64 loc) · 2.34 KB
/
ConsulClusteringOptions.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
using System;
using Consul;
using Orleans.Runtime;
namespace Orleans.Configuration
{
/// <summary>
/// Base class for consul-cluster-options.
/// </summary>
public class ConsulClusteringOptions
{
/// <summary>
/// Consul KV root folder name.
/// </summary>
public string KvRootFolder { get; set; }
/// <summary>
/// Factory for the used Consul-Client.
/// </summary>
public Func<IConsulClient> CreateClient { get; private set; }
/// <summary>
/// Configures the <see cref="CreateClient"/> using the provided callback.
/// </summary>
public void ConfigureConsulClient(Func<IConsulClient> createClientCallback)
{
CreateClient = createClientCallback ?? throw new ArgumentNullException(nameof(createClientCallback));
}
/// <summary>
/// Configures the <see cref="CreateClient"/> using the consul-address and a acl-token.
/// </summary>
public void ConfigureConsulClient(Uri address, string aclClientToken = null)
{
if (address is null) throw new ArgumentNullException(nameof(address));
CreateClient = () => new ConsulClient(config =>
{
config.Address = address;
config.Token = aclClientToken;
});
}
public ConsulClusteringOptions()
{
this.CreateClient = () => new ConsulClient();
}
internal void Validate(string name)
{
if (CreateClient is null)
{
throw new OrleansConfigurationException($"No callback specified. Use the {GetType().Name}.{nameof(ConsulClusteringOptions.ConfigureConsulClient)} method to configure the consul client.");
}
}
}
public class ConsulClusteringOptionsValidator<TOptions> : IConfigurationValidator where TOptions : ConsulClusteringOptions
{
public ConsulClusteringOptionsValidator(TOptions options, string name = null)
{
Options = options;
Name = name;
}
public TOptions Options { get; }
public string Name { get; }
public virtual void ValidateConfiguration()
{
Options.Validate(Name);
}
}
}