Skip to content

Commit 821ad08

Browse files
committed
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
1 parent 6c3e130 commit 821ad08

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
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.3.2",
44
"google/cloud-storage": "^1.23"
55
}
66
}

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

Lines changed: 8 additions & 6 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.
@@ -82,12 +84,12 @@ function create_instance(
8284
$operation = $instancesClient->insert($instance, $projectId, $zone);
8385

8486
// 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);
87+
$operation = wait_for_operation($operation, $projectId, $zone);
88+
if (! $operation->getError()) {
89+
printf('Created instance %s' . PHP_EOL, $instanceName);
90+
} else {
91+
printf('Instance creation failed!' . PHP_EOL);
8892
}
89-
90-
printf('Created instance %s' . PHP_EOL, $instanceName);
9193
}
9294
# [END compute_instances_create]
9395

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

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +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-
/**
33-
* This method waits for an operation to be completed. Calling this function
34-
* will block until the operation is finished.
35-
*
36-
* @param Operation $operation The Operation object representing the operation you want to
37-
wait on.
38-
* @param string $projectId Your Google Cloud project ID.
39-
* @param string $zone Zone where the instance you want to delete is (like "us-central1-a").
40-
*
41-
* @throws \Google\ApiCore\ApiException if the remote call fails.
42-
* @return Operation Finished Operation object.
43-
*/
44-
function wait_for_operation(
45-
Operation $operation,
46-
string $projectId,
47-
string $zone
48-
): Operation {
49-
$operationClient = new ZoneOperationsClient();
50-
51-
while ($operation->getStatus() != Operation\Status::DONE) {
52-
// Wait for the operation to complete.
53-
$operation = $operationClient->wait($operation->getName(), $projectId, $zone);
54-
55-
if ($operation->hasError()) {
56-
printf("Operation failed with error(s): %s" . PHP_EOL, $operation->getError()->serializeToString());
57-
return $operation;
58-
}
59-
}
60-
61-
printf("Operation successful" . PHP_EOL);
62-
return $operation;
63-
}
64-
# [END compute_instances_operation_check]
6530

6631
/**
6732
* Delete an instance.
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)