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

feat: use new surface LROs in new surface clients #714

Merged
merged 17 commits into from
May 30, 2024
Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"google/gax": "^1.19.1"
"google/gax": "dev-support-operations-for-compute-v2 as 1.34"
},
"scripts": {
"update-all-tests": [
52 changes: 44 additions & 8 deletions src/Generation/GapicClientV2Generator.php
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@

use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\LongRunning\OperationsClient as LegacyOperationsClient;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\RequestParamsHeaderDescriptor;
@@ -44,6 +44,7 @@
use Google\Generator\Utils\ResolvedType;
use Google\Generator\Utils\Transport;
use Google\Generator\Utils\Type;
use Google\LongRunning\Client\OperationsClient;
use GuzzleHttp\Promise\PromiseInterface;

class GapicClientV2Generator
@@ -285,10 +286,12 @@ private function operationMethods(): Vector
if (!$this->serviceDetails->hasLro && !$this->serviceDetails->hasCustomOp) {
return Vector::new([]);
}

$ctype = $this->serviceDetails->hasCustomOp ?
$this->serviceDetails->customOperationServiceClientType :
Type::fromName(OperationsClient::class);
$ctype = $this->serviceDetails->hasCustomOp
? $this->serviceDetails->customOperationServiceClientType
: Type::fromName($this->serviceDetails->migrationMode === MigrationMode::NEW_SURFACE_ONLY
? OperationsClient::class
: LegacyOperationsClient::class
);
$methods = Vector::new([]);

// getOperationsClient returns the operation client instance.
@@ -368,6 +371,39 @@ private function operationMethods(): Vector
));
$methods = $methods->append($resumeOperation);

if ($this->serviceDetails->migrationMode === MigrationMode::NEW_SURFACE_ONLY) {
// write createOperationsClient method for new surface clients
$operationsClientType = $this->serviceDetails->hasCustomOp
? $this->ctx->type($this->serviceDetails->customOperationServiceClientType)
: $this->ctx->type(Type::fromName(OperationsClient::class));
$createOperationsClient = AST::method('createOperationsClient')
->withAccess(Access::PRIVATE)
->withParams(AST::param(ResolvedType::array(), $options))
->withBody(AST::block(
AST::call(AST::method('unset'))(
AST::index($options, 'serviceName'),
AST::index($options, 'clientConfig'),
AST::index($options, 'descriptorsConfigPath'),
),
PHP_EOL,
AST::if(AST::call(AST::method('isset'))(AST::index($options, 'operationsClient'))
)->then(AST::return(AST::index($options, 'operationsClient'))),
PHP_EOL,
AST::return(AST::new($operationsClientType)($options))
))
->withPhpDoc(PhpDoc::block(
PhpDoc::text(
'Create the default operation client for the service.',
),
PhpDoc::param(
AST::param(ResolvedType::array(), $options),
PhpDoc::text('ClientOptions for the client.')
),
PhpDoc::return($operationsClientType)
));
$methods = $methods->append($createOperationsClient);
}

return $methods;
}

@@ -459,15 +495,15 @@ private function getClientDefaults(): PhpClassMember
$credentialsConfig['useJwtAccessWithScope'] = false;
}
$clientDefaultValues['credentialsConfig'] = AST::array($credentialsConfig);

if ($this->serviceDetails->transportType !== Transport::GRPC) {
$clientDefaultValues['transportConfig'] = AST::array([
'rest' => AST::array([
'restClientConfigPath' => AST::concat(AST::__DIR__, "/../resources/{$this->serviceDetails->restConfigFilename}"),
])
]);
}

if ($this->serviceDetails->hasCustomOp) {
$clientDefaultValues['operationsClientClass'] = AST::access(
$this->ctx->type($this->serviceDetails->customOperationServiceClientType),
@@ -537,7 +573,7 @@ private function construct(): PhpClassMember
'object. Note that when this object is provided, any settings in $transportConfig, and any $apiEndpoint',
'setting, will be ignored.'
);

$transportConfigSampleValues = [
'grpc' => AST::arrayEllipsis(),
'rest' => AST::arrayEllipsis()
4 changes: 2 additions & 2 deletions src/Generation/ResourcesGenerator.php
Original file line number Diff line number Diff line change
@@ -186,7 +186,7 @@ public static function generateDescriptorConfig(ServiceDetails $serviceDetails,
->withApacheLicense($currentYear)
->withGeneratedCodeWarning()
->withBlock($codeBlock)
->toCode() . ";";
->toCode() . ";";
}

public static function customOperationDescriptor(ServiceDetails $serviceDetails, MethodDetails $method)
@@ -287,7 +287,7 @@ public static function generateRestConfig(ServiceDetails $serviceDetails, Servic
);

$currentYear = (int)date("Y");

return AST::file(null)
->withApacheLicense($currentYear)
->withGeneratedCodeWarning()
4 changes: 3 additions & 1 deletion src/Generation/ServiceDetails.php
Original file line number Diff line number Diff line change
@@ -215,7 +215,9 @@ public function __construct(

// Assuming the custom operations service client is in the same namespace as the client to generate.
$cname = $this->customOperationService->getName() . 'Client';
$this->customOperationServiceClientType = Type::fromName("{$this->namespace}\\{$cname}");
$this->customOperationServiceClientType = $this->migrationMode === MigrationMode::NEW_SURFACE_ONLY
? Type::fromName("{$this->namespace}\\Client\\{$cname}")
: Type::fromName("{$this->namespace}\\{$cname}");
}
if ($desc->hasOptions() && $desc->getOptions()->hasDeprecated()) {
$this->isDeprecated = $desc->getOptions()->getDeprecated();
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
@@ -52,6 +51,7 @@
use Google\Cloud\Iam\V1\SetIamPolicyRequest;
use Google\Cloud\Iam\V1\TestIamPermissionsRequest;
use Google\Cloud\Iam\V1\TestIamPermissionsResponse;
use Google\LongRunning\Client\OperationsClient;
use Google\LongRunning\Operation;
use GuzzleHttp\Promise\PromiseInterface;

@@ -157,6 +157,24 @@ public function resumeOperation($operationName, $methodName = null)
return $operation;
}

/**
* Create the default operation client for the service.
*
* @param array $options ClientOptions for the client.
*
* @return OperationsClient
*/
private function createOperationsClient(array $options)
{
unset($options['serviceName'], $options['clientConfig'], $options['descriptorsConfigPath']);

if (isset($options['operationsClient'])) {
return $options['operationsClient'];
}

return new OperationsClient($options);
}

/**
* Formats a string containing the fully-qualified path to represent a
* cloud_function resource.
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\GapicClientTrait;
use Google\ApiCore\LongRunning\OperationsClient;
use Google\ApiCore\OperationResponse;
use Google\ApiCore\PagedListResponse;
use Google\ApiCore\ResourceHelperTrait;
@@ -51,6 +50,7 @@
use Google\Cloud\Redis\V1\RescheduleMaintenanceRequest;
use Google\Cloud\Redis\V1\UpdateInstanceRequest;
use Google\Cloud\Redis\V1\UpgradeInstanceRequest;
use Google\LongRunning\Client\OperationsClient;
use Google\LongRunning\Operation;
use GuzzleHttp\Promise\PromiseInterface;

@@ -176,6 +176,24 @@ public function resumeOperation($operationName, $methodName = null)
return $operation;
}

/**
* Create the default operation client for the service.
*
* @param array $options ClientOptions for the client.
*
* @return OperationsClient
*/
private function createOperationsClient(array $options)
{
unset($options['serviceName'], $options['clientConfig'], $options['descriptorsConfigPath']);

if (isset($options['operationsClient'])) {
return $options['operationsClient'];
}

return new OperationsClient($options);
}

/**
* Formats a string containing the fully-qualified path to represent a instance
* resource.
2 changes: 1 addition & 1 deletion tests/Unit/ProtoTests/GoldenUpdateMain.php
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ class GoldenUpdateMain
'protoPath' => 'BasicGrpcOnly/basic-grpc-only.proto',
'migrationMode' => MigrationMode::NEW_SURFACE_ONLY,
'transport' => 'grpc'
]
],
];

public static function updateAll()
18 changes: 18 additions & 0 deletions tests/Unit/autoload.php
Original file line number Diff line number Diff line change
@@ -71,6 +71,24 @@ public function mergeFromString($s)
$pos = $colon2 + 1 + $valueLen;
}
}

// Used to create the requests for custom build methods.
public static function build(): self
{
$self = new self();
foreach (func_get_args() as $pos => $arg) {
if ($pos === 0) {
$self->setOperation($arg);
} elseif (!empty($arg)) {
$parts = explode('-', $arg);
$setter = 'set' . ucwords($parts[0]);
$self->$setter($arg);
} else {
$self->setFoo($arg);
}
}
return $self;
}
}

function protosOnDemandLoader($class)