Skip to content

Commit 104005b

Browse files
rsamborskibshaffer
andauthored
Refactoring compute_instances_operation_check sample (GoogleCloudPlatform#1506)
* Refactoring compute_instances_operation_check sample - moved wait_for_operation to a separate file - added autoDelete flag to disks created with the instance to make sure they are cleaned up after VM deletion - update google/cloud-compute version Co-authored-by: Brent Shaffer
1 parent a214551 commit 104005b

File tree

4 files changed

+82
-25
lines changed

4 files changed

+82
-25
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-compute": "^0.3.1",
3+
"google/cloud-compute": "^0.4.0",
44
"google/cloud-storage": "^1.23"
55
}
66
}

compute/cloud-client/instances/src/create_instance.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26+
include_once "wait_for_operation.php";
27+
2628
# [START compute_instances_create]
2729
use Google\Cloud\Compute\V1\InstancesClient;
2830
use Google\Cloud\Compute\V1\AttachedDisk;
2931
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
3032
use Google\Cloud\Compute\V1\Instance;
3133
use Google\Cloud\Compute\V1\NetworkInterface;
3234
use Google\Cloud\Compute\V1\Operation;
33-
use Google\Cloud\Compute\V1\ZoneOperationsClient;
3435

3536
/**
3637
* Create an instance in the specified project and zone.
@@ -64,6 +65,7 @@ function create_instance(
6465
->setSourceImage($sourceImage);
6566
$disk = (new AttachedDisk())
6667
->setBoot(true)
68+
->setAutoDelete(true)
6769
->setInitializeParams($diskInitializeParams);
6870

6971
// Use the network interface provided in the $networkName argument.
@@ -81,13 +83,14 @@ function create_instance(
8183
$instancesClient = new InstancesClient();
8284
$operation = $instancesClient->insert($instance, $projectId, $zone);
8385

84-
// Wait for the create operation to complete.
85-
if ($operation->getStatus() === Operation\Status::RUNNING) {
86-
$operationClient = new ZoneOperationsClient();
87-
$operationClient->wait($operation->getName(), $projectId, $zone);
86+
// Wait for the create operation to complete using a custom helper function.
87+
// @see src/wait_for_operation.php
88+
$operation = wait_for_operation($operation, $projectId, $zone);
89+
if (empty($operation->getError())) {
90+
printf('Created instance %s' . PHP_EOL, $instanceName);
91+
} else {
92+
printf('Instance creation failed!' . PHP_EOL);
8893
}
89-
90-
printf('Created instance %s' . PHP_EOL, $instanceName);
9194
}
9295
# [END compute_instances_create]
9396

compute/cloud-client/instances/src/delete_instance.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26+
include_once "wait_for_operation.php";
27+
2628
# [START compute_instances_delete]
2729
use Google\Cloud\Compute\V1\InstancesClient;
28-
# [START compute_instances_operation_check]
29-
use Google\Cloud\Compute\V1\Operation;
30-
use Google\Cloud\Compute\V1\ZoneOperationsClient;
31-
32-
# [END compute_instances_operation_check]
3330

3431
/**
3532
* Delete an instance.
@@ -53,19 +50,14 @@ function delete_instance(
5350
$instancesClient = new InstancesClient();
5451
$operation = $instancesClient->delete($instanceName, $projectId, $zone);
5552

56-
# [START compute_instances_operation_check]
57-
if ($operation->getStatus() === Operation\Status::RUNNING) {
58-
// Wait for the operation to complete.
59-
$operationClient = new ZoneOperationsClient();
60-
61-
// Default timeout of 60 s is not always enough for operation to finish,
62-
// to avoid an exception we set timeout to 180000 ms = 180 s = 3 minutes
63-
$optionalArgs = ['timeoutMillis' => 180000];
64-
$operationClient->wait($operation->getName(), $projectId, $zone, $optionalArgs);
53+
// Wait for the create operation to complete using a custom helper function.
54+
// @see src/wait_for_operation.php
55+
$operation = wait_for_operation($operation, $projectId, $zone);
56+
if (empty($operation->getError())) {
57+
printf('Deleted instance %s' . PHP_EOL, $instanceName);
58+
} else {
59+
printf('Instance deletion failed!' . PHP_EOL);
6560
}
66-
# [END compute_instances_operation_check]
67-
68-
printf('Deleted instance %s' . PHP_EOL, $instanceName);
6961
}
7062
# [END compute_instances_delete]
7163

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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+
* http://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+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
# [START compute_instances_operation_check]
27+
use Google\Cloud\Compute\V1\Operation;
28+
use Google\Cloud\Compute\V1\ZoneOperationsClient;
29+
30+
/**
31+
* This method waits for an operation to be completed. Calling this function
32+
* will block until the operation is finished.
33+
*
34+
* @param Operation $operation The Operation object representing the operation you want to
35+
wait on.
36+
* @param string $projectId Your Google Cloud project ID.
37+
* @param string $zone Zone where the instance you want to delete is (like "us-central1-a").
38+
*
39+
* @throws \Google\ApiCore\ApiException if the remote call fails.
40+
* @return Operation Finished Operation object.
41+
*/
42+
function wait_for_operation(
43+
Operation $operation,
44+
string $projectId,
45+
string $zone
46+
): Operation {
47+
$operationClient = new ZoneOperationsClient();
48+
49+
while ($operation->getStatus() != Operation\Status::DONE) {
50+
// Wait for the operation to complete.
51+
$operation = $operationClient->wait($operation->getName(), $projectId, $zone);
52+
53+
if ($operation->hasError()) {
54+
printf('Operation failed with error(s): %s' . PHP_EOL, $operation->getError()->serializeToString());
55+
return $operation;
56+
}
57+
}
58+
59+
print('Operation successful' . PHP_EOL);
60+
return $operation;
61+
}
62+
# [END compute_instances_operation_check]

0 commit comments

Comments
 (0)