Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional TableClientOption parameter support for Discovery.Azure DefaultAzureCredential setup #783

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net;
using Akka.Actor;
using Akka.Hosting;
using Azure.Data.Tables;
using Azure.Identity;

namespace Akka.Discovery.Azure
Expand Down Expand Up @@ -37,7 +38,7 @@ public static class AkkaHostingExtensions
/// and query this node. Defaults to 8558
/// </param>
/// <returns>
/// The same <see cref="ActorSystem"/> instance originally passed in.
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
public static AkkaConfigurationBuilder WithAzureDiscovery(
this AkkaConfigurationBuilder builder,
Expand All @@ -56,18 +57,52 @@ public static class AkkaHostingExtensions
return builder.WithAzureDiscovery(setup);
}

/// <summary>
/// Adds Akka.Discovery.Azure support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="azureTableEndpoint">
/// The <see cref="Uri"/> to the azure table endpoint, this is usually in the form of "https://{yourAccountName}.table.core.windows.net/"
/// </param>
/// <param name="azureCredential">
/// The <see cref="DefaultAzureCredential"/> instance that will be used to authorize the table client with Azure Table Storage service.
/// </param>
/// <param name="tableClientOptions">
/// Optional <see cref="TableClientOptions"/> instance to configure requests to the Azure Table Storage service.
/// </param>
/// <param name="serviceName">
/// The service name assigned to the cluster.
/// </param>
/// <param name="publicHostname">
/// The public IP/host of this node, usually for akka management. It will be used by other nodes to connect
/// and query this node. Defaults to <see cref="Dns"/>
/// </param>
/// <param name="publicPort">
/// The public port of this node, usually for akka management. It will be used by other nodes to connect
/// and query this node. Defaults to 8558
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
public static AkkaConfigurationBuilder WithAzureDiscovery(
this AkkaConfigurationBuilder builder,
Uri azureTableEndpoint,
DefaultAzureCredential azureCredential,
TableClientOptions tableClientOptions = null,
string serviceName = null,
string publicHostname = null,
int? publicPort = null)
{
if (azureCredential == null) throw new ArgumentNullException(nameof(azureCredential));
var setup = new AzureDiscoverySetup
{
AzureTableEndpoint = azureTableEndpoint,
AzureCredential = azureCredential,
TableClientOptions = tableClientOptions,
ServiceName = serviceName,
HostName = publicHostname,
Port = publicPort
Expand Down
28 changes: 21 additions & 7 deletions src/discovery/azure/Akka.Discovery.Azure/AzureDiscoverySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Net;
using Akka.Actor;
using Azure.Data.Tables;
using Azure.Identity;

namespace Akka.Discovery.Azure
Expand All @@ -26,7 +27,8 @@ public sealed class AzureDiscoverySettings
retryBackoff: TimeSpan.FromMilliseconds(500),
maximumRetryBackoff: TimeSpan.FromSeconds(5),
azureTableEndpoint: null,
azureCredential: null);
azureCredential: null,
tableClientOptions: null);

public static AzureDiscoverySettings Create(ActorSystem system)
=> Create(system.Settings.Config);
Expand Down Expand Up @@ -55,7 +57,8 @@ public static AzureDiscoverySettings Create(Configuration.Config config)
retryBackoff: cfg.GetTimeSpan("retry-backoff"),
maximumRetryBackoff: cfg.GetTimeSpan("max-retry-backoff"),
azureTableEndpoint: null,
azureCredential: null);
azureCredential: null,
tableClientOptions: null);
}

private AzureDiscoverySettings(
Expand All @@ -71,7 +74,8 @@ public static AzureDiscoverySettings Create(Configuration.Config config)
TimeSpan retryBackoff,
TimeSpan maximumRetryBackoff,
Uri azureTableEndpoint,
DefaultAzureCredential azureCredential)
DefaultAzureCredential azureCredential,
TableClientOptions tableClientOptions)
{
if (ttlHeartbeatInterval <= TimeSpan.Zero)
throw new ArgumentException("Must be greater than zero", nameof(ttlHeartbeatInterval));
Expand Down Expand Up @@ -116,6 +120,7 @@ public static AzureDiscoverySettings Create(Configuration.Config config)
MaximumRetryBackoff = maximumRetryBackoff;
AzureTableEndpoint = azureTableEndpoint;
AzureAzureCredential = azureCredential;
TableClientOptions = tableClientOptions;
}

public string ServiceName { get; }
Expand All @@ -131,6 +136,7 @@ public static AzureDiscoverySettings Create(Configuration.Config config)
public TimeSpan MaximumRetryBackoff { get; }
public Uri AzureTableEndpoint { get; }
public DefaultAzureCredential AzureAzureCredential { get; }
public TableClientOptions TableClientOptions { get; }

public override string ToString()
=> "[AzureDiscoverySettings](" +
Expand Down Expand Up @@ -179,8 +185,14 @@ public AzureDiscoverySettings WithOperationTimeout(TimeSpan operationTimeout)
public AzureDiscoverySettings WithRetryBackoff(TimeSpan retryBackoff, TimeSpan maximumRetryBackoff)
=> Copy(retryBackoff: retryBackoff, maximumRetryBackoff: maximumRetryBackoff);

public AzureDiscoverySettings WithAzureCredential(Uri azureTableEndpoint, DefaultAzureCredential credential)
=> Copy(azureTableEndpoint: azureTableEndpoint, credential: credential);
public AzureDiscoverySettings WithAzureCredential(
Uri azureTableEndpoint,
DefaultAzureCredential credential,
TableClientOptions tableClientOptions = null)
=> Copy(
azureTableEndpoint: azureTableEndpoint,
credential: credential,
tableClientOptions: tableClientOptions);

private AzureDiscoverySettings Copy(
string serviceName = null,
Expand All @@ -195,7 +207,8 @@ public AzureDiscoverySettings WithAzureCredential(Uri azureTableEndpoint, Defaul
TimeSpan? retryBackoff = null,
TimeSpan? maximumRetryBackoff = null,
Uri azureTableEndpoint = null,
DefaultAzureCredential credential = null)
DefaultAzureCredential credential = null,
TableClientOptions tableClientOptions = null)
=> new AzureDiscoverySettings(
serviceName: serviceName ?? ServiceName,
hostName: host ?? HostName,
Expand All @@ -209,6 +222,7 @@ public AzureDiscoverySettings WithAzureCredential(Uri azureTableEndpoint, Defaul
retryBackoff: retryBackoff ?? RetryBackoff,
maximumRetryBackoff: maximumRetryBackoff ?? MaximumRetryBackoff,
azureTableEndpoint: azureTableEndpoint ?? AzureTableEndpoint,
azureCredential: credential ?? AzureAzureCredential);
azureCredential: credential ?? AzureAzureCredential,
tableClientOptions: tableClientOptions ?? TableClientOptions);
}
}
13 changes: 10 additions & 3 deletions src/discovery/azure/Akka.Discovery.Azure/AzureDiscoverySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using Akka.Actor.Setup;
using Azure.Data.Tables;
using Azure.Identity;

namespace Akka.Discovery.Azure
Expand All @@ -26,7 +27,7 @@ public sealed class AzureDiscoverySetup: Setup
public TimeSpan? MaximumRetryBackoff { get; set; }
public Uri AzureTableEndpoint { get; set; }
public DefaultAzureCredential AzureCredential { get; set; }

public TableClientOptions TableClientOptions { get; set; }
public AzureDiscoverySetup WithServiceName(string serviceName)
{
ServiceName = serviceName;
Expand Down Expand Up @@ -88,10 +89,14 @@ public AzureDiscoverySetup WithRetryBackoff(TimeSpan retryBackoff, TimeSpan maxi
return this;
}

public AzureDiscoverySetup WithAzureCredential(Uri azureTableEndpoint, DefaultAzureCredential credential)
public AzureDiscoverySetup WithAzureCredential(
Uri azureTableEndpoint,
DefaultAzureCredential credential,
TableClientOptions tableClientOptions = null)
{
AzureTableEndpoint = azureTableEndpoint;
AzureCredential = credential;
TableClientOptions = tableClientOptions;
return this;
}

Expand Down Expand Up @@ -124,6 +129,8 @@ public override string ToString()
props.Add($"{nameof(AzureTableEndpoint)}:{AzureTableEndpoint}");
if(AzureCredential != null)
props.Add($"{nameof(AzureCredential)}:{AzureCredential}");
if(TableClientOptions != null)
props.Add($"{nameof(TableClientOptions)}:{TableClientOptions}");

return $"[AzureDiscoverySetup]({string.Join(", ", props)})";
}
Expand Down Expand Up @@ -151,7 +158,7 @@ public AzureDiscoverySettings Apply(AzureDiscoverySettings setting)
if (RetryBackoff != null && MaximumRetryBackoff != null)
setting = setting.WithRetryBackoff(RetryBackoff.Value, MaximumRetryBackoff.Value);
if (AzureTableEndpoint != null && AzureCredential != null)
setting = setting.WithAzureCredential(AzureTableEndpoint, AzureCredential);
setting = setting.WithAzureCredential(AzureTableEndpoint, AzureCredential, TableClientOptions);

return setting;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ClusterMemberTableClient
_log = log;
_serviceName = settings.ServiceName;
_client = (settings.AzureAzureCredential != null && settings.AzureTableEndpoint != null)
? new TableClient(settings.AzureTableEndpoint, settings.TableName, settings.AzureAzureCredential)
? new TableClient(settings.AzureTableEndpoint, settings.TableName, settings.AzureAzureCredential, settings.TableClientOptions)
: new TableClient(settings.ConnectionString, settings.TableName);
}

Expand Down