Skip to content

Commit

Permalink
fix: update Emulator configuration function (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajupazhamayil committed May 31, 2024
1 parent 46f8670 commit 83b772b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
45 changes: 25 additions & 20 deletions src/Generation/EmulatorSupportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Google\Generator\Ast\AST;
use Google\Generator\Ast\Access;
use Google\Generator\Ast\PhpDoc;
use Google\Generator\Utils\ResolvedType;
use Google\Generator\Utils\Type;

class EmulatorSupportGenerator
Expand All @@ -31,12 +32,16 @@ class EmulatorSupportGenerator
private static $emulatorSupportClients = [
'\Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient' => 'SPANNER_EMULATOR_HOST',
'\Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient' => 'SPANNER_EMULATOR_HOST',
'\Google\Cloud\Spanner\V1\Client\SpannerClient' => 'SPANNER_EMULATOR_HOST',
'\Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient' => 'BIGTABLE_EMULATOR_HOST',
'\Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient' => 'BIGTABLE_EMULATOR_HOST',
'\Google\Cloud\Bigtable\V2\Client\BigtableClient' => 'BIGTABLE_EMULATOR_HOST',
// Added for unittesting
'\Testing\Basic\Client\BasicClient' => 'BASIC_EMULATOR_HOST'
];

/** @var string Name of the default emulator config function. */
public const DEFAULT_EMULATOR_CONFIG_FN = 'getDefaultEmulatorConfig';
public const DEFAULT_EMULATOR_CONFIG_FN = 'setDefaultEmulatorConfig';

public static function generateEmulatorSupport(ServiceDetails $serviceDetails, SourceFileContext $ctx)
{
Expand All @@ -45,37 +50,37 @@ public static function generateEmulatorSupport(ServiceDetails $serviceDetails, S
$phpUrlSchemeConst = AST::constant('PHP_URL_SCHEME');
$schemeVar = AST::var('scheme');
$searchVar = AST::var('search');
$optionsVar = AST::var('options');
$transportConfigIndexVar = AST::index(AST::index(AST::index(AST::index(
$optionsVar,
'transportConfig'
), 'grpc'), 'stubOpts'), 'credentials');

if (!array_key_exists($fullClassName, self::$emulatorSupportClients)) {
return null;
}

return AST::method(self::DEFAULT_EMULATOR_CONFIG_FN)
->withAccess(Access::PRIVATE)
->withParams(AST::param(ResolvedType::array(), $optionsVar))
->withBody(AST::block(
AST::assign($emulatorHostVar, AST::call(AST::GET_ENV)(self::$emulatorSupportClients[$fullClassName])),
AST::if(AST::call(AST::EMPTY)($emulatorHostVar))->then(AST::return(AST::array([]))),
AST::if(AST::call(AST::EMPTY)($emulatorHostVar))->then(AST::return($optionsVar)),
AST::if(AST::assign($schemeVar, AST::call(AST::PARSE_URL)($emulatorHostVar, $phpUrlSchemeConst)))
->then(AST::block(
AST::assign($searchVar, AST::binaryOp($schemeVar, '.', '://')),
AST::assign($emulatorHostVar, AST::call(AST::STRING_REPLACE)($searchVar, '', $emulatorHostVar)),
)),
AST::return(
AST::array([
'apiEndpoint' => $emulatorHostVar,
'transportConfig' => [
'grpc' => [
'stubOpts' => [
'credentials' => AST::staticCall(
$ctx->type((Type::fromName("Grpc\ChannelCredentials"))),
AST::method('createInsecure')
)()
]
]
],
'credentials' => AST::new($ctx->type((Type::fromName("Google\ApiCore\InsecureCredentialsWrapper"))))(),
])
)
AST::nullCoalescingAssign(AST::index($optionsVar, 'apiEndpoint'), $emulatorHostVar),
AST::nullCoalescingAssign($transportConfigIndexVar, AST::staticCall(
$ctx->type((Type::fromName("Grpc\ChannelCredentials"))),
AST::method('createInsecure')
)()),
AST::nullCoalescingAssign(
AST::index($optionsVar,'credentials'),
AST::new($ctx->type((Type::fromName("Google\ApiCore\InsecureCredentialsWrapper"))))()
),
AST::return($optionsVar)
), AST::return(AST::array([])))
->withPhpDoc(PhpDoc::block(
PhpDoc::text("Configure the gapic configuration to use a service emulator.")
Expand All @@ -85,13 +90,13 @@ public static function generateEmulatorSupport(ServiceDetails $serviceDetails, S

public static function generateEmulatorOptions(ServiceDetails $serviceDetails, AST $options)
{
$getDefaultEmulatorConfig = AST::method(self::DEFAULT_EMULATOR_CONFIG_FN);
$setDefaultEmulatorConfig = AST::method(self::DEFAULT_EMULATOR_CONFIG_FN);

if (!array_key_exists($serviceDetails->gapicClientV2Type->getFullName(), self::$emulatorSupportClients)) {
return null;
}

return Ast::assign($options, Ast::binaryOp($options, '+', AST::call(AST::THIS, $getDefaultEmulatorConfig)()));
return Ast::assign($options, AST::call(AST::THIS, $setDefaultEmulatorConfig)($options));
}

public static function generateEmulatorPhpDoc(ServiceDetails $serviceDetails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public static function parseName(string $formattedName, string $template = null)
*/
public function __construct(array $options = [])
{
$options = $options + $this->getDefaultEmulatorConfig();
$options = $this->setDefaultEmulatorConfig($options);
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
$this->operationsClient = $this->createOperationsClient($clientOptions);
Expand Down Expand Up @@ -1042,28 +1042,21 @@ public function updateDatabaseDdl(UpdateDatabaseDdlRequest $request, array $call
}

/** Configure the gapic configuration to use a service emulator. */
private function getDefaultEmulatorConfig(): array
private function setDefaultEmulatorConfig(array $options): array
{
$emulatorHost = getenv('SPANNER_EMULATOR_HOST');
if (empty($emulatorHost)) {
return [];
return $options;
}

if ($scheme = parse_url($emulatorHost, PHP_URL_SCHEME)) {
$search = $scheme . '://';
$emulatorHost = str_replace($search, '', $emulatorHost);
}

return [
'apiEndpoint' => $emulatorHost,
'transportConfig' => [
'grpc' => [
'stubOpts' => [
'credentials' => ChannelCredentials::createInsecure(),
],
],
],
'credentials' => new InsecureCredentialsWrapper(),
];
$options['apiEndpoint'] ??= $emulatorHost;
$options['transportConfig']['grpc']['stubOpts']['credentials'] ??= ChannelCredentials::createInsecure();
$options['credentials'] ??= new InsecureCredentialsWrapper();
return $options;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
<?php
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

return [
'interfaces' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
<?php
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

return [
'interfaces' => [
Expand Down
21 changes: 7 additions & 14 deletions tests/Unit/ProtoTests/Basic/out/src/Client/BasicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static function getClientDefaults()
*/
public function __construct(array $options = [])
{
$options = $options + $this->getDefaultEmulatorConfig();
$options = $this->setDefaultEmulatorConfig($options);
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
}
Expand Down Expand Up @@ -220,28 +220,21 @@ public function methodWithArgs(RequestWithArgs $request, array $callOptions = []
}

/** Configure the gapic configuration to use a service emulator. */
private function getDefaultEmulatorConfig(): array
private function setDefaultEmulatorConfig(array $options): array
{
$emulatorHost = getenv('BASIC_EMULATOR_HOST');
if (empty($emulatorHost)) {
return [];
return $options;
}

if ($scheme = parse_url($emulatorHost, PHP_URL_SCHEME)) {
$search = $scheme . '://';
$emulatorHost = str_replace($search, '', $emulatorHost);
}

return [
'apiEndpoint' => $emulatorHost,
'transportConfig' => [
'grpc' => [
'stubOpts' => [
'credentials' => ChannelCredentials::createInsecure(),
],
],
],
'credentials' => new InsecureCredentialsWrapper(),
];
$options['apiEndpoint'] ??= $emulatorHost;
$options['transportConfig']['grpc']['stubOpts']['credentials'] ??= ChannelCredentials::createInsecure();
$options['credentials'] ??= new InsecureCredentialsWrapper();
return $options;
}
}
1 change: 1 addition & 0 deletions tests/scripts/run_bazel_updates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bazel run //tests/Integration:iam_update && \
bazel run //tests/Integration:logging_update && \
bazel run //tests/Integration:redis_update && \
bazel run //tests/Integration:retail_update && \
bazel run //tests/Integration:spanner_update && \
bazel run //tests/Integration:speech_update && \
bazel run //tests/Integration:securitycenter_update && \
bazel run //tests/Integration:talent_update && \
Expand Down

0 comments on commit 83b772b

Please sign in to comment.