Skip to content

Commit a1468a8

Browse files
authored
feat(compute): support for full LRO and string enums (GoogleCloudPlatform#1556)
1 parent 03f9756 commit a1468a8

11 files changed

+58
-144
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.4.0",
3+
"google/cloud-compute": "^0.5.0",
44
"google/cloud-storage": "^1.23"
55
}
66
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
include_once 'wait_for_operation.php';
27-
2826
# [START compute_firewall_create]
2927
use Google\Cloud\Compute\V1\FirewallsClient;
3028
use Google\Cloud\Compute\V1\Allowed;
@@ -47,6 +45,7 @@
4745
* global/networks/{network}
4846
*
4947
* @throws \Google\ApiCore\ApiException if the remote call fails.
48+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
5049
*/
5150

5251
function create_firewall_rule(string $projectId, string $firewallRuleName, string $network = 'global/networks/default')
@@ -57,7 +56,7 @@ function create_firewall_rule(string $projectId, string $firewallRuleName, strin
5756
->setPorts(['80', '443']);
5857
$firewallResource = (new Firewall())
5958
->setName($firewallRuleName)
60-
->setDirection(Direction::INGRESS)
59+
->setDirection(Direction::name(Direction::INGRESS))
6160
->setAllowed([$allowedPorts])
6261
->setSourceRanges(['0.0.0.0/0'])
6362
->setTargetTags(['web'])
@@ -77,13 +76,13 @@ function create_firewall_rule(string $projectId, string $firewallRuleName, strin
7776
//Create the firewall rule using Firewalls Client.
7877
$operation = $firewallsClient->insert($firewallResource, $projectId);
7978

80-
// Wait for the create operation to complete using a custom helper function.
81-
// @see src/wait_for_operation.php
82-
$operation = wait_for_operation($operation, $projectId);
83-
if (empty($operation->getError())) {
79+
// Wait for the operation to complete.
80+
$operation->pollUntilComplete();
81+
if ($operation->operationSucceeded()) {
8482
printf('Created rule %s.' . PHP_EOL, $firewallRuleName);
8583
} else {
86-
printf('Firewall rule creation failed!' . PHP_EOL);
84+
$error = $operation->getError();
85+
printf('Firewall rule creation failed: %s' . PHP_EOL, $error->getMessage());
8786
}
8887
}
8988
# [END compute_firewall_create]

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
include_once 'wait_for_operation.php';
27-
2826
# [START compute_instances_create]
2927
use Google\Cloud\Compute\V1\InstancesClient;
3028
use Google\Cloud\Compute\V1\AttachedDisk;
@@ -48,6 +46,7 @@
4846
* @param string $networkName Network interface to associate with the instance.
4947
*
5048
* @throws \Google\ApiCore\ApiException if the remote call fails.
49+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
5150
*/
5251
function create_instance(
5352
string $projectId,
@@ -83,14 +82,16 @@ function create_instance(
8382
$instancesClient = new InstancesClient();
8483
$operation = $instancesClient->insert($instance, $projectId, $zone);
8584

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);
89-
if (empty($operation->getError())) {
85+
# [START compute_instances_operation_check]
86+
// Wait for the operation to complete.
87+
$operation->pollUntilComplete();
88+
if ($operation->operationSucceeded()) {
9089
printf('Created instance %s' . PHP_EOL, $instanceName);
9190
} else {
92-
printf('Instance creation failed!' . PHP_EOL);
91+
$error = $operation->getError();
92+
printf('Instance creation failed: %s' . PHP_EOL, $error->getMessage());
9393
}
94+
# [END compute_instances_operation_check]
9495
}
9596
# [END compute_instances_create]
9697

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
include_once 'wait_for_operation.php';
27-
2826
# [START compute_firewall_delete]
2927
use Google\Cloud\Compute\V1\FirewallsClient;
3028

@@ -40,6 +38,7 @@
4038
* @param string $firewallRuleName Name of the rule that is deleted.
4139
*
4240
* @throws \Google\ApiCore\ApiException if the remote call fails.
41+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
4342
*/
4443
function delete_firewall_rule(string $projectId, string $firewallRuleName)
4544
{
@@ -48,13 +47,13 @@ function delete_firewall_rule(string $projectId, string $firewallRuleName)
4847
// Delete the firewall rule using Firewalls Client.
4948
$operation = $firewallsClient->delete($firewallRuleName, $projectId);
5049

51-
// Wait for the create operation to complete using a custom helper function.
52-
// @see src/wait_for_operation.php
53-
$operation = wait_for_operation($operation, $projectId);
54-
if (empty($operation->getError())) {
50+
// Wait for the operation to complete.
51+
$operation->pollUntilComplete();
52+
if ($operation->operationSucceeded()) {
5553
printf('Rule %s deleted successfully!' . PHP_EOL, $firewallRuleName);
5654
} else {
57-
print('Deletion failed!' . PHP_EOL);
55+
$error = $operation->getError();
56+
printf('Failed to delete firewall rule: %s' . PHP_EOL, $error->getMessage());
5857
}
5958
}
6059
# [END compute_firewall_delete]

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
include_once 'wait_for_operation.php';
27-
2826
# [START compute_instances_delete]
2927
use Google\Cloud\Compute\V1\InstancesClient;
3028

@@ -40,6 +38,7 @@
4038
* @param string $instanceName Unique name for the Compute instance to delete.
4139
*
4240
* @throws \Google\ApiCore\ApiException if the remote call fails.
41+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
4342
*/
4443
function delete_instance(
4544
string $projectId,
@@ -50,13 +49,13 @@ function delete_instance(
5049
$instancesClient = new InstancesClient();
5150
$operation = $instancesClient->delete($instanceName, $projectId, $zone);
5251

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);
56-
if (empty($operation->getError())) {
52+
// Wait for the operation to complete.
53+
$operation->pollUntilComplete();
54+
if ($operation->operationSucceeded()) {
5755
printf('Deleted instance %s' . PHP_EOL, $instanceName);
5856
} else {
59-
printf('Instance deletion failed!' . PHP_EOL);
57+
$error = $operation->getError();
58+
printf('Failed to delete instance: %s' . PHP_EOL, $error->getMessage());
6059
}
6160
}
6261
# [END compute_instances_delete]

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
# [START compute_usage_report_disable]
2727
use Google\Cloud\Compute\V1\ProjectsClient;
28-
use Google\Cloud\Compute\V1\GlobalOperationsClient;
2928
use Google\Cloud\Compute\V1\Operation;
3029

3130
/**
@@ -45,13 +44,14 @@ function disable_usage_export_bucket(string $projectId)
4544
$projectsClient = new ProjectsClient();
4645
$operation = $projectsClient->setUsageExportBucket($projectId, null);
4746

48-
// Wait for the set operation to complete.
49-
if ($operation->getStatus() === Operation\Status::RUNNING) {
50-
$operationClient = new GlobalOperationsClient();
51-
$operationClient->wait($operation->getName(), $projectId);
47+
// Wait for the operation to complete.
48+
$operation->pollUntilComplete();
49+
if ($operation->operationSucceeded()) {
50+
printf('Compute Engine usage export bucket for project `%s` was disabled.', $projectId);
51+
} else {
52+
$error = $operation->getError();
53+
printf('Failed to disable usage report bucket for project `%s`: %s' . PHP_EOL, $projectId, $error->getMessage());
5254
}
53-
54-
printf('Compute Engine usage export bucket for project `%s` disabled.', $projectId);
5555
}
5656
# [END compute_usage_report_disable]
5757

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
include_once 'wait_for_operation.php';
27-
2826
# [START compute_firewall_patch]
2927
use Google\Cloud\Compute\V1\FirewallsClient;
3028
use Google\Cloud\Compute\V1\Firewall;
@@ -42,6 +40,7 @@
4240
* @param int $priority The new priority to be set for the rule.
4341
*
4442
* @throws \Google\ApiCore\ApiException if the remote call fails.
43+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
4544
*/
4645
function patch_firewall_priority(string $projectId, string $firewallRuleName, int $priority)
4746
{
@@ -52,13 +51,13 @@ function patch_firewall_priority(string $projectId, string $firewallRuleName, in
5251
// the values that were set in it, in this case it will only change the priority.
5352
$operation = $firewallsClient->patch($firewallRuleName, $firewallResource, $projectId);
5453

55-
// Wait for the create operation to complete using a custom helper function.
56-
// @see src/wait_for_operation.php
57-
$operation = wait_for_operation($operation, $projectId);
58-
if (empty($operation->getError())) {
54+
// Wait for the operation to complete.
55+
$operation->pollUntilComplete();
56+
if ($operation->operationSucceeded()) {
5957
printf('Patched %s priority to %d.' . PHP_EOL, $firewallRuleName, $priority);
6058
} else {
61-
print('Patching failed!' . PHP_EOL);
59+
$error = $operation->getError();
60+
printf('Patching failed: %s' . PHP_EOL, $error->getMessage());
6261
}
6362
}
6463
# [END compute_firewall_patch]

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace Google\Cloud\Samples\Compute;
2525

2626
use Google\Cloud\Compute\V1\FirewallsClient;
27-
use Google\Cloud\Compute\V1\Firewall\Direction;
2827

2928
/**
3029
* Prints details about a particular firewall rule in the specified project
@@ -49,11 +48,7 @@ function print_firewall_rule(string $projectId, string $firewallRuleName)
4948
printf('Kind: %s' . PHP_EOL, $response->getKind());
5049
printf('Name: %s' . PHP_EOL, $response->getName());
5150
printf('Creation Time: %s' . PHP_EOL, $response->getCreationTimestamp());
52-
if ($direction = Direction::INGRESS) {
53-
print('Direction: INGRESS' . PHP_EOL);
54-
} else {
55-
print('Direction: EGRESS' . PHP_EOL);
56-
}
51+
printf('Direction: %s' . PHP_EOL, $direction);
5752
printf('Network: %s' . PHP_EOL, $response->getNetwork());
5853
printf('Disabled: %s' . PHP_EOL, var_export($response->getDisabled(), true));
5954
printf('Priority: %s' . PHP_EOL, $response->getPriority());

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Google\Cloud\Compute\V1\ProjectsClient;
2828
use Google\Cloud\Compute\V1\UsageExportLocation;
2929
use Google\Cloud\Compute\V1\Operation;
30-
use Google\Cloud\Compute\V1\GlobalOperationsClient;
3130

3231
/**
3332
* Set Compute Engine usage export bucket for the Cloud project.
@@ -44,6 +43,7 @@
4443
* to showcase default values behavior.
4544
*
4645
* @throws \Google\ApiCore\ApiException if the remote call fails.
46+
* @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
4747
*/
4848
function set_usage_export_bucket(
4949
string $projectId,
@@ -68,19 +68,20 @@ function set_usage_export_bucket(
6868
$projectsClient = new ProjectsClient();
6969
$operation = $projectsClient->setUsageExportBucket($projectId, $usageExportLocation);
7070

71-
// Wait for the set operation to complete.
72-
if ($operation->getStatus() === Operation\Status::RUNNING) {
73-
$operationClient = new GlobalOperationsClient();
74-
$operationClient->wait($operation->getName(), $projectId);
71+
// Wait for the operation to complete.
72+
$operation->pollUntilComplete();
73+
if ($operation->operationSucceeded()) {
74+
printf(
75+
'Compute Engine usage export bucket for project `%s` set to bucket_name = `%s` with ' .
76+
'report_name_prefix = `%s`.' . PHP_EOL,
77+
$projectId,
78+
$usageExportLocation->getBucketName(),
79+
(strlen($reportNamePrefix) == 0) ? 'usage_gce' : $usageExportLocation->getReportNamePrefix()
80+
);
81+
} else {
82+
$error = $operation->getError();
83+
printf('Setting usage export bucket failed: %s' . PHP_EOL, $error->getMessage());
7584
}
76-
77-
printf(
78-
'Compute Engine usage export bucket for project `%s` set to bucket_name = `%s` with ' .
79-
'report_name_prefix = `%s`.' . PHP_EOL,
80-
$projectId,
81-
$usageExportLocation->getBucketName(),
82-
(strlen($reportNamePrefix) == 0) ? 'usage_gce' : $usageExportLocation->getReportNamePrefix()
83-
);
8485
}
8586
# [END compute_usage_report_set]
8687

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

Lines changed: 0 additions & 79 deletions
This file was deleted.

compute/cloud-client/instances/test/instancesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testSetUsageExportBucketDefaultPrefix()
129129
$output = $this->runFunctionSnippet('disable_usage_export_bucket', [
130130
'projectId' => self::$projectId
131131
]);
132-
$this->assertStringContainsString('project `' . self::$projectId . '` disabled', $output);
132+
$this->assertStringContainsString('project `' . self::$projectId . '` was disabled', $output);
133133

134134
$output = $this->runFunctionSnippet('get_usage_export_bucket', [
135135
'projectId' => self::$projectId
@@ -167,7 +167,7 @@ public function testSetUsageExportBucketCustomPrefix()
167167
$output = $this->runFunctionSnippet('disable_usage_export_bucket', [
168168
'projectId' => self::$projectId
169169
]);
170-
$this->assertStringContainsString('project `' . self::$projectId . '` disabled', $output);
170+
$this->assertStringContainsString('project `' . self::$projectId . '` was disabled', $output);
171171

172172
$output = $this->runFunctionSnippet('get_usage_export_bucket', [
173173
'projectId' => self::$projectId

0 commit comments

Comments
 (0)