Skip to content

Commit

Permalink
chore: upgrade compute samples to new surface (#1896)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 5, 2024
1 parent ebf69d4 commit 5a8554b
Show file tree
Hide file tree
Showing 25 changed files with 200 additions and 73 deletions.
2 changes: 1 addition & 1 deletion compute/firewall/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-compute": "^1.6"
"google/cloud-compute": "^1.14"
}
}
12 changes: 8 additions & 4 deletions compute/firewall/src/create_firewall_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_firewall_create]
use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\Allowed;
use Google\Cloud\Compute\V1\Firewall;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\Enums\Firewall\Direction;

/**
* To correctly handle string enums in Cloud Compute library
* use constants defined in the Enums subfolder.
*/
use Google\Cloud\Compute\V1\Enums\Firewall\Direction;
use Google\Cloud\Compute\V1\Firewall;
use Google\Cloud\Compute\V1\InsertFirewallRequest;

/**
* Creates a simple firewall rule allowing incoming HTTP and HTTPS access from the entire internet.
Expand Down Expand Up @@ -74,7 +75,10 @@ function create_firewall_rule(string $projectId, string $firewallRuleName, strin
*/

//Create the firewall rule using Firewalls Client.
$operation = $firewallsClient->insert($firewallResource, $projectId);
$request = (new InsertFirewallRequest())
->setFirewallResource($firewallResource)
->setProject($projectId);
$operation = $firewallsClient->insert($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
8 changes: 6 additions & 2 deletions compute/firewall/src/delete_firewall_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_firewall_delete]
use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\DeleteFirewallRequest;

/**
* Delete a firewall rule from the specified project.
Expand All @@ -40,7 +41,10 @@ function delete_firewall_rule(string $projectId, string $firewallRuleName)
$firewallsClient = new FirewallsClient();

// Delete the firewall rule using Firewalls Client.
$operation = $firewallsClient->delete($firewallRuleName, $projectId);
$request = (new DeleteFirewallRequest())
->setFirewall($firewallRuleName)
->setProject($projectId);
$operation = $firewallsClient->delete($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
7 changes: 5 additions & 2 deletions compute/firewall/src/list_firewall_rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_firewall_list]
use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\ListFirewallsRequest;

/**
* Return a list of all the firewall rules in specified project. Also prints the
Expand All @@ -38,7 +39,9 @@ function list_firewall_rules(string $projectId)
{
// List all firewall rules defined for the project using Firewalls Client.
$firewallClient = new FirewallsClient();
$firewallList = $firewallClient->list($projectId);
$request = (new ListFirewallsRequest())
->setProject($projectId);
$firewallList = $firewallClient->list($request);

print('--- Firewall Rules ---' . PHP_EOL);
foreach ($firewallList->iterateAllElements() as $firewall) {
Expand Down
9 changes: 7 additions & 2 deletions compute/firewall/src/patch_firewall_priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_firewall_patch]
use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\Firewall;
use Google\Cloud\Compute\V1\PatchFirewallRequest;

/**
* Modifies the priority of a given firewall rule.
Expand All @@ -44,7 +45,11 @@ function patch_firewall_priority(string $projectId, string $firewallRuleName, in

// The patch operation doesn't require the full definition of a Firewall object. It will only update
// the values that were set in it, in this case it will only change the priority.
$operation = $firewallsClient->patch($firewallRuleName, $firewallResource, $projectId);
$request = (new PatchFirewallRequest())
->setFirewall($firewallRuleName)
->setFirewallResource($firewallResource)
->setProject($projectId);
$operation = $firewallsClient->patch($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
8 changes: 6 additions & 2 deletions compute/firewall/src/print_firewall_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

namespace Google\Cloud\Samples\Compute;

use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\GetFirewallRequest;

/**
* Prints details about a particular firewall rule in the specified project.
Expand All @@ -37,7 +38,10 @@ function print_firewall_rule(string $projectId, string $firewallRuleName)
{
// Get details of a firewall rule defined for the project using Firewalls Client.
$firewallClient = new FirewallsClient();
$response = $firewallClient->get($firewallRuleName, $projectId);
$request = (new GetFirewallRequest())
->setFirewall($firewallRuleName)
->setProject($projectId);
$response = $firewallClient->get($request);
$direction = $response->getDirection();
printf('ID: %s' . PHP_EOL, $response->getID());
printf('Kind: %s' . PHP_EOL, $response->getKind());
Expand Down
60 changes: 44 additions & 16 deletions compute/helloworld/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@

require_once 'vendor/autoload.php';

use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\ZonesClient;
use Google\Cloud\Compute\V1\MachineTypesClient;
use Google\Cloud\Compute\V1\ImagesClient;
use Google\Cloud\Compute\V1\FirewallsClient;
use Google\Cloud\Compute\V1\NetworksClient;
use Google\Cloud\Compute\V1\DisksClient;
use Google\Cloud\Compute\V1\GlobalOperationsClient;
use Google\Cloud\Compute\V1\Client\DisksClient;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\Client\GlobalOperationsClient;
use Google\Cloud\Compute\V1\Client\ImagesClient;
use Google\Cloud\Compute\V1\Client\InstancesClient;
use Google\Cloud\Compute\V1\Client\MachineTypesClient;
use Google\Cloud\Compute\V1\Client\NetworksClient;
use Google\Cloud\Compute\V1\Client\ZonesClient;
use Google\Cloud\Compute\V1\ListDisksRequest;
use Google\Cloud\Compute\V1\ListFirewallsRequest;
use Google\Cloud\Compute\V1\ListGlobalOperationsRequest;
use Google\Cloud\Compute\V1\ListImagesRequest;
use Google\Cloud\Compute\V1\ListInstancesRequest;
use Google\Cloud\Compute\V1\ListMachineTypesRequest;
use Google\Cloud\Compute\V1\ListNetworksRequest;
use Google\Cloud\Compute\V1\ListZonesRequest;
use Google\Protobuf\Internal\Message;

/**
Expand Down Expand Up @@ -53,6 +61,26 @@ function print_message(Message $message)
JSON_PRETTY_PRINT
);
}

$request = (new ListInstancesRequest())
->setProject($projectId)
->setZone($zoneName);
$request2 = (new ListZonesRequest())
->setProject($projectId);
$request3 = (new ListDisksRequest())
->setProject($projectId)
->setZone($zoneName);
$request4 = (new ListMachineTypesRequest())
->setProject($projectId)
->setZone($zoneName);
$request5 = (new ListImagesRequest())
->setProject($projectId);
$request6 = (new ListFirewallsRequest())
->setProject($projectId);
$request7 = (new ListNetworksRequest())
->setProject($projectId);
$request8 = (new ListGlobalOperationsRequest())
->setProject($projectId);
?>
<!doctype html>
<html>
Expand All @@ -62,56 +90,56 @@ function print_message(Message $message)
<div class="main-content">
<h2 class="collapsible">List Instances</h2>
<div id="listInstances" class="collapsible-content">
<?php foreach ($instancesClient->list($projectId, $zoneName) as $instance): ?>
<?php foreach ($instancesClient->list($request) as $instance): ?>
<pre><?= print_message($instance) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Zones</h2>
<div id="listZones" class="collapsible-content">
<?php foreach ($zonesClient->list($projectId) as $zone): ?>
<?php foreach ($zonesClient->list($request2) as $zone): ?>
<pre><?= print_message($zone) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Disks</h2>
<div id="listDisks" class="collapsible-content">
<?php foreach ($disksClient->list($projectId, $zoneName) as $disk): ?>
<?php foreach ($disksClient->list($request3) as $disk): ?>
<pre><?= print_message($disk) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Machine Types</h2>
<div id="listMachineTypes" class="collapsible-content">
<?php foreach ($machineTypesClient->list($projectId, $zoneName) as $machineType): ?>
<?php foreach ($machineTypesClient->list($request4) as $machineType): ?>
<pre><?= print_message($machineType) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Images</h2>
<div id="listImages" class="collapsible-content">
<?php foreach ($imagesClient->list($projectId) as $image): ?>
<?php foreach ($imagesClient->list($request5) as $image): ?>
<pre><?= print_message($image) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Firewalls</h2>
<div id="listFirewalls" class="collapsible-content">
<?php foreach ($firewalls = $firewallsClient->list($projectId) as $firewall): ?>
<?php foreach ($firewalls = $firewallsClient->list($request6) as $firewall): ?>
<pre><?= print_message($firewall) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Networks</h2>
<div id="listNetworks" class="collapsible-content">
<?php foreach ($networksClient->list($projectId) as $network): ?>
<?php foreach ($networksClient->list($request7) as $network): ?>
<pre><?= print_message($network) ?></pre>
<?php endforeach ?>
</div>

<h2 class="collapsible">List Operations</h2>
<div id="listGlobalOperations" class="collapsible-content">
<?php foreach ($globalOperationsClient->list($projectId) as $operation): ?>
<?php foreach ($globalOperationsClient->list($request8) as $operation): ?>
<pre><?= print_message($operation) ?></pre>
<?php endforeach ?>
</div>
Expand Down
2 changes: 1 addition & 1 deletion compute/helloworld/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-compute": "^1.0.2"
"google/cloud-compute": "^1.14"
}
}
4 changes: 2 additions & 2 deletions compute/instances/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-compute": "^1.6",
"google/cloud-storage": "^1.26"
"google/cloud-compute": "^1.14",
"google/cloud-storage": "^1.36"
}
}
15 changes: 10 additions & 5 deletions compute/instances/src/create_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_instances_create]
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;
use Google\Cloud\Compute\V1\Client\InstancesClient;
use Google\Cloud\Compute\V1\Enums\AttachedDisk\Type;
use Google\Cloud\Compute\V1\InsertInstanceRequest;

/**
* To correctly handle string enums in Cloud Compute library
* use constants defined in the Enums subfolder.
*/
use Google\Cloud\Compute\V1\Enums\AttachedDisk\Type;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;

/**
* Creates an instance in the specified project and zone.
Expand Down Expand Up @@ -82,7 +83,11 @@ function create_instance(

// Insert the new Compute Engine instance using InstancesClient.
$instancesClient = new InstancesClient();
$operation = $instancesClient->insert($instance, $projectId, $zone);
$request = (new InsertInstanceRequest())
->setInstanceResource($instance)
->setProject($projectId)
->setZone($zone);
$operation = $instancesClient->insert($request);

# [START compute_instances_operation_check]
// Wait for the operation to complete.
Expand Down
17 changes: 11 additions & 6 deletions compute/instances/src/create_instance_with_encryption_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_instances_create_encrypted]
use Google\Cloud\Compute\V1\CustomerEncryptionKey;
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;
use Google\Cloud\Compute\V1\Client\InstancesClient;
use Google\Cloud\Compute\V1\CustomerEncryptionKey;
use Google\Cloud\Compute\V1\Enums\AttachedDisk\Type;
use Google\Cloud\Compute\V1\InsertInstanceRequest;

/**
* To correctly handle string enums in Cloud Compute library
* use constants defined in the Enums subfolder.
*/
use Google\Cloud\Compute\V1\Enums\AttachedDisk\Type;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;

/**
* Creates an instance in the specified project and zone with encrypted disk that uses customer provided key.
Expand Down Expand Up @@ -94,7 +95,11 @@ function create_instance_with_encryption_key(

// Insert the new Compute Engine instance using InstancesClient.
$instancesClient = new InstancesClient();
$operation = $instancesClient->insert($instance, $projectId, $zone);
$request = (new InsertInstanceRequest())
->setInstanceResource($instance)
->setProject($projectId)
->setZone($zone);
$operation = $instancesClient->insert($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
9 changes: 7 additions & 2 deletions compute/instances/src/delete_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_instances_delete]
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\Client\InstancesClient;
use Google\Cloud\Compute\V1\DeleteInstanceRequest;

/**
* Delete an instance.
Expand All @@ -43,7 +44,11 @@ function delete_instance(
) {
// Delete the Compute Engine instance using InstancesClient.
$instancesClient = new InstancesClient();
$operation = $instancesClient->delete($instanceName, $projectId, $zone);
$request = (new DeleteInstanceRequest())
->setInstance($instanceName)
->setProject($projectId)
->setZone($zone);
$operation = $instancesClient->delete($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
8 changes: 6 additions & 2 deletions compute/instances/src/disable_usage_export_bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
namespace Google\Cloud\Samples\Compute;

# [START compute_usage_report_disable]
use Google\Cloud\Compute\V1\ProjectsClient;
use Google\Cloud\Compute\V1\Client\ProjectsClient;
use Google\Cloud\Compute\V1\Operation;
use Google\Cloud\Compute\V1\SetUsageExportBucketProjectRequest;
use Google\Cloud\Compute\V1\UsageExportLocation;

/**
Expand All @@ -39,7 +40,10 @@ function disable_usage_export_bucket(string $projectId)
{
// Disable the usage export location by sending empty UsageExportLocation as usageExportLocationResource.
$projectsClient = new ProjectsClient();
$operation = $projectsClient->setUsageExportBucket($projectId, new UsageExportLocation());
$request = (new SetUsageExportBucketProjectRequest())
->setProject($projectId)
->setUsageExportLocationResource(new UsageExportLocation());
$operation = $projectsClient->setUsageExportBucket($request);

// Wait for the operation to complete.
$operation->pollUntilComplete();
Expand Down
Loading

0 comments on commit 5a8554b

Please sign in to comment.