Skip to content

Commit

Permalink
feat: allow setting of universe domain in environment variable (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Feb 15, 2024
1 parent da805a0 commit 6e6603b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/ClientOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ private function buildClientOptions(array $options)
'libVersion' => null,
'apiEndpoint' => null,
'clientCertSource' => null,
// if the universe domain hasn't been explicitly set, assume GDU ("googleapis.com")
'universeDomain' => GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
'universeDomain' => null,
];

$supportedTransports = $this->supportedTransports();
Expand Down Expand Up @@ -178,6 +177,11 @@ private function buildClientOptions(array $options)
$apiEndpoint = self::determineMtlsEndpoint($options['apiEndpoint']);
}

// If the user has not supplied a universe domain, use the environment variable if set.
// Otherwise, use the default ("googleapis.com").
$options['universeDomain'] ??= getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN')
?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN;

// mTLS: It is not valid to configure mTLS outside of "googleapis.com" (yet)
if (isset($options['clientCertSource'])
&& $options['universeDomain'] !== GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN
Expand Down
23 changes: 21 additions & 2 deletions tests/Tests/Unit/ClientOptionsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,17 @@ public function testMtlsClientOptionWithDefaultClientCertSource()

/**
* @dataProvider provideServiceAddressTemplate
* @runInSeparateProcess
*/
public function testServiceAddressTemplate(array $options, string $expectedEndpoint)
public function testServiceAddressTemplate(array $options, string $expectedEndpoint, string $envVar = null)
{
if ($envVar) {
putenv($envVar);
}
$client = new UniverseDomainStubClientOptionsClient();
$updatedOptions = $client->buildClientOptions($options);

$this->assertEquals($updatedOptions['apiEndpoint'], $expectedEndpoint);
$this->assertEquals($expectedEndpoint, $updatedOptions['apiEndpoint']);
}

public function provideServiceAddressTemplate()
Expand All @@ -503,6 +507,21 @@ public function provideServiceAddressTemplate()
['universeDomain' => 'foo.com', 'apiEndpoint' => 'new.test.address.com'],
'new.test.address.com', // set through api endpoint (universe domain is not used)
],
[
[],
'stub.googleapis.com',
'GOOGLE_CLOUD_UNIVERSE_DOMAIN=', // env var is ignored when empty
],
[
['universeDomain' => 'foo.com'],
'stub.foo.com',
'GOOGLE_CLOUD_UNIVERSE_DOMAIN=bar.com', // env var is ignored when client option is set
],
[
[],
'stub.bar.com',
'GOOGLE_CLOUD_UNIVERSE_DOMAIN=bar.com', // env var is used when client option isn't set
],
];
}

Expand Down

0 comments on commit 6e6603b

Please sign in to comment.