Skip to content

Commit 00bd9e2

Browse files
feat: [AiPlatform] add Model Garden deploy OSS model API (#8385)
* feat: add Model Garden deploy OSS model API PiperOrigin-RevId: 772620353 Source-Link: googleapis/googleapis@ab2a125 Source-Link: googleapis/googleapis-gen@9fad2b7 Copy-Tag: eyJwIjoiQWlQbGF0Zm9ybS8uT3dsQm90LnlhbWwiLCJoIjoiOWZhZDJiN2ZjYjE5NWMwOTFlZjk3ODAyY2Q4MzdjZjAzYzRhZjc5ZSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 5f48a8e commit 00bd9e2

14 files changed

Lines changed: 1639 additions & 20 deletions
2.18 KB
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/*
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* GENERATED CODE WARNING
20+
* This file was automatically generated - do not edit!
21+
*/
22+
23+
require_once __DIR__ . '/../../../vendor/autoload.php';
24+
25+
// [START aiplatform_v1_generated_ModelGardenService_Deploy_sync]
26+
use Google\ApiCore\ApiException;
27+
use Google\ApiCore\OperationResponse;
28+
use Google\Cloud\AIPlatform\V1\Client\ModelGardenServiceClient;
29+
use Google\Cloud\AIPlatform\V1\DeployRequest;
30+
use Google\Cloud\AIPlatform\V1\DeployResponse;
31+
use Google\Rpc\Status;
32+
33+
/**
34+
* Deploys a model to a new endpoint.
35+
*
36+
* @param string $formattedDestination The resource name of the Location to deploy the model in.
37+
* Format: `projects/{project}/locations/{location}`
38+
* Please see {@see ModelGardenServiceClient::locationName()} for help formatting this field.
39+
*/
40+
function deploy_sample(string $formattedDestination): void
41+
{
42+
// Create a client.
43+
$modelGardenServiceClient = new ModelGardenServiceClient();
44+
45+
// Prepare the request message.
46+
$request = (new DeployRequest())
47+
->setDestination($formattedDestination);
48+
49+
// Call the API and handle any network failures.
50+
try {
51+
/** @var OperationResponse $response */
52+
$response = $modelGardenServiceClient->deploy($request);
53+
$response->pollUntilComplete();
54+
55+
if ($response->operationSucceeded()) {
56+
/** @var DeployResponse $result */
57+
$result = $response->getResult();
58+
printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
59+
} else {
60+
/** @var Status $error */
61+
$error = $response->getError();
62+
printf('Operation failed with error data: %s' . PHP_EOL, $error->serializeToJsonString());
63+
}
64+
} catch (ApiException $ex) {
65+
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
66+
}
67+
}
68+
69+
/**
70+
* Helper to execute the sample.
71+
*
72+
* This sample has been automatically generated and should be regarded as a code
73+
* template only. It will require modifications to work:
74+
* - It may require correct/in-range values for request initialization.
75+
* - It may require specifying regional endpoints when creating the service client,
76+
* please see the apiEndpoint client configuration option for more details.
77+
*/
78+
function callSample(): void
79+
{
80+
$formattedDestination = ModelGardenServiceClient::locationName('[PROJECT]', '[LOCATION]');
81+
82+
deploy_sample($formattedDestination);
83+
}
84+
// [END aiplatform_v1_generated_ModelGardenService_Deploy_sync]

AiPlatform/src/V1/Client/ModelGardenServiceClient.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
use Google\ApiCore\ApiException;
2828
use Google\ApiCore\CredentialsWrapper;
2929
use Google\ApiCore\GapicClientTrait;
30+
use Google\ApiCore\OperationResponse;
3031
use Google\ApiCore\PagedListResponse;
3132
use Google\ApiCore\ResourceHelperTrait;
3233
use Google\ApiCore\RetrySettings;
3334
use Google\ApiCore\Transport\TransportInterface;
3435
use Google\ApiCore\ValidationException;
3536
use Google\Auth\FetchAuthTokenInterface;
37+
use Google\Cloud\AIPlatform\V1\DeployRequest;
3638
use Google\Cloud\AIPlatform\V1\GetPublisherModelRequest;
3739
use Google\Cloud\AIPlatform\V1\PublisherModel;
3840
use Google\Cloud\Iam\V1\GetIamPolicyRequest;
@@ -43,6 +45,8 @@
4345
use Google\Cloud\Location\GetLocationRequest;
4446
use Google\Cloud\Location\ListLocationsRequest;
4547
use Google\Cloud\Location\Location;
48+
use Google\LongRunning\Client\OperationsClient;
49+
use Google\LongRunning\Operation;
4650
use GuzzleHttp\Promise\PromiseInterface;
4751
use Psr\Log\LoggerInterface;
4852

@@ -57,6 +61,7 @@
5761
* name, and additionally a parseName method to extract the individual identifiers
5862
* contained within formatted names that are returned by the API.
5963
*
64+
* @method PromiseInterface<OperationResponse> deployAsync(DeployRequest $request, array $optionalArgs = [])
6065
* @method PromiseInterface<PublisherModel> getPublisherModelAsync(GetPublisherModelRequest $request, array $optionalArgs = [])
6166
* @method PromiseInterface<Location> getLocationAsync(GetLocationRequest $request, array $optionalArgs = [])
6267
* @method PromiseInterface<PagedListResponse> listLocationsAsync(ListLocationsRequest $request, array $optionalArgs = [])
@@ -91,6 +96,8 @@ final class ModelGardenServiceClient
9196
/** The default scopes required by the service. */
9297
public static $serviceScopes = ['https://www.googleapis.com/auth/cloud-platform'];
9398

99+
private $operationsClient;
100+
94101
private static function getClientDefaults()
95102
{
96103
return [
@@ -110,6 +117,73 @@ private static function getClientDefaults()
110117
];
111118
}
112119

120+
/**
121+
* Return an OperationsClient object with the same endpoint as $this.
122+
*
123+
* @return OperationsClient
124+
*/
125+
public function getOperationsClient()
126+
{
127+
return $this->operationsClient;
128+
}
129+
130+
/**
131+
* Resume an existing long running operation that was previously started by a long
132+
* running API method. If $methodName is not provided, or does not match a long
133+
* running API method, then the operation can still be resumed, but the
134+
* OperationResponse object will not deserialize the final response.
135+
*
136+
* @param string $operationName The name of the long running operation
137+
* @param string $methodName The name of the method used to start the operation
138+
*
139+
* @return OperationResponse
140+
*/
141+
public function resumeOperation($operationName, $methodName = null)
142+
{
143+
$options = isset($this->descriptors[$methodName]['longRunning'])
144+
? $this->descriptors[$methodName]['longRunning']
145+
: [];
146+
$operation = new OperationResponse($operationName, $this->getOperationsClient(), $options);
147+
$operation->reload();
148+
return $operation;
149+
}
150+
151+
/**
152+
* Create the default operation client for the service.
153+
*
154+
* @param array $options ClientOptions for the client.
155+
*
156+
* @return OperationsClient
157+
*/
158+
private function createOperationsClient(array $options)
159+
{
160+
// Unset client-specific configuration options
161+
unset($options['serviceName'], $options['clientConfig'], $options['descriptorsConfigPath']);
162+
163+
if (isset($options['operationsClient'])) {
164+
return $options['operationsClient'];
165+
}
166+
167+
return new OperationsClient($options);
168+
}
169+
170+
/**
171+
* Formats a string containing the fully-qualified path to represent a location
172+
* resource.
173+
*
174+
* @param string $project
175+
* @param string $location
176+
*
177+
* @return string The formatted location resource.
178+
*/
179+
public static function locationName(string $project, string $location): string
180+
{
181+
return self::getPathTemplate('location')->render([
182+
'project' => $project,
183+
'location' => $location,
184+
]);
185+
}
186+
113187
/**
114188
* Formats a string containing the fully-qualified path to represent a
115189
* publisher_model resource.
@@ -127,11 +201,32 @@ public static function publisherModelName(string $publisher, string $model): str
127201
]);
128202
}
129203

204+
/**
205+
* Formats a string containing the fully-qualified path to represent a reservation
206+
* resource.
207+
*
208+
* @param string $projectIdOrNumber
209+
* @param string $zone
210+
* @param string $reservationName
211+
*
212+
* @return string The formatted reservation resource.
213+
*/
214+
public static function reservationName(string $projectIdOrNumber, string $zone, string $reservationName): string
215+
{
216+
return self::getPathTemplate('reservation')->render([
217+
'project_id_or_number' => $projectIdOrNumber,
218+
'zone' => $zone,
219+
'reservation_name' => $reservationName,
220+
]);
221+
}
222+
130223
/**
131224
* Parses a formatted name string and returns an associative array of the components in the name.
132225
* The following name formats are supported:
133226
* Template: Pattern
227+
* - location: projects/{project}/locations/{location}
134228
* - publisherModel: publishers/{publisher}/models/{model}
229+
* - reservation: projects/{project_id_or_number}/zones/{zone}/reservations/{reservation_name}
135230
*
136231
* The optional $template argument can be supplied to specify a particular pattern,
137232
* and must match one of the templates listed above. If no $template argument is
@@ -218,6 +313,7 @@ public function __construct(array $options = [])
218313
{
219314
$clientOptions = $this->buildClientOptions($options);
220315
$this->setClientOptions($clientOptions);
316+
$this->operationsClient = $this->createOperationsClient($clientOptions);
221317
}
222318

223319
/** Handles execution of the async variants for each documented method. */
@@ -231,6 +327,32 @@ public function __call($method, $args)
231327
return call_user_func_array([$this, 'startAsyncCall'], $args);
232328
}
233329

330+
/**
331+
* Deploys a model to a new endpoint.
332+
*
333+
* The async variant is {@see ModelGardenServiceClient::deployAsync()} .
334+
*
335+
* @example samples/V1/ModelGardenServiceClient/deploy.php
336+
*
337+
* @param DeployRequest $request A request to house fields associated with the call.
338+
* @param array $callOptions {
339+
* Optional.
340+
*
341+
* @type RetrySettings|array $retrySettings
342+
* Retry settings to use for this call. Can be a {@see RetrySettings} object, or an
343+
* associative array of retry settings parameters. See the documentation on
344+
* {@see RetrySettings} for example usage.
345+
* }
346+
*
347+
* @return OperationResponse
348+
*
349+
* @throws ApiException Thrown if the API call fails.
350+
*/
351+
public function deploy(DeployRequest $request, array $callOptions = []): OperationResponse
352+
{
353+
return $this->startApiCall('Deploy', $request, $callOptions)->wait();
354+
}
355+
234356
/**
235357
* Gets a Model Garden publisher model.
236358
*

0 commit comments

Comments
 (0)