From 1ac8d14d3e4d40575020b759f78397f3081ae9da Mon Sep 17 00:00:00 2001 From: Amanda Tarafa Mas Date: Mon, 13 Jan 2025 16:32:34 -0800 Subject: [PATCH] feat: Support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable. --- Google.Api.Gax.Grpc/ClientBuilderBase.cs | 44 ++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/Google.Api.Gax.Grpc/ClientBuilderBase.cs b/Google.Api.Gax.Grpc/ClientBuilderBase.cs index c496a3b9..e1591170 100644 --- a/Google.Api.Gax.Grpc/ClientBuilderBase.cs +++ b/Google.Api.Gax.Grpc/ClientBuilderBase.cs @@ -25,6 +25,7 @@ namespace Google.Api.Gax.Grpc public abstract class ClientBuilderBase { internal const string ApiKeyHeader = "x-goog-api-key"; + internal const string UniverseDomainEnvironmentVariable = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"; /// /// The default gRPC options. @@ -61,16 +62,17 @@ public abstract class ClientBuilderBase /// /// Effective, and known, universe domain to connect to. /// Will be null if is not set and there's nothing to gain - /// from defaulting to . For instance, - /// if has been set, which is self contained, we really don't know - /// the universe we are in, and we really don't care. + /// from using the default universe domain. For instance, + /// if has been set, which is self contained, including the credential, + /// we really don't know the universe we are in, and we really don't care. /// /// /// This will be: /// /// The value of if set. /// null if is set. - /// null if both and one of the non options is used. + /// null if both and a credential is set through one of the non options. + /// The value of the environment variable if set and not empty. /// otherwise. /// /// Note that we don't validate here that the builder properties are set in a valid combination. @@ -80,11 +82,16 @@ public abstract class ClientBuilderBase #pragma warning disable CS0618 // Type or member is obsolete protected string EffectiveUniverseDomain => UniverseDomain ?? (CallInvoker is not null ? null : - Endpoint is null ? ServiceMetadata.DefaultUniverseDomain : - TokenAccessMethod is null && ChannelCredentials is null && Credential is null ? ServiceMetadata.DefaultUniverseDomain : + Endpoint is null ? EffectiveDefaultUniverseDomain : + TokenAccessMethod is null && ChannelCredentials is null && Credential is null ? EffectiveDefaultUniverseDomain : null); #pragma warning restore CS0618 // Type or member is obsolete + /// + /// The default universe domain, which is specified on if set, otherwise, . + /// + private static string EffectiveDefaultUniverseDomain => GetNonWhiteSpaceOrNullEnvironmentVariable(UniverseDomainEnvironmentVariable) ?? ServiceMetadata.DefaultUniverseDomain; + /// /// The endpoint to connect to, or null to use the default endpoint. /// @@ -356,7 +363,7 @@ protected Dictionary GetEmulatorEnvironment( Func environmentVariableProvider = null) { environmentVariableProvider ??= Environment.GetEnvironmentVariable; - var environment = allEmulatorEnvironmentVariables.ToDictionary(key => key, key => GetEnvironmentVariableOrNull(key)); + var environment = allEmulatorEnvironmentVariables.ToDictionary(key => key, key => GetNonWhiteSpaceOrNullValue(key, environmentVariableProvider)); switch (EmulatorDetection) { @@ -423,13 +430,6 @@ void CheckNotSet(object obj, string name) default: throw new InvalidOperationException($"Invalid emulator detection value: {EmulatorDetection}"); } - - // Retrieves an environment variable from , mapping empty or whitespace-only strings to null. - string GetEnvironmentVariableOrNull(string variable) - { - var value = environmentVariableProvider(variable); - return string.IsNullOrWhiteSpace(value) ? null : value; - } } /// @@ -750,6 +750,22 @@ public async Task BuildAsync(IServiceProvider provider, CancellationTok protected virtual ChannelBase CreateChannel(string endpoint, ChannelCredentials credentials) => LastCreatedChannel = EffectiveGrpcAdapter.CreateChannel(ServiceMetadata, endpoint, credentials, GetChannelOptions()); + /// + /// Retrieves the value of the environment variable with , + /// mapping empty or whitespace-only strings to null. + /// + private static string GetNonWhiteSpaceOrNullEnvironmentVariable(string name) => GetNonWhiteSpaceOrNullValue(name, Environment.GetEnvironmentVariable); + + /// + /// Retrieves a value with from , + /// mapping empty or whitespace-only strings to null. + /// + private static string GetNonWhiteSpaceOrNullValue(string valueName, Func valueProvider) + { + var value = valueProvider(valueName); + return string.IsNullOrWhiteSpace(value) ? null : value; + } + private class DelegatedTokenAccess : ITokenAccessWithHeaders { private readonly Func> _tokenAccessMethod;