Skip to content

Commit

Permalink
proposal: get endpoint and credentials from builder
Browse files Browse the repository at this point in the history
Ask the SpannerClientBuilder the endpoint and credentials that will be
used based on the given EmulatorDetection argument.
  • Loading branch information
skuruppu committed May 20, 2020
1 parent a106545 commit e98b470
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static async Task<ChannelCredentials> CreatedScopedDefaultCredentials()

internal SpannerClientCreationOptions(SpannerConnectionStringBuilder builder)
{
Endpoint = builder.EndPoint;
Endpoint = SpannerClientBuilder.MaybeEmulatorEndpoint(builder.EmulatorDetection) ?? builder.EndPoint;
_credentialsFile = builder.CredentialFile;
_credentialsOverride = builder.CredentialOverride;
EmulatorDetection = builder.EmulatorDetection;
Expand Down Expand Up @@ -135,6 +135,11 @@ public override string ToString()
/// </summary>
internal async Task<ChannelCredentials> GetCredentialsAsync()
{
var emulatorCredentials = SpannerClientBuilder.MaybeEmulatorCredentials(EmulatorDetection);
if (emulatorCredentials != null)
{
return emulatorCredentials;
}
if (_credentialsOverride != null)
{
return _credentialsOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

using Google.Api.Gax;
using Grpc.Core;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -34,6 +36,35 @@ public new EmulatorDetection EmulatorDetection
set => base.EmulatorDetection = value;
}

/// <summary>
/// Returns the endpoint that would be used under certain emulator detection states.
/// </summary>
/// <remarks>
/// If <see cref="EmulatorDetection.None"/> or <see cref="EmulatorDetection.ProductionOnly"/>,
/// returns null. Otherwise, returns the value of the emulator host environment variable.
/// </remarks>
public static string MaybeEmulatorEndpoint(EmulatorDetection emulatorDetection)
{
if (emulatorDetection is EmulatorDetection.None) { return null; }
if (emulatorDetection is EmulatorDetection.ProductionOnly) { return null; }
return Environment.GetEnvironmentVariable(s_emulatorHostEnvironmentVariable);
}

/// <summary>
/// Returns the credentials that would be used under certain emulator detection states.
/// </summary>
/// <remarks>
/// If <see cref="EmulatorDetection.None"/> or <see cref="EmulatorDetection.ProductionOnly"/>,
/// returns null. Otherwise, returns the credentials that would be used to connect to the
/// emulator.
/// </remarks>
public static ChannelCredentials MaybeEmulatorCredentials(EmulatorDetection emulatorDetection)
{
if (emulatorDetection is EmulatorDetection.None) { return null; }
if (emulatorDetection is EmulatorDetection.ProductionOnly) { return null; }
return Grpc.Core.ChannelCredentials.Insecure;
}

private const string s_emulatorHostEnvironmentVariable = "SPANNER_EMULATOR_HOST";
private static readonly string[] s_emulatorEnvironmentVariables = { s_emulatorHostEnvironmentVariable };

Expand Down

0 comments on commit e98b470

Please sign in to comment.