Using client libraries Stay organized with collections Save and categorize content based on your preferences.
This document demonstrates how to use Cloud Client Libraries forCompute Engine. It describes how to list, create, and delete virtual machine(VM) instances. It also describes how to check the status of a long-runningoperation. You can run code samples from your local machine or on a VM instance,provided that you have authorized the sample correctly.
For a full list of available client libraries, including other client librariesand third-party open source libraries, seeCompute Engine Client Libraries.
Costs
In this document, you use the following billable components of Google Cloud:
- Compute Engine
To generate a cost estimate based on your projected usage, use thepricing calculator.
Before you begin
Before you begin, follow the installation and authentication setup steps onCompute Engine Client Libraries for theclient library you want to use.
The examples in this document require you to provide information about the VMyou want to create, like its location, machine type, and source image. Fordetails on those values, see the following documents:
Creating an instance
The following example defines an instance that uses a specified name, machinetype, network, source image, and disk. Then, it creates (inserts) the instancein a project and zone that you provide. In this example, the instance connectsto thedefault network, which is created automatically for every project. Youreceive a message back when the API successfully creates the instance.
When you create a virtual machine (VM) instance, you must also attach a bootdisk to the VM. The boot disk contains all of the necessary files required forstarting a VM, including a public or custom OS image.
C#
usingGoogle.Cloud.Compute.V1;usingSystem.Threading.Tasks;publicclassCreateInstanceAsyncSample{publicasyncTaskCreateInstanceAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id",stringzone="us-central1-a",stringmachineName="test-machine",stringmachineType="n1-standard-1",stringdiskImage="projects/debian-cloud/global/images/family/debian-12",longdiskSizeGb=10,stringnetworkName="default"){Instanceinstance=newInstance{Name=machineName,// See https://cloud.google.com/compute/docs/machine-types for more information on machine types.MachineType=$"zones/{zone}/machineTypes/{machineType}",// Instance creation requires at least one persistent disk.Disks={newAttachedDisk{AutoDelete=true,Boot=true,Type=ComputeEnumConstants.AttachedDisk.Type.Persistent,InitializeParams=newAttachedDiskInitializeParams{// See https://cloud.google.com/compute/docs/images for more information on available images.SourceImage=diskImage,DiskSizeGb=diskSizeGb}}},NetworkInterfaces={newNetworkInterface{Name=networkName}}};// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.InstancesClientclient=awaitInstancesClient.CreateAsync();// Insert the instance in the specified project and zone.varinstanceCreation=awaitclient.InsertAsync(projectId,zone,instance);// Wait for the operation to complete using client-side polling.// The server-side operation is not affected by polling,// and might finish successfully even if polling times out.awaitinstanceCreation.PollUntilCompletedAsync();}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")// createInstance sends an instance creation request to the Compute Engine API and waits for it to complete.funccreateInstance(wio.Writer,projectID,zone,instanceName,machineType,sourceImage,networkNamestring)error{// projectID := "your_project_id"// zone := "europe-central2-b"// instanceName := "your_instance_name"// machineType := "n1-standard-1"// sourceImage := "projects/debian-cloud/global/images/family/debian-12"// networkName := "global/networks/default"ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()req:=&computepb.InsertInstanceRequest{Project:projectID,Zone:zone,InstanceResource:&computepb.Instance{Name:proto.String(instanceName),Disks:[]*computepb.AttachedDisk{{InitializeParams:&computepb.AttachedDiskInitializeParams{DiskSizeGb:proto.Int64(10),SourceImage:proto.String(sourceImage),},AutoDelete:proto.Bool(true),Boot:proto.Bool(true),Type:proto.String(computepb.AttachedDisk_PERSISTENT.String()),},},MachineType:proto.String(fmt.Sprintf("zones/%s/machineTypes/%s",zone,machineType)),NetworkInterfaces:[]*computepb.NetworkInterface{{Name:proto.String(networkName),},},},}op,err:=instancesClient.Insert(ctx,req)iferr!=nil{returnfmt.Errorf("unable to create instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Instance created\n")returnnil}Java
importcom.google.api.gax.longrunning.OperationFuture;importcom.google.cloud.compute.v1.AttachedDisk;importcom.google.cloud.compute.v1.AttachedDisk.Type;importcom.google.cloud.compute.v1.AttachedDiskInitializeParams;importcom.google.cloud.compute.v1.InsertInstanceRequest;importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.NetworkInterface;importcom.google.cloud.compute.v1.Operation;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;publicclassCreateInstance{publicstaticvoidmain(String[]args)throwsIOException,InterruptedException,ExecutionException,TimeoutException{// TODO(developer): Replace these variables before running the sample.Stringproject="your-project-id";Stringzone="zone-name";StringinstanceName="instance-name";createInstance(project,zone,instanceName);}// Create a new instance with the provided "instanceName" value in the specified project and zone.publicstaticvoidcreateInstance(Stringproject,Stringzone,StringinstanceName)throwsIOException,InterruptedException,ExecutionException,TimeoutException{// Below are sample values that can be replaced.// machineType: machine type of the VM being created.// * This value uses the format zones/{zone}/machineTypes/{type_name}.// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types// sourceImage: path to the operating system image to mount.// * For details about images you can mount, see https://cloud.google.com/compute/docs/images// diskSizeGb: storage size of the boot disk to attach to the instance.// networkName: network interface to associate with the instance.StringmachineType=String.format("zones/%s/machineTypes/n1-standard-1",zone);StringsourceImage=String.format("projects/debian-cloud/global/images/family/%s","debian-11");longdiskSizeGb=10L;StringnetworkName="default";// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to safely// clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){// Instance creation requires at least one persistent disk and one network interface.AttachedDiskdisk=AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(Type.PERSISTENT.toString()).setDeviceName("disk-1").setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();// Use the network interface provided in the networkName argument.NetworkInterfacenetworkInterface=NetworkInterface.newBuilder().setName(networkName).build();// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.InstanceinstanceResource=Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).build();System.out.printf("Creating instance: %s at %s %n",instanceName,zone);// Insert the instance in the specified project and zone.InsertInstanceRequestinsertInstanceRequest=InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();OperationFuture<Operation,Operation>operation=instancesClient.insertAsync(insertInstanceRequest);// Wait for the operation to complete.Operationresponse=operation.get(3,TimeUnit.MINUTES);if(response.hasError()){System.out.println("Instance creation failed ! ! "+response);return;}System.out.println("Operation Status: "+response.getStatus());}}}Node.js
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b'// const instanceName = 'YOUR_INSTANCE_NAME'// const machineType = 'n1-standard-1';// const sourceImage = 'projects/debian-cloud/global/images/family/debian-11';// const networkName = 'global/networks/default';constcompute=require('@google-cloud/compute');// Create a new instance with the values provided above in the specified project and zone.asyncfunctioncreateInstance(){constinstancesClient=newcompute.InstancesClient();console.log(`Creating the${instanceName} instance in${zone}...`);const[response]=awaitinstancesClient.insert({instanceResource:{name:instanceName,disks:[{// Describe the size and source image of the boot disk to attach to the instance.initializeParams:{diskSizeGb:'10',sourceImage,},autoDelete:true,boot:true,type:'PERSISTENT',},],machineType:`zones/${zone}/machineTypes/${machineType}`,networkInterfaces:[{// Use the network interface provided in the networkName argument.name:networkName,},],},project:projectId,zone,});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the create operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Instance created.');}createInstance();PHP
use Google\Cloud\Compute\V1\AttachedDisk;use Google\Cloud\Compute\V1\AttachedDisk\Type;use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;use Google\Cloud\Compute\V1\Client\InstancesClient;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\Instance;use Google\Cloud\Compute\V1\NetworkInterface;/** * Creates an instance in the specified project and zone. * * @param string $projectId Project ID of the Cloud project to create the instance in. * @param string $zone Zone to create the instance in (like "us-central1-a"). * @param string $instanceName Unique name for this Compute Engine instance. * @param string $machineType Machine type of the instance being created. * @param string $sourceImage Boot disk image name or family. * @param string $networkName Network interface to associate with the instance. * * @throws \Google\ApiCore\ApiException if the remote call fails. * @throws \Google\ApiCore\ValidationException if local error occurs before remote call. */function create_instance( string $projectId, string $zone, string $instanceName, string $machineType = 'n1-standard-1', string $sourceImage = 'projects/debian-cloud/global/images/family/debian-11', string $networkName = 'global/networks/default') { // Set the machine type using the specified zone. $machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType); // Describe the source image of the boot disk to attach to the instance. $diskInitializeParams = (new AttachedDiskInitializeParams()) ->setSourceImage($sourceImage); $disk = (new AttachedDisk()) ->setBoot(true) ->setAutoDelete(true) ->setType(Type::name(Type::PERSISTENT)) ->setInitializeParams($diskInitializeParams); // Use the network interface provided in the $networkName argument. $network = (new NetworkInterface()) ->setName($networkName); // Create the Instance object. $instance = (new Instance()) ->setName($instanceName) ->setDisks([$disk]) ->setMachineType($machineTypeFullName) ->setNetworkInterfaces([$network]); // Insert the new Compute Engine instance using InstancesClient. $instancesClient = new InstancesClient(); $request = (new InsertInstanceRequest()) ->setInstanceResource($instance) ->setProject($projectId) ->setZone($zone); $operation = $instancesClient->insert($request); // Wait for the operation to complete. $operation->pollUntilComplete(); if ($operation->operationSucceeded()) { printf('Created instance %s' . PHP_EOL, $instanceName); } else { $error = $operation->getError(); printf('Instance creation failed: %s' . PHP_EOL, $error?->getMessage()); }}Python
from__future__importannotationsimportreimportsysfromtypingimportAnyimportwarningsfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defget_image_from_family(project:str,family:str)->compute_v1.Image:""" Retrieve the newest image that is part of a given family in a project. Args: project: project ID or project number of the Cloud project you want to get image from. family: name of the image family you want to get image from. Returns: An Image object. """image_client=compute_v1.ImagesClient()# List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-detailsnewest_image=image_client.get_from_family(project=project,family=family)returnnewest_imagedefdisk_from_image(disk_type:str,disk_size_gb:int,boot:bool,source_image:str,auto_delete:bool=True,)->compute_v1.AttachedDisk:""" Create an AttachedDisk object to be used in VM instance creation. Uses an image as the source for the new disk. Args: disk_type: the type of disk you want to create. This value uses the following format: "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". For example: "zones/us-west3-b/diskTypes/pd-ssd" disk_size_gb: size of the new disk in gigabytes boot: boolean flag indicating whether this disk should be used as a boot disk of an instance source_image: source image to use when creating this disk. You must have read access to this disk. This can be one of the publicly available images or an image from one of your projects. This value uses the following format: "projects/{project_name}/global/images/{image_name}" auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it Returns: AttachedDisk object configured to be created using the specified image. """boot_disk=compute_v1.AttachedDisk()initialize_params=compute_v1.AttachedDiskInitializeParams()initialize_params.source_image=source_imageinitialize_params.disk_size_gb=disk_size_gbinitialize_params.disk_type=disk_typeboot_disk.initialize_params=initialize_params# Remember to set auto_delete to True if you want the disk to be deleted when you delete# your VM instance.boot_disk.auto_delete=auto_deleteboot_disk.boot=bootreturnboot_diskdefwait_for_extended_operation(operation:ExtendedOperation,verbose_name:str="operation",timeout:int=300)->Any:""" Waits for the extended (long-running) operation to complete. If the operation is successful, it will return its result. If the operation ends with an error, an exception will be raised. If there were any warnings during the execution of the operation they will be printed to sys.stderr. Args: operation: a long-running operation you want to wait on. verbose_name: (optional) a more verbose name of the operation, used only during error and warning reporting. timeout: how long (in seconds) to wait for operation to finish. If None, wait indefinitely. Returns: Whatever the operation.result() returns. Raises: This method will raise the exception received from `operation.exception()` or RuntimeError if there is no exception set, but there is an `error_code` set for the `operation`. In case of an operation taking longer than `timeout` seconds to complete, a `concurrent.futures.TimeoutError` will be raised. """result=operation.result(timeout=timeout)ifoperation.error_code:print(f"Error during{verbose_name}: [Code:{operation.error_code}]:{operation.error_message}",file=sys.stderr,flush=True,)print(f"Operation ID:{operation.name}",file=sys.stderr,flush=True)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during{verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" -{warning.code}:{warning.message}",file=sys.stderr,flush=True)returnresultdefcreate_instance(project_id:str,zone:str,instance_name:str,disks:list[compute_v1.AttachedDisk],machine_type:str="n1-standard-1",network_link:str="global/networks/default",subnetwork_link:str=None,internal_ip:str=None,external_access:bool=False,external_ipv4:str=None,accelerators:list[compute_v1.AcceleratorConfig]=None,preemptible:bool=False,spot:bool=False,instance_termination_action:str="STOP",custom_hostname:str=None,delete_protection:bool=False,)->compute_v1.Instance:""" Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. disks: a list of compute_v1.AttachedDisk objects describing the disks you want to attach to your new instance. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" network_link: name of the network you want the new instance to use. For example: "global/networks/default" represents the network named "default", which is created automatically for each project. subnetwork_link: name of the subnetwork you want the new instance to use. This value uses the following format: "regions/{region}/subnetworks/{subnetwork_name}" internal_ip: internal IP address you want to assign to the new instance. By default, a free address from the pool of available internal IP addresses of used subnet will be used. external_access: boolean flag indicating if the instance should have an external IPv4 address assigned. external_ipv4: external IPv4 address to be assigned to this instance. If you specify an external IP address, it must live in the same region as the zone of the instance. This setting requires `external_access` to be set to True to work. accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible or not. Preemptible VMs have been deprecated and you should now use Spot VMs. spot: boolean value indicating if the new instance should be a Spot VM or not. instance_termination_action: What action should be taken once a Spot VM is terminated. Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be protected against deletion or not. Returns: Instance object. """instance_client=compute_v1.InstancesClient()# Use the network interface provided in the network_link argument.network_interface=compute_v1.NetworkInterface()network_interface.network=network_linkifsubnetwork_link:network_interface.subnetwork=subnetwork_linkifinternal_ip:network_interface.network_i_p=internal_ipifexternal_access:access=compute_v1.AccessConfig()access.type_=compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.nameaccess.name="External NAT"access.network_tier=access.NetworkTier.PREMIUM.nameifexternal_ipv4:access.nat_i_p=external_ipv4network_interface.access_configs=[access]# Collect information into the Instance object.instance=compute_v1.Instance()instance.network_interfaces=[network_interface]instance.name=instance_nameinstance.disks=disksifre.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$",machine_type):instance.machine_type=machine_typeelse:instance.machine_type=f"zones/{zone}/machineTypes/{machine_type}"instance.scheduling=compute_v1.Scheduling()ifaccelerators:instance.guest_accelerators=acceleratorsinstance.scheduling.on_host_maintenance=(compute_v1.Scheduling.OnHostMaintenance.TERMINATE.name)ifpreemptible:# Set the preemptible settingwarnings.warn("Preemptible VMs are being replaced by Spot VMs.",DeprecationWarning)instance.scheduling=compute_v1.Scheduling()instance.scheduling.preemptible=Trueifspot:# Set the Spot VM settinginstance.scheduling.provisioning_model=(compute_v1.Scheduling.ProvisioningModel.SPOT.name)instance.scheduling.instance_termination_action=instance_termination_actionifcustom_hostnameisnotNone:# Set the custom hostname for the instanceinstance.hostname=custom_hostnameifdelete_protection:# Set the delete protection bitinstance.deletion_protection=True# Prepare the request to insert an instance.request=compute_v1.InsertInstanceRequest()request.zone=zonerequest.project=project_idrequest.instance_resource=instance# Wait for the create operation to complete.print(f"Creating the{instance_name} instance in{zone}...")operation=instance_client.insert(request=request)wait_for_extended_operation(operation,"instance creation")print(f"Instance{instance_name} created.")returninstance_client.get(project=project_id,zone=zone,instance=instance_name)Ruby
require"google/cloud/compute/v1"# Sends an instance creation request to the Compute Engine API and waits for it to complete.## @param [String] project project ID or project number of the Cloud project you want to use.# @param [String] zone name of the zone you want to use. For example: "us-west3-b"# @param [String] instance_name name of the new virtual machine.# @param [String] machine_type machine type of the VM being created. For example: "e2-standard-2"# See https://cloud.google.com/compute/docs/machine-types for more information# on machine types.# @param [String] source_image path to the operating system image to mount on your boot# disk. This can be one of the public images# (like "projects/debian-cloud/global/images/family/debian-11")# or a private image you have access to.# See https://cloud.google.com/compute/docs/images for more information on available images.# @param [String] network_name name of the network you want the new instance to use.# For example: "global/networks/default" represents the `default`# network interface, which is created automatically for each project.defcreate_instanceproject:,zone:,instance_name:,machine_type:"n2-standard-2",source_image:"projects/debian-cloud/global/images/family/debian-11",network_name:"global/networks/default"# Initialize client that will be used to send requests. This client only needs to be created# once, and can be reused for multiple requests.client=::Google::Cloud::Compute::V1::Instances::Rest::Client.new# Construct the instance object.# It can be either a hash or ::Google::Cloud::Compute::V1::Instance instance.instance={name:instance_name,machine_type:"zones/#{zone}/machineTypes/#{machine_type}",# Instance creation requires at least one persistent disk.disks:[{auto_delete:true,boot:true,type::PERSISTENT,initialize_params:{source_image:source_image,disk_size_gb:10}}],network_interfaces:[{name:network_name}]}# Prepare a request to create the instance in the specified project and zone.request={project:project,zone:zone,instance_resource:instance}puts"Creating the#{instance_name} instance in#{zone}..."begin# Send the insert request.operation=client.insertrequest# Wait for the create operation to complete.operation=wait_until_doneoperation:operationifoperation.error?warn"Error during creation:",operation.errorelsecompute_operation=operation.operationwarn"Warning during creation:",compute_operation.warningsunlesscompute_operation.warnings.empty?puts"Instance#{instance_name} created."endrescue::Google::Cloud::Error=>ewarn"Exception during creation:",eendendRust
usegoogle_cloud_compute_v1::client::Instances;usegoogle_cloud_compute_v1::model::{AttachedDisk,AttachedDiskInitializeParams,Instance,NetworkInterface,};usegoogle_cloud_lro::Poller;pubasyncfnsample(client:&Instances,project_id:&str,name:&str)->anyhow::Result<()>{constZONE:&str="us-central1-a";letinstance=Instance::new().set_machine_type(format!("zones/{ZONE}/machineTypes/f1-micro")).set_name(name).set_description("A test VM created by the Rust client library.").set_labels([("source","compute_instances_create")]).set_disks([AttachedDisk::new().set_initialize_params(AttachedDiskInitializeParams::new().set_source_image("projects/cos-cloud/global/images/family/cos-stable"),).set_boot(true).set_auto_delete(true)]).set_network_interfaces([NetworkInterface::new().set_network("global/networks/default")]);letoperation=client.insert().set_project(project_id).set_zone(ZONE).set_body(instance).poller().until_done().await?.to_result()?;println!("Instance successfully created: {operation:?}");Ok(())}Listing instances
The following examples list instances in a project, either in one zone or acrossall zones:
C#
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Collections.Generic;usingSystem.Threading.Tasks;publicclassListZoneInstancesAsyncSample{publicasyncTask<IList<Instance>>ListZoneInstancesAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id",stringzone="us-central1-a"){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.InstancesClientclient=awaitInstancesClient.CreateAsync();IList<Instance>allInstances=newList<Instance>();// Make the request to list all VM instances in the given zone in the specified project.awaitforeach(varinstanceinclient.ListAsync(projectId,zone)){// The result is an Instance collection.Console.WriteLine($"Instance: {instance.Name}");allInstances.Add(instance);}returnallInstances;}}You can also get a list of instances across all zones in your project:
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Collections.Generic;usingSystem.Threading.Tasks;publicclassListAllInstancesAsyncSample{publicasyncTask<IList<Instance>>ListAllInstancesAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id"){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.InstancesClientclient=awaitInstancesClient.CreateAsync();IList<Instance>allInstances=newList<Instance>();// Make the request to list all VM instances in a project.awaitforeach(varinstancesByZoneinclient.AggregatedListAsync(projectId)){// The result contains a KeyValuePair collection, where the key is a zone and the value// is a collection of instances in that zone.Console.WriteLine($"Instances for zone: {instancesByZone.Key}");foreach(varinstanceininstancesByZone.Value.Instances){Console.WriteLine($"-- Name: {instance.Name}");allInstances.Add(instance);}}returnallInstances;}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/api/iterator")// listInstances prints a list of instances created in given project in given zone.funclistInstances(wio.Writer,projectID,zonestring)error{// projectID := "your_project_id"// zone := "europe-central2-b"ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()req:=&computepb.ListInstancesRequest{Project:projectID,Zone:zone,}it:=instancesClient.List(ctx,req)fmt.Fprintf(w,"Instances found in zone %s:\n",zone)for{instance,err:=it.Next()iferr==iterator.Done{break}iferr!=nil{returnerr}fmt.Fprintf(w,"- %s %s\n",instance.GetName(),instance.GetMachineType())}returnnil}You can also get a list of instances across all zones in your project:
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/api/iterator""google.golang.org/protobuf/proto")// listAllInstances prints all instances present in a project, grouped by their zone.funclistAllInstances(wio.Writer,projectIDstring)error{// projectID := "your_project_id"ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()// Use the `MaxResults` parameter to limit the number of results that the API returns per response page.req:=&computepb.AggregatedListInstancesRequest{Project:projectID,MaxResults:proto.Uint32(3),}it:=instancesClient.AggregatedList(ctx,req)fmt.Fprintf(w,"Instances found:\n")// Despite using the `MaxResults` parameter, you don't need to handle the pagination// yourself. The returned iterator object handles pagination// automatically, returning separated pages as you iterate over the results.for{pair,err:=it.Next()iferr==iterator.Done{break}iferr!=nil{returnerr}instances:=pair.Value.Instancesiflen(instances) >0{fmt.Fprintf(w,"%s\n",pair.Key)for_,instance:=rangeinstances{fmt.Fprintf(w,"- %s %s\n",instance.GetName(),instance.GetMachineType())}}}returnnil}Java
importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.InstancesClient;importjava.io.IOException;publicclassListInstance{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sampleStringproject="your-project-id";Stringzone="zone-name";listInstances(project,zone);}// List all instances in the given zone in the specified project ID.publicstaticvoidlistInstances(Stringproject,Stringzone)throwsIOException{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to// safely clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){// Set the project and zone to retrieve instances present in the zone.System.out.printf("Listing instances from %s in %s:",project,zone);for(InstancezoneInstance:instancesClient.list(project,zone).iterateAll()){System.out.println(zoneInstance.getName());}System.out.println("####### Listing instances complete #######");}}}You can also get a list of instances across all zones in your project:
importcom.google.cloud.compute.v1.AggregatedListInstancesRequest;importcom.google.cloud.compute.v1.Instance;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse;importcom.google.cloud.compute.v1.InstancesScopedList;importjava.io.IOException;importjava.util.Map;publicclassListAllInstances{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sampleStringproject="your-project-id";listAllInstances(project);}// List all instances in the specified project ID.publicstaticAggregatedListPagedResponselistAllInstances(Stringproject)throwsIOException{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to// safely clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){// Use the `setMaxResults` parameter to limit the number of results// that the API returns per response page.AggregatedListInstancesRequestaggregatedListInstancesRequest=AggregatedListInstancesRequest.newBuilder().setProject(project).setMaxResults(5).build();InstancesClient.AggregatedListPagedResponseresponse=instancesClient.aggregatedList(aggregatedListInstancesRequest);// Despite using the `setMaxResults` parameter, you don't need to handle the pagination// yourself. The returned `AggregatedListPager` object handles pagination// automatically, requesting next pages as you iterate over the results.for(Map.Entry<String,InstancesScopedList>zoneInstances:response.iterateAll()){// Instances scoped by each zoneStringzone=zoneInstances.getKey();if(!zoneInstances.getValue().getInstancesList().isEmpty()){// zoneInstances.getKey() returns the fully qualified address.// Hence, strip it to get the zone name onlySystem.out.printf("Instances at %s: ",zone.substring(zone.lastIndexOf('/')+1));for(Instanceinstance:zoneInstances.getValue().getInstancesList()){System.out.println(instance.getName());}}}System.out.println("####### Listing all instances complete #######");returnresponse;}}}Node.js
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b'constcompute=require('@google-cloud/compute');// List all instances in the given zone in the specified project.asyncfunctionlistInstances(){constinstancesClient=newcompute.InstancesClient();const[instanceList]=awaitinstancesClient.list({project:projectId,zone,});console.log(`Instances found in zone${zone}:`);for(constinstanceofinstanceList){console.log(` -${instance.name} (${instance.machineType})`);}}listInstances();You can also get a list of instances across all zones in your project:
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';constcompute=require('@google-cloud/compute');// List all instances in the specified project.asyncfunctionlistAllInstances(){constinstancesClient=newcompute.InstancesClient();//Use the `maxResults` parameter to limit the number of results that the API returns per response page.constaggListRequest=instancesClient.aggregatedListAsync({project:projectId,maxResults:5,});console.log('Instances found:');// Despite using the `maxResults` parameter, you don't need to handle the pagination// yourself. The returned object handles pagination automatically,// requesting next pages as you iterate over the results.forawait(const[zone,instancesObject]ofaggListRequest){constinstances=instancesObject.instances;if(instances &&instances.length >0){console.log(`${zone}`);for(constinstanceofinstances){console.log(` -${instance.name} (${instance.machineType})`);}}}}listAllInstances();PHP
use Google\Cloud\Compute\V1\Client\InstancesClient;use Google\Cloud\Compute\V1\ListInstancesRequest;/** * List all instances for a particular Cloud project and zone. * * @param string $projectId Your Google Cloud project ID. * @param string $zone Zone to list instances for (like "us-central1-a"). * * @throws \Google\ApiCore\ApiException if the remote call fails. */function list_instances(string $projectId, string $zone){ // List Compute Engine instances using InstancesClient. $instancesClient = new InstancesClient(); $request = (new ListInstancesRequest()) ->setProject($projectId) ->setZone($zone); $instancesList = $instancesClient->list($request); printf('Instances for %s (%s)' . PHP_EOL, $projectId, $zone); foreach ($instancesList as $instance) { printf(' - %s' . PHP_EOL, $instance->getName()); }}You can also get a list of instances across all zones in your project:
use Google\Cloud\Compute\V1\AggregatedListInstancesRequest;use Google\Cloud\Compute\V1\Client\InstancesClient;/** * List all instances for a particular Cloud project. * * @param string $projectId Your Google Cloud project ID. * * @throws \Google\ApiCore\ApiException if the remote call fails. */function list_all_instances(string $projectId){ // List Compute Engine instances using InstancesClient. $instancesClient = new InstancesClient(); $request = (new AggregatedListInstancesRequest()) ->setProject($projectId); $allInstances = $instancesClient->aggregatedList($request); printf('All instances for %s' . PHP_EOL, $projectId); foreach ($allInstances as $zone => $zoneInstances) { $instances = $zoneInstances->getInstances(); if (count($instances) > 0) { printf('Zone - %s' . PHP_EOL, $zone); foreach ($instances as $instance) { printf(' - %s' . PHP_EOL, $instance->getName()); } } }}Python
from__future__importannotationsfromcollections.abcimportIterablefromgoogle.cloudimportcompute_v1deflist_instances(project_id:str,zone:str)->Iterable[compute_v1.Instance]:""" List all instances in the given zone in the specified project. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” Returns: An iterable collection of Instance objects. """instance_client=compute_v1.InstancesClient()instance_list=instance_client.list(project=project_id,zone=zone)print(f"Instances found in zone{zone}:")forinstanceininstance_list:print(f" -{instance.name} ({instance.machine_type})")returninstance_listYou can also get a list of instances across all zones in your project:
from__future__importannotationsfromcollectionsimportdefaultdictfromcollections.abcimportIterablefromgoogle.cloudimportcompute_v1deflist_all_instances(project_id:str,)->dict[str,Iterable[compute_v1.Instance]]:""" Returns a dictionary of all instances present in a project, grouped by their zone. Args: project_id: project ID or project number of the Cloud project you want to use. Returns: A dictionary with zone names as keys (in form of "zones/{zone_name}") and iterable collections of Instance objects as values. """instance_client=compute_v1.InstancesClient()request=compute_v1.AggregatedListInstancesRequest()request.project=project_id# Use the `max_results` parameter to limit the number of results that the API returns per response page.request.max_results=50agg_list=instance_client.aggregated_list(request=request)all_instances=defaultdict(list)print("Instances found:")# Despite using the `max_results` parameter, you don't need to handle the pagination# yourself. The returned `AggregatedListPager` object handles pagination# automatically, returning separated pages as you iterate over the results.forzone,responseinagg_list:ifresponse.instances:all_instances[zone].extend(response.instances)print(f"{zone}:")forinstanceinresponse.instances:print(f" -{instance.name} ({instance.machine_type})")returnall_instancesRuby
require"google/cloud/compute/v1"# Lists all instances in the given zone in the specified project.## @param [String] project project ID or project number of the Cloud project you want to use.# @param [String] zone name of the zone you want to use. For example: "us-west3-b"# @return [Array<::Google::Cloud::Compute::V1::Instance>] Array of instances.deflist_instancesproject:,zone:# Initialize client that will be used to send requests. This client only needs to be created# once, and can be reused for multiple requests.client=::Google::Cloud::Compute::V1::Instances::Rest::Client.new# Send the request to list all VM instances in the given zone in the specified project.instance_list=client.listproject:project,zone:zoneputs"Instances found in zone#{zone}:"instances=[]instance_list.eachdo|instance|puts" -#{instance.name} (#{instance.machine_type})"instances <<instanceendinstancesendYou can also get a list of instances across all zones in your project:
require"google/cloud/compute/v1"# Returns a dictionary of all instances present in a project, grouped by their zone.## @param [String] project project ID or project number of the Cloud project you want to use.# @return [Hash<String, Array<::Google::Cloud::Compute::V1::Instance>>] A hash with zone names# as keys (in form of "zones/{zone_name}") and arrays of instances as values.deflist_all_instancesproject:# Initialize client that will be used to send requests. This client only needs to be created# once, and can be reused for multiple requests.client=::Google::Cloud::Compute::V1::Instances::Rest::Client.new# Send the request to list all VM instances in a project.agg_list=client.aggregated_listproject:projectall_instances={}puts"Instances found:"# The result contains a Map collection, where the key is a zone and the value# is a collection of instances in that zone.agg_list.eachdo|zone,list|nextiflist.instances.empty?all_instances[zone]=list.instancesputs"#{zone}:"list.instances.eachdo|instance|puts" -#{instance.name} (#{instance.machine_type})"endendall_instancesendRust
usegoogle_cloud_compute_v1::client::Instances;usegoogle_cloud_gax::paginator::ItemPaginator;pubasyncfnsample(client:&Instances,project_id:&str)->anyhow::Result<()>{letmutitems=client.aggregated_list().set_project(project_id).by_item();whileletSome((zone,scoped_list))=items.next().await.transpose()?{forinstanceinscoped_list.instances{println!("Instance {} found in zone: {zone}",instance.name.expect("name should be Some()"));}}Ok(())}Deleting an instance
The following example deletes an instance based on the project, zone, andinstance name that you provide. You receive a message back when the APIsuccessfully deletes the instance.
The delete operation isirreversible. When you only want to turn off aninstance temporarily, considerstopping orsuspending the instance instead.
If theautoDelete parameter istrue for the instance, you also delete theboot disk when you delete the instance. This setting is set totrue by defaultduring instance creation, and you canadjust itwhen your use case calls for disks and instances to be deleted separately.
C#
usingGoogle.Cloud.Compute.V1;usingSystem.Threading.Tasks;publicclassDeleteInstanceAsyncSample{publicasyncTaskDeleteInstanceAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id",stringzone="us-central1-a",stringmachineName="test-machine"){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.InstancesClientclient=awaitInstancesClient.CreateAsync();// Make the request to delete a VM instance.varinstanceDeletion=awaitclient.DeleteAsync(projectId,zone,machineName);// Wait for the operation to complete using client-side polling.awaitinstanceDeletion.PollUntilCompletedAsync();}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb")// deleteInstance sends a delete request to the Compute Engine API and waits for it to complete.funcdeleteInstance(wio.Writer,projectID,zone,instanceNamestring)error{// projectID := "your_project_id"// zone := "europe-central2-b"// instanceName := "your_instance_name"ctx:=context.Background()instancesClient,err:=compute.NewInstancesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewInstancesRESTClient: %w",err)}deferinstancesClient.Close()req:=&computepb.DeleteInstanceRequest{Project:projectID,Zone:zone,Instance:instanceName,}op,err:=instancesClient.Delete(ctx,req)iferr!=nil{returnfmt.Errorf("unable to delete instance: %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Instance deleted\n")returnnil}Java
importcom.google.api.gax.longrunning.OperationFuture;importcom.google.cloud.compute.v1.DeleteInstanceRequest;importcom.google.cloud.compute.v1.InstancesClient;importcom.google.cloud.compute.v1.Operation;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;publicclassDeleteInstance{publicstaticvoidmain(String[]args)throwsIOException,InterruptedException,ExecutionException,TimeoutException{// TODO(developer): Replace these variables before running the sample.Stringproject="your-project-id";Stringzone="zone-name";StringinstanceName="instance-name";deleteInstance(project,zone,instanceName);}// Delete the instance specified by `instanceName`// if it's present in the given project and zone.publicstaticvoiddeleteInstance(Stringproject,Stringzone,StringinstanceName)throwsIOException,InterruptedException,ExecutionException,TimeoutException{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to safely// clean up any remaining background resources.try(InstancesClientinstancesClient=InstancesClient.create()){System.out.printf("Deleting instance: %s ",instanceName);// Describe which instance is to be deleted.DeleteInstanceRequestdeleteInstanceRequest=DeleteInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();OperationFuture<Operation,Operation>operation=instancesClient.deleteAsync(deleteInstanceRequest);// Wait for the operation to complete.Operationresponse=operation.get(3,TimeUnit.MINUTES);if(response.hasError()){System.out.println("Instance deletion failed ! ! "+response);return;}System.out.println("Operation Status: "+response.getStatus());}}}Node.js
/** * TODO(developer): Uncomment these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const zone = 'europe-central2-b'// const instanceName = 'YOUR_INSTANCE_NAME';constcompute=require('@google-cloud/compute');// Delete the instance specified by `instanceName` if it's present in the given project and zone.asyncfunctiondeleteInstance(){constinstancesClient=newcompute.InstancesClient();console.log(`Deleting${instanceName} from${zone}...`);const[response]=awaitinstancesClient.delete({project:projectId,zone,instance:instanceName,});letoperation=response.latestResponse;constoperationsClient=newcompute.ZoneOperationsClient();// Wait for the delete operation to complete.while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Instance deleted.');}deleteInstance();PHP
use Google\Cloud\Compute\V1\Client\InstancesClient;use Google\Cloud\Compute\V1\DeleteInstanceRequest;/** * Delete an instance. * * @param string $projectId Your Google Cloud project ID. * @param string $zone Zone where the instance you want to delete is (like "us-central1-a"). * @param string $instanceName Unique name for the Compute instance to delete. * * @throws \Google\ApiCore\ApiException if the remote call fails. * @throws \Google\ApiCore\ValidationException if local error occurs before remote call. */function delete_instance( string $projectId, string $zone, string $instanceName) { // Delete the Compute Engine instance using InstancesClient. $instancesClient = new InstancesClient(); $request = (new DeleteInstanceRequest()) ->setInstance($instanceName) ->setProject($projectId) ->setZone($zone); $operation = $instancesClient->delete($request); // Wait for the operation to complete. $operation->pollUntilComplete(); if ($operation->operationSucceeded()) { printf('Deleted instance %s' . PHP_EOL, $instanceName); } else { $error = $operation->getError(); printf('Failed to delete instance: %s' . PHP_EOL, $error?->getMessage()); }}Python
from__future__importannotationsimportsysfromtypingimportAnyfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defwait_for_extended_operation(operation:ExtendedOperation,verbose_name:str="operation",timeout:int=300)->Any:""" Waits for the extended (long-running) operation to complete. If the operation is successful, it will return its result. If the operation ends with an error, an exception will be raised. If there were any warnings during the execution of the operation they will be printed to sys.stderr. Args: operation: a long-running operation you want to wait on. verbose_name: (optional) a more verbose name of the operation, used only during error and warning reporting. timeout: how long (in seconds) to wait for operation to finish. If None, wait indefinitely. Returns: Whatever the operation.result() returns. Raises: This method will raise the exception received from `operation.exception()` or RuntimeError if there is no exception set, but there is an `error_code` set for the `operation`. In case of an operation taking longer than `timeout` seconds to complete, a `concurrent.futures.TimeoutError` will be raised. """result=operation.result(timeout=timeout)ifoperation.error_code:print(f"Error during{verbose_name}: [Code:{operation.error_code}]:{operation.error_message}",file=sys.stderr,flush=True,)print(f"Operation ID:{operation.name}",file=sys.stderr,flush=True)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during{verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" -{warning.code}:{warning.message}",file=sys.stderr,flush=True)returnresultdefdelete_instance(project_id:str,zone:str,machine_name:str)->None:""" Send an instance deletion request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” machine_name: name of the machine you want to delete. """instance_client=compcompute_v1tInstancesClientprint(f"Deleting{machine_name} from{zone}...")operation=instinstance_client.deleteproject=project_id,zone=zone,instance=machine_name)wait_for_extended_operation(operation,"instance deletion")print(f"Instance{machine_name} deleted.")Ruby
require"google/cloud/compute/v1"# Sends an instance deletion request to the Compute Engine API and waits for it to complete.## @param [String] project project ID or project number of the Cloud project you want to use.# @param [String] zone name of the zone you want to use. For example: "us-west3-b"# @param [String] instance_name name of the instance you want to delete.defdelete_instanceproject:,zone:,instance_name:# Initialize client that will be used to send requests. This client only needs to be created# once, and can be reused for multiple requests.client=::Google::Cloud::Compute::V1::Instances::Rest::Client.newputs"Deleting#{instance_name} from#{zone}..."begin# Make the request to delete a VM instance.operation=client.deleteproject:project,zone:zone,instance:instance_name# Wait for the delete operation to complete.operation=wait_until_doneoperation:operationifoperation.error?warn"Error during deletion:",operation.errorelsecompute_operation=operation.operationwarn"Warning during creation:",compute_operation.warningsunlesscompute_operation.warnings.empty?puts"Instance#{instance_name} deleted."endrescue::Google::Cloud::Error=>ewarn"Exception during deletion:",eendendRust
usegoogle_cloud_compute_v1::client::Instances;usegoogle_cloud_lro::Poller;pubasyncfnsample(client:&Instances,project_id:&str,name:&str)->anyhow::Result<()>{constZONE:&str="us-central1-a";letoperation=client.delete().set_project(project_id).set_zone(ZONE).set_instance(name).poller().until_done().await?.to_result()?;println!("Instance successfully deleted: {operation:?}");Ok(())}Checking operation status
Requests to the Compute Engine API that modify resources such as instancesimmediately return a response acknowledging your request. The acknowledgementincludes information about the status of the requested operation.
Depending on the nature of the operation, it can take some time to complete. Thefollowing example waits until an operation completes before continuing codeexecution:
C#
usingGoogle.Cloud.Compute.V1;usingSystem.Threading.Tasks;publicclassCreateInstanceAsyncSample{publicasyncTaskCreateInstanceAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id",stringzone="us-central1-a",stringmachineName="test-machine",stringmachineType="n1-standard-1",stringdiskImage="projects/debian-cloud/global/images/family/debian-12",longdiskSizeGb=10,stringnetworkName="default"){Instanceinstance=newInstance{Name=machineName,// See https://cloud.google.com/compute/docs/machine-types for more information on machine types.MachineType=$"zones/{zone}/machineTypes/{machineType}",// Instance creation requires at least one persistent disk.Disks={newAttachedDisk{AutoDelete=true,Boot=true,Type=ComputeEnumConstants.AttachedDisk.Type.Persistent,InitializeParams=newAttachedDiskInitializeParams{// See https://cloud.google.com/compute/docs/images for more information on available images.SourceImage=diskImage,DiskSizeGb=diskSizeGb}}},NetworkInterfaces={newNetworkInterface{Name=networkName}}};// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.InstancesClientclient=awaitInstancesClient.CreateAsync();// Insert the instance in the specified project and zone.varinstanceCreation=awaitclient.InsertAsync(projectId,zone,instance);// Wait for the operation to complete using client-side polling.// The server-side operation is not affected by polling,// and might finish successfully even if polling times out.awaitinstanceCreation.PollUntilCompletedAsync();}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1")// waitForOperation waits for an operation to be completed. Calling this function will block until the operation is finished.funcwaitForOperation(wio.Writer,projectIDstring,operation*compute.Operation)error{// projectID := "your_project_id"// zone := "europe-central2-b"// opName := "your_operation_name"ctx:=context.Background()iferr:=operation.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Operation finished\n")returnnil}Java
importcom.google.cloud.compute.v1.Operation;importcom.google.cloud.compute.v1.Operation.Status;importcom.google.cloud.compute.v1.ZoneOperationsClient;importjava.io.IOException;publicclassWaitForOperation{publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{// TODO(developer): Replace these variables before running the sample.// operation: Specify the operation to wait.Stringproject="your-project-id";Operationoperation=Operation.newBuilder().build();waitForOperation(project,operation);}// Waits for the specified operation to complete.publicstaticvoidwaitForOperation(Stringproject,Operationoperation)throwsIOException{try(ZoneOperationsClientzoneOperationsClient=ZoneOperationsClient.create()){// Check if the operation hasn't been completed already.if(operation.getStatus()!=Status.DONE){Stringzone=operation.getZone();zone=zone.substring(zone.lastIndexOf("/")+1);// Wait for the operation to complete.Operationresponse=zoneOperationsClient.wait(project,zone,operation.getName());// Check if the operation has errors.if(response.hasError()){System.out.println("Error in executing the operation ! ! "+response.getError());return;}System.out.println("Operation Status: "+response.getStatus());return;}System.out.println("Operation Status: "+operation.getStatus());}}}Node.js
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const operationString = 'YOUR_OPERATION_STRING'constcompute=require('@google-cloud/compute');// Parse stringified operation to the object instance.letoperation=JSON.parse(operationString);asyncfunctionwaitForOperation(){constoperationsClient=newcompute.ZoneOperationsClient();while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,zone:operation.zone.split('/').pop(),});}console.log('Operation finished.');}waitForOperation();PHP
// Wait for the operation to complete.$operation->pollUntilComplete();if ($operation->operationSucceeded()) { printf('Created instance %s' . PHP_EOL, $instanceName);} else { $error = $operation->getError(); printf('Instance creation failed: %s' . PHP_EOL, $error?->getMessage());}Python
fromgoogle.cloudimportcompute_v1defwait_for_operation(operation:compute_v1.Operation,project_id:str)->compute_v1.Operation:""" This method waits for an operation to be completed. Calling this function will block until the operation is finished. Args: operation: The Operation object representing the operation you want to wait on. project_id: project ID or project number of the Cloud project you want to use. Returns: Finished Operation object. """kwargs={"project":project_id,"operation":operation.name}ifoperation.zone:client=compute_v1.ZoneOperationsClient()# Operation.zone is a full URL address of a zone, so we need to extract just the namekwargs["zone"]=operation.zone.rsplit("/",maxsplit=1)[1]elifoperation.region:client=compute_v1.RegionOperationsClient()# Operation.region is a full URL address of a region, so we need to extract just the namekwargs["region"]=operation.region.rsplit("/",maxsplit=1)[1]else:client=compute_v1.GlobalOperationsClient()returnclient.wait(**kwargs)Ruby
require"google/cloud/compute/v1"require"time"# Waits for an operation to be completed. Calling this method# will block until the operation is finished or timed out.## @param [::Gapic::GenericLRO::Operation] operation The operation to wait for.# @param [Numeric] timeout seconds until timeout (default is 3 minutes)# @return [::Gapic::GenericLRO::Operation] Finished Operation object.defwait_until_doneoperation:,timeout:3*60retry_policy=::Gapic::Operation::RetryPolicy.newinitial_delay:0.2,multiplier:2,max_delay:1,timeout:timeoutoperation.wait_until_done!retry_policy:retry_policyendRust
usegoogle_cloud_compute_v1::client::{Instances,ZoneOperations};usegoogle_cloud_compute_v1::model::{AttachedDisk,AttachedDiskInitializeParams,Instance,NetworkInterface,};usegoogle_cloud_compute_v1::model::{Operation,operation::Status};pubasyncfnsample(client:&Instances,project_id:&str,name:&str)->anyhow::Result<()>{constZONE:&str="us-central1-a";letoperations_client=ZoneOperations::builder().build().await?;// Start an operation, in this example we use a VM creation.letmutop=start_instance_insert(client,project_id,ZONE,name).await?;// Manually wait for the operation.letoperation=loop{ifop.status.as_ref().is_some_and(|s|s==&Status::Done){breakop;}letSome(name)=op.name.clone()else{returnErr(anyhow::Error::msg(format!("the operation name should be set, operation={op:?}")));};println!("polling operation {op:?}");op=operations_client.wait().set_project(project_id).set_zone(ZONE).set_operation(name).send().await?;};println!("Instance creation finished: {operation:?}");// Check if there was an error.ifletErr(error)=operation.to_result(){println!("Instance creation failed: {error:?}");returnErr(anyhow::Error::msg(format!("instance creation failed with: {error:?}")));}Ok(())}asyncfnstart_instance_insert(client:&Instances,project_id:&str,zone:&str,name:&str,)->anyhow::Result<Operation>{letinstance=Instance::new().set_machine_type(format!("zones/{zone}/machineTypes/f1-micro")).set_name(name).set_description("A test VM created by the Rust client library.").set_labels([("source","compute_instances_create")]).set_disks([AttachedDisk::new().set_initialize_params(AttachedDiskInitializeParams::new().set_source_image("projects/cos-cloud/global/images/family/cos-stable"),).set_boot(true).set_auto_delete(true)]).set_network_interfaces([NetworkInterface::new().set_network("global/networks/default")]);// Start the operation without waiting for it to complete. This will require// manually waiting for the operation. In most cases we recommend you use// `.poller().until_done()` instead of `.send()`:letop=client.insert().set_project(project_id).set_zone(zone).set_body(instance).send().await?;Ok(op)}Handling default values
Forsome fields that you choose not to send in requests or don't receive inresponses, the Compute Engine API assumes non-zero default values. Googlespecifies these default values inAPI reference fielddescriptions and in the comments of the protocol buffers that the clientlibraries are built on.
These protocol buffers are inprotocol buffers v3 format and report azero value for fields that aren't present, even when the Compute Engine APIserver uses a different default. Zero values for fields that aren't present areas follows:0 for numbers,false for booleans, empty for strings.
In the protocol buffers that the Compute Engine Cloud Client Librariesare built on, all non-required fields are marked as proto3optional. Considera field namedname marked asoptional in a hypothetical protocol buffermessage namedMyRecord. For that field, do the following:
When sending a request: If you intend to send an explicit zero value tothe server for the field, you must set it in the request message:
MyRecord.name="". Otherwise, the server uses its default value. Before youmake the request to the server, you can confirm based on field presence thatyou set the zero value in your code. A field is present when something likethe hypotheticalMyRecord.hasName()method returnstrue.When handling a response: If a field marked as
optionalis absent or hasan explicit zero value, check the API reference to see what value you shouldassume for the field. For some fields, a zero value in the response is a validnon-default value and represents the intended value of the field.
In general, check the API reference documentation to see how the serverinterprets an absent value or zero value for a field, as it varies by method.
For example, consider changing the prefix of the name of the usage report(reportNamePrefix) for your project. When the server receives an empty stringvalue or doesn't receive the field at all, it uses a certain default value(usage_gce, as described in the reference forprojects.setUsageExportBucket) to generatethe report.
Although the server explicitly returns an empty string forreportNamePrefix,the reference forprojects.get correctly indicates that thereport is generated with the default valueusage_gce forreportNamePrefix.
C#
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Threading.Tasks;publicclassSetUsageExportBucketAsyncSample{publicasyncTaskSetUsageExportBucketAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id",stringbucketName="usage-export-bucket",stringreportNamePrefix="usage-report"){if(string.IsNullOrEmpty(reportNamePrefix)){Console.WriteLine($"Setting {nameof(UsageExportLocation.ReportNamePrefix)} "+$"to null or empty values causes the report to have the default prefix of `usage_gce`.");}UsageExportLocationexportLocation=newUsageExportLocation{BucketName=bucketName,ReportNamePrefix=reportNamePrefix,};// Initialize the client that will be used to send project-related requests.// You should reuse the same client for multiple requests.ProjectsClientclient=awaitProjectsClient.CreateAsync();varoperation=awaitclient.SetUsageExportBucketAsync(projectId,exportLocation);// Wait for the operation to complete using client-side polling.awaitoperation.PollUntilCompletedAsync();}}Verify that the server used the default prefix valueusage_gce:
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Threading.Tasks;publicclassGetUsageExportBucketAsyncSample{publicasyncTask<Project>GetUsageExportBucketAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id"){// Initialize the client that will be used to send project-related requests.// You should reuse the same client for multiple requests.ProjectsClientclient=awaitProjectsClient.CreateAsync();Projectproject=awaitclient.GetAsync(projectId);if(project.UsageExportLocationisnull){Console.WriteLine("This project has no usage report export location configured.");}elseif(string.IsNullOrEmpty(project.UsageExportLocation.ReportNamePrefix)){Console.WriteLine($"Setting {nameof(UsageExportLocation.ReportNamePrefix)} "+$"to null or empty values causes the report to have the default prefix of `usage_gce`.");}returnproject;}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")// setUsageExportBucket sets the Compute Engine usage export bucket for the Cloud project.// This sample presents how to interpret the default value for the report name prefix parameter.funcsetUsageExportBucket(wio.Writer,projectID,bucketName,reportNamePrefixstring)error{// projectID := "your_project_id"// bucketName := "your_bucket_name"// reportNamePrefix := ""ctx:=context.Background()projectsClient,err:=compute.NewProjectsRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewProjectsRESTClient: %w",err)}deferprojectsClient.Close()// Updating the setting with an empty UsageExportLocationResource value disables the usage report generation.req:=&computepb.SetUsageExportBucketProjectRequest{Project:projectID,UsageExportLocationResource:&computepb.UsageExportLocation{BucketName:proto.String(bucketName),ReportNamePrefix:proto.String(reportNamePrefix),},}ifreportNamePrefix==""{// Sending an empty value for reportNamePrefix results in the next usage report being generated// with the default prefix value "usage_gce". (see: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)fmt.Fprintf(w,"Setting reportNamePrefix to empty value causes the report to have the default prefix value `usage_gce`.\n")}op,err:=projectsClient.SetUsageExportBucket(ctx,req)iferr!=nil{returnfmt.Errorf("unable to set usage export bucket %w",err)}iferr=op.Wait(ctx);err!=nil{returnfmt.Errorf("unable to wait for the operation: %w",err)}fmt.Fprintf(w,"Usage export bucket has been set\n")returnnil}Verify that the server used the default prefix valueusage_gce:
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/protobuf/proto")// getUsageExportBucket retrieves the Compute Engine usage export bucket for the Cloud project. Replaces the empty value returned by the API with the default value used to generate report file names.funcgetUsageExportBucket(wio.Writer,projectIDstring)error{// projectID := "your_project_id"ctx:=context.Background()projectsClient,err:=compute.NewProjectsRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewProjectsRESTClient: %w",err)}deferprojectsClient.Close()// Updating the setting with an empty UsageExportLocationResource value disables the usage report generation.req:=&computepb.GetProjectRequest{Project:projectID,}project,err:=projectsClient.Get(ctx,req)iferr!=nil{returnfmt.Errorf("unable to get project: %w",err)}usageExportLocation:=project.GetUsageExportLocation()ifusageExportLocation==nil||usageExportLocation.GetBucketName()==""{// The usage reports are disabled.returnnil}ifusageExportLocation.ReportNamePrefix!=nil{// Although the server explicitly sent the empty string value,// the next usage report generated with these settings still has the default prefix value `usage_gce`.// (see https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)fmt.Fprintf(w,"Report name prefix not set, replacing with default value of `usage_gce`.\n")usageExportLocation.ReportNamePrefix=proto.String("usage_gce")}fmt.Fprintf(w,"Returned ReportNamePrefix: %s\n",usageExportLocation.GetReportNamePrefix())returnnil}Java
importcom.google.api.gax.longrunning.OperationFuture;importcom.google.cloud.compute.v1.Operation;importcom.google.cloud.compute.v1.Project;importcom.google.cloud.compute.v1.ProjectsClient;importcom.google.cloud.compute.v1.SetUsageExportBucketProjectRequest;importcom.google.cloud.compute.v1.UsageExportLocation;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;// Set Compute Engine usage export bucket for the Cloud project.// This sample presents how to interpret the default value for the report name prefix parameter.publicstaticvoidsetUsageExportBucket(Stringproject,StringbucketName,StringreportNamePrefix)throwsIOException,InterruptedException,ExecutionException,TimeoutException{// bucketName: Cloud Storage Bucket used to store Compute Engine usage reports.// An existing Google Cloud Storage bucket is required.// reportNamePrefix: Prefix of the name of the usage report that would// store Google Compute Engine data.try(ProjectsClientprojectsClient=ProjectsClient.create()){// Initialize UsageExportLocation object with provided bucket name and report name prefix.UsageExportLocationusageExportLocation=UsageExportLocation.newBuilder().setBucketName(bucketName).setReportNamePrefix(reportNamePrefix).build();if(reportNamePrefix.length()==0){// Sending an empty value for reportNamePrefix results in the// next usage report being generated with the default prefix value "usage_gce".// (see,// https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket)System.out.println("Setting reportNamePrefix to empty value causes the "+"report to have the default value of `usage_gce`.");}// Set the usage export location.OperationFuture<Operation,Operation>operation=projectsClient.setUsageExportBucketAsync(SetUsageExportBucketProjectRequest.newBuilder().setProject(project).setUsageExportLocationResource(usageExportLocation).build());// Wait for the operation to complete.Operationresponse=operation.get(3,TimeUnit.MINUTES);if(response.hasError()){System.out.println("Setting usage export bucket failed ! ! "+response);return;}System.out.println("Operation Status: "+response.getStatus());}}Verify that the server used the default prefix valueusage_gce:
importcom.google.api.gax.longrunning.OperationFuture;importcom.google.cloud.compute.v1.Operation;importcom.google.cloud.compute.v1.Project;importcom.google.cloud.compute.v1.ProjectsClient;importcom.google.cloud.compute.v1.SetUsageExportBucketProjectRequest;importcom.google.cloud.compute.v1.UsageExportLocation;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;// Retrieve Compute Engine usage export bucket for the Cloud project.// Replaces the empty value returned by the API with the default value used// to generate report file names.publicstaticUsageExportLocationgetUsageExportBucket(Stringproject)throwsIOException{try(ProjectsClientprojectsClient=ProjectsClient.create()){// Get the usage export location for the project from the server.ProjectprojectResponse=projectsClient.get(project);// Replace the empty value returned by the API with the default value// used to generate report file names.if(projectResponse.hasUsageExportLocation()){UsageExportLocationusageExportLocation=projectResponse.getUsageExportLocation();// Verify that the server explicitly sent the optional field.if(usageExportLocation.hasReportNamePrefix()){StringreportNamePrefix=usageExportLocation.getReportNamePrefix();if(reportNamePrefix.length()==0){// Although the server explicitly sent the empty string value,// the next usage report generated with these settings still has the default// prefix value "usage_gce".// (see, https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)reportNamePrefix="usage_gce";System.out.println("Report name prefix not set, replacing with default value of `usage_gce`.");}}returnusageExportLocation;}else{// The usage reports are disabled.returnnull;}}}Node.js
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const bucketName = 'YOUR_BUCKET_NAME';constcompute=require('@google-cloud/compute');constcomputeProtos=compute.protos.google.cloud.compute.v1;asyncfunctionsetUsageExportBucket(){constusageExportLocationResource=newcomputeProtos.UsageExportLocation();usageExportLocationResource.bucketName=bucketName;usageExportLocationResource.reportNamePrefix=reportNamePrefix;if(!reportNamePrefix){// Sending an empty value for reportNamePrefix results in the next usage report being generated with the default prefix value "usage_gce".// (see: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)console.log('Setting reportNamePrefix to empty value causes the report to have the default prefix value `usage_gce`.');}// Set the usage export location.constprojectsClient=newcompute.ProjectsClient();constoperationsClient=newcompute.GlobalOperationsClient();let[operation]=awaitprojectsClient.setUsageExportBucket({project:projectId,usageExportLocationResource,});while(operation.status!=='DONE'){[operation]=awaitoperationsClient.wait({operation:operation.name,project:projectId,});}}setUsageExportBucket();Verify that the server used the default prefix valueusage_gce:
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';constcompute=require('@google-cloud/compute');asyncfunctiongetUsageExportBucket(){// Get the usage export location for the project from the server.constprojectsClient=newcompute.ProjectsClient();const[project]=awaitprojectsClient.get({project:projectId,});constusageExportLocation=project.usageExportLocation;if(!usageExportLocation||!usageExportLocation.bucketName){// The usage reports are disabled.return;}if(!usageExportLocation.reportNamePrefix){// Although the server explicitly sent the empty string value,// the next usage report generated with these settings still has the default prefix value `usage_gce`.// (see https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)console.log('Report name prefix not set, replacing with default value of `usage_gce`.');usageExportLocation.reportNamePrefix='usage_gce';}console.log('Returned reportNamePrefix:',usageExportLocation.reportNamePrefix);}getUsageExportBucket();PHP
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;/** * Set Compute Engine usage export bucket for the Cloud project. * This sample presents how to interpret the default value for the report name prefix parameter. * * @param string $projectId Your Google Cloud project ID. * @param string $bucketName Google Cloud Storage bucket used to store Compute Engine usage reports. * An existing Google Cloud Storage bucket is required. * @param string $reportNamePrefix Prefix of the usage report name which defaults to an empty string * to showcase default values behavior. * * @throws \Google\ApiCore\ApiException if the remote call fails. * @throws \Google\ApiCore\ValidationException if local error occurs before remote call. */function set_usage_export_bucket( string $projectId, string $bucketName, string $reportNamePrefix = '') { // Initialize UsageExportLocation object with provided bucket name and no report name prefix. $usageExportLocation = new UsageExportLocation(array( 'bucket_name' => $bucketName, 'report_name_prefix' => $reportNamePrefix )); if (strlen($reportNamePrefix) == 0) { // Sending empty value for report_name_prefix results in the next usage report // being generated with the default prefix value "usage_gce". // See https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket print('Setting report_name_prefix to empty value causes the ' . 'report to have the default value of `usage_gce`.' . PHP_EOL); } // Set the usage export location. $projectsClient = new ProjectsClient(); $request = (new SetUsageExportBucketProjectRequest()) ->setProject($projectId) ->setUsageExportLocationResource($usageExportLocation); $operation = $projectsClient->setUsageExportBucket($request); // Wait for the operation to complete. $operation->pollUntilComplete(); if ($operation->operationSucceeded()) { printf( 'Compute Engine usage export bucket for project `%s` set to bucket_name = `%s` with ' . 'report_name_prefix = `%s`.' . PHP_EOL, $projectId, $usageExportLocation->getBucketName(), (strlen($reportNamePrefix) == 0) ? 'usage_gce' : $usageExportLocation->getReportNamePrefix() ); } else { $error = $operation->getError(); printf('Setting usage export bucket failed: %s' . PHP_EOL, $error?->getMessage()); }}Verify that the server used the default prefix valueusage_gce:
use Google\Cloud\Compute\V1\Client\ProjectsClient;use Google\Cloud\Compute\V1\GetProjectRequest;/** * Retrieve Compute Engine usage export bucket for the Cloud project. * Replaces the empty value returned by the API with the default value used * to generate report file names. * * @param string $projectId Your Google Cloud project ID. * * @throws \Google\ApiCore\ApiException if the remote call fails. */function get_usage_export_bucket(string $projectId){ // Get the usage export location for the project from the server. $projectsClient = new ProjectsClient(); $request = (new GetProjectRequest()) ->setProject($projectId); $projectResponse = $projectsClient->get($request); // Replace the empty value returned by the API with the default value used to generate report file names. if ($projectResponse->hasUsageExportLocation()) { $responseUsageExportLocation = $projectResponse->getUsageExportLocation(); // Verify that the server explicitly sent the optional field. if ($responseUsageExportLocation->hasReportNamePrefix()) { if ($responseUsageExportLocation->getReportNamePrefix() == '') { // Although the server explicitly sent the empty string value, the next usage // report generated with these settings still has the default prefix value "usage_gce". // See https://cloud.google.com/compute/docs/reference/rest/v1/projects/get print('Report name prefix not set, replacing with default value of `usage_gce`.' . PHP_EOL); $responseUsageExportLocation->setReportNamePrefix('usage_gce'); } } printf( 'Compute Engine usage export bucket for project `%s` is bucket_name = `%s` with ' . 'report_name_prefix = `%s`.' . PHP_EOL, $projectId, $responseUsageExportLocation->getBucketName(), $responseUsageExportLocation->getReportNamePrefix() ); } else { // The usage reports are disabled. printf('Compute Engine usage export bucket for project `%s` is disabled.', $projectId); }}Python
from__future__importannotationsimportsysfromtypingimportAnyfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defwait_for_extended_operation(operation:ExtendedOperation,verbose_name:str="operation",timeout:int=300)->Any:""" Waits for the extended (long-running) operation to complete. If the operation is successful, it will return its result. If the operation ends with an error, an exception will be raised. If there were any warnings during the execution of the operation they will be printed to sys.stderr. Args: operation: a long-running operation you want to wait on. verbose_name: (optional) a more verbose name of the operation, used only during error and warning reporting. timeout: how long (in seconds) to wait for operation to finish. If None, wait indefinitely. Returns: Whatever the operation.result() returns. Raises: This method will raise the exception received from `operation.exception()` or RuntimeError if there is no exception set, but there is an `error_code` set for the `operation`. In case of an operation taking longer than `timeout` seconds to complete, a `concurrent.futures.TimeoutError` will be raised. """result=operation.result(timeout=timeout)ifoperation.error_code:print(f"Error during{verbose_name}: [Code:{operation.error_code}]:{operation.error_message}",file=sys.stderr,flush=True,)print(f"Operation ID:{operation.name}",file=sys.stderr,flush=True)raiseoperation.exception()orRuntimeError(operation.error_message)ifoperation.warnings:print(f"Warnings during{verbose_name}:\n",file=sys.stderr,flush=True)forwarninginoperation.warnings:print(f" -{warning.code}:{warning.message}",file=sys.stderr,flush=True)returnresultdefset_usage_export_bucket(project_id:str,bucket_name:str,report_name_prefix:str="")->None:""" Set Compute Engine usage export bucket for the Cloud project. This sample presents how to interpret the default value for the report name prefix parameter. Args: project_id: project ID or project number of the project to update. bucket_name: Google Cloud Storage bucket used to store Compute Engine usage reports. An existing Google Cloud Storage bucket is required. report_name_prefix: Prefix of the usage report name which defaults to an empty string to showcase default values behaviour. """usage_export_location=compute_v1.UsageExportLocation()usage_export_location.bucket_name=bucket_nameusage_export_location.report_name_prefix=report_name_prefixifnotreport_name_prefix:# Sending an empty value for report_name_prefix results in the# next usage report being generated with the default prefix value# "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket)print("Setting report_name_prefix to empty value causes the report ""to have the default prefix of `usage_gce`.")projects_client=compute_v1.ProjectsClient()operation=projects_client.set_usage_export_bucket(project=project_id,usage_export_location_resource=usage_export_location)wait_for_extended_operation(operation,"setting GCE usage bucket")Verify that the server used the default prefix valueusage_gce:
from__future__importannotationsimportsysfromtypingimportAnyfromgoogle.api_core.extended_operationimportExtendedOperationfromgoogle.cloudimportcompute_v1defget_usage_export_bucket(project_id:str)->compute_v1.UsageExportLocation:""" Retrieve Compute Engine usage export bucket for the Cloud project. Replaces the empty value returned by the API with the default value used to generate report file names. Args: project_id: project ID or project number of the project to update. Returns: UsageExportLocation object describing the current usage export settings for project project_id. """projects_client=compute_v1.ProjectsClient()project_data=projects_client.get(project=project_id)uel=project_data.usage_export_locationifnotuel.bucket_name:# The usage reports are disabled.returnuelifnotuel.report_name_prefix:# Although the server sent the empty string value, the next usage report# generated with these settings still has the default prefix value# "usage_gce". (see https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)print("Report name prefix not set, replacing with default value of ""`usage_gce`.")uel.report_name_prefix="usage_gce"returnuelRuby
require"google/cloud/compute/v1"# Sets Compute Engine usage export bucket for the Cloud project.# This sample presents how to interpret the default value for the# report name prefix parameter.## @param [String] project project ID or project number of the project to update.# @param [String] bucket_name Google Cloud Storage bucket used to store Compute Engine# usage reports. An existing Google Cloud Storage bucket is required.# @param [String] report_name_prefix Prefix of the usage report name which defaults to an empty string# to showcase default values behaviour.defset_usage_export_bucketproject:,bucket_name:,report_name_prefix:""export_location={bucket_name:bucket_name,report_name_prefix:report_name_prefix}ifreport_name_prefix.empty?# Sending an empty value for report_name_prefix results in the# next usage report being generated with the default prefix value# "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket)puts"Setting report_name_prefix to empty value causes the report "\"to have the default prefix of `usage_gce`."endprojects_client=::Google::Cloud::Compute::V1::Projects::Rest::Client.newoperation=projects_client.set_usage_export_bucketproject:project,usage_export_location_resource:export_locationwait_until_doneoperation:operationendVerify that the server used the default prefix valueusage_gce:
require"google/cloud/compute/v1"# Retrieves Compute Engine usage export bucket for the Cloud project.# Replaces the empty value returned by the API with the default value used# to generate report file names.## @param [String] project project ID or project number of the project to get from.# @return [::Google::Cloud::Compute::V1::UsageExportLocation] object describing the current usage# export settings for project.defget_usage_export_bucketproject:projects_client=::Google::Cloud::Compute::V1::Projects::Rest::Client.newproject_data=projects_client.getproject:projectexport_location=project_data.usage_export_locationif!export_location.nil? &&export_location.report_name_prefix.empty?puts"Report name prefix not set, replacing with default value of `usage_gce`."export_location.report_name_prefix="usage_gce"endexport_locationendRust
usegoogle_cloud_compute_v1::client::Projects;usegoogle_cloud_compute_v1::model::UsageExportLocation;usegoogle_cloud_lro::Poller;pubasyncfnsample(client:&Projects,project_id:&str,bucket_name:&str)->anyhow::Result<()>{letoperation=client.set_usage_export_bucket().set_project(project_id).set_body(UsageExportLocation::new().set_bucket_name(bucket_name)).poller().until_done().await?;println!("Setting the usage export bucket with the default prefix (`usage_gce`) completed successfully: {operation:?}");letoperation=client.set_usage_export_bucket().set_project(project_id).set_body(UsageExportLocation::new().set_bucket_name(bucket_name).set_report_name_prefix("report-prefix"),).poller().until_done().await?.to_result()?;println!("Setting the usage export bucket completed successfully: {operation:?}");Ok(())}Verify that the server used the default prefix valueusage_gce:
usegoogle_cloud_compute_v1::client::Projects;pubasyncfnsample(client:&Projects,project_id:&str)->anyhow::Result<()>{letresponse=client.get().set_project(project_id).send().await?;letSome(bucket)=response.usage_export_location.as_ref().and_then(|l|l.bucket_name.as_ref())else{println!("usage reports are disabled for {project_id}");returnOk(());};letprefix=response.usage_export_location.as_ref().and_then(|l|l.report_name_prefix.as_deref()).unwrap_or("usage_gce");println!("usage reports are enabled and go to bucket {bucket} with prefix {prefix}");Ok(())}For more information on setting up usage exports, seeViewing usage reports.
Handling pagination
Some requests to the Compute Engine API return long lists of results, whichthe server provides to you as smaller "pages" of results with each call. Wheninteracting with the API directly, you make multiple API requests to getresponses for successive pages.
When you use Cloud Client Libraries, those requests simply return an iterator.You can then use the iterator to obtain all the individual results correspondingto your initial request. This process happens as though the results werereturned in one long list. The client library silently issues any necessary APIrequests for additional pages to ensure your iterator automatically traversesthe full set of results.
C#
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Threading.Tasks;publicclassListImagesAsyncSample{publicasyncTaskListImagesAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id"){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.ImagesClientclient=awaitImagesClient.CreateAsync();// Make the request to list all non-deprecated images in a project.ListImagesRequestrequest=newListImagesRequest{Project=projectId,// Listing only non-deprecated images to reduce the size of the reply.Filter="deprecated.state != DEPRECATED",// MaxResults indicates the maximum number of items that will be returned per page.MaxResults=100};// Although the MaxResults parameter is specified in the request, the sequence returned// by the ListAsync() method hides the pagination mechanic. The library makes multiple// requests to the API for you, so you can simply iterate over all the images.awaitforeach(varimageinclient.ListAsync(request)){// The result is an Image collection.Console.WriteLine($"Image: {image.Name}");}}}You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
usingGoogle.Cloud.Compute.V1;usingSystem;usingSystem.Threading.Tasks;publicclassListImagesPerPageAsyncSample{publicasyncTaskListImagesPerPageAsync(// TODO(developer): Set your own default values for these parameters or pass different values when calling this method.stringprojectId="your-project-id"){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.ImagesClientclient=awaitImagesClient.CreateAsync();// Make the request to list all non-deprecated images in a project.ListImagesRequestrequest=newListImagesRequest{Project=projectId,// Listing only non-deprecated images to reduce the size of the reply.Filter="deprecated.state != DEPRECATED",// MaxResults indicates the maximum number of items that will be returned per page.MaxResults=10};// Call the AsRawResponses() method of the returned image sequence to access the page sequece instead// This allows you to have more granular control of iteration over paginated results from the API.// Each time you access the next page, the library retrieves that page from the API.intpageIndex=0;awaitforeach(varpageinclient.ListAsync(request).AsRawResponses()){Console.WriteLine($"Page index: {pageIndex}");pageIndex++;foreach(varimageinpage){// The result is an Image collection.Console.WriteLine($"Image: {image.Name}");}}}}Go
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/api/iterator""google.golang.org/protobuf/proto")// printImagesList prints a list of all non-deprecated image names available in given project.funcprintImagesList(wio.Writer,projectIDstring)error{// projectID := "your_project_id"ctx:=context.Background()imagesClient,err:=compute.NewImagesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewImagesRESTClient: %w",err)}deferimagesClient.Close()// Listing only non-deprecated images to reduce the size of the reply.req:=&computepb.ListImagesRequest{Project:projectID,MaxResults:proto.Uint32(3),Filter:proto.String("deprecated.state != DEPRECATED"),}// Although the `MaxResults` parameter is specified in the request, the iterator returned// by the `list()` method hides the pagination mechanic. The library makes multiple// requests to the API for you, so you can simply iterate over all the images.it:=imagesClient.List(ctx,req)for{image,err:=it.Next()iferr==iterator.Done{break}iferr!=nil{returnerr}fmt.Fprintf(w,"- %s\n",image.GetName())}returnnil}You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
import("context""fmt""io"compute"cloud.google.com/go/compute/apiv1"computepb"cloud.google.com/go/compute/apiv1/computepb""google.golang.org/api/iterator""google.golang.org/protobuf/proto")// printImagesListByPage prints a list of all non-deprecated image names available in a given project,// divided into pages as returned by the Compute Engine API.funcprintImagesListByPage(wio.Writer,projectIDstring,pageSizeuint32)error{// projectID := "your_project_id"// pageSize := 10ctx:=context.Background()imagesClient,err:=compute.NewImagesRESTClient(ctx)iferr!=nil{returnfmt.Errorf("NewImagesRESTClient: %w",err)}deferimagesClient.Close()// Listing only non-deprecated images to reduce the size of the reply.req:=&computepb.ListImagesRequest{Project:projectID,MaxResults:proto.Uint32(pageSize),Filter:proto.String("deprecated.state != DEPRECATED"),}// Use the `iterator.NewPage` to have more granular control of iteration over// paginated results from the API. Each time you want to access the// next page, the library retrieves that page from the API.it:=imagesClient.List(ctx,req)p:=iterator.NewPager(it,int(pageSize),""/* start from the beginning */)forpage:=0;;page++{varitems[]*computepb.ImagepageToken,err:=p.NextPage(&items)iferr!=nil{returnfmt.Errorf("iterator paging failed: %w",err)}fmt.Fprintf(w,"Page %d: %v\n",page,items)ifpageToken==""{break}}returnnil}Java
importcom.google.cloud.compute.v1.Image;importcom.google.cloud.compute.v1.ImagesClient;importcom.google.cloud.compute.v1.ImagesClient.ListPage;importcom.google.cloud.compute.v1.ListImagesRequest;importjava.io.IOException;// Prints a list of all non-deprecated image names available in given project.publicstaticvoidlistImages(Stringproject)throwsIOException{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to// safely clean up any remaining background resources.try(ImagesClientimagesClient=ImagesClient.create()){// Listing only non-deprecated images to reduce the size of the reply.ListImagesRequestimagesRequest=ListImagesRequest.newBuilder().setProject(project).setMaxResults(100).setFilter("deprecated.state != DEPRECATED").build();// Although the `setMaxResults` parameter is specified in the request, the iterable returned// by the `list()` method hides the pagination mechanic. The library makes multiple// requests to the API for you, so you can simply iterate over all the images.intimageCount=0;for(Imageimage:imagesClient.list(imagesRequest).iterateAll()){imageCount++;System.out.println(image.getName());}System.out.printf("Image count in %s is: %s",project,imageCount);}}You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
importcom.google.cloud.compute.v1.Image;importcom.google.cloud.compute.v1.ImagesClient;importcom.google.cloud.compute.v1.ImagesClient.ListPage;importcom.google.cloud.compute.v1.ListImagesRequest;importjava.io.IOException;// Prints a list of all non-deprecated image names available in a given project,// divided into pages as returned by the Compute Engine API.publicstaticvoidlistImagesByPage(Stringproject,intpageSize)throwsIOException{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the `instancesClient.close()` method on the client to// safely clean up any remaining background resources.try(ImagesClientimagesClient=ImagesClient.create()){// Listing only non-deprecated images to reduce the size of the reply.ListImagesRequestimagesRequest=ListImagesRequest.newBuilder().setProject(project).setMaxResults(pageSize).setFilter("deprecated.state != DEPRECATED").build();// Use the `iteratePages` attribute of returned iterable to have more granular control of// iteration over paginated results from the API. Each time you want to access the// next page, the library retrieves that page from the API.intpageNumber=1;for(ListPagepage:imagesClient.list(imagesRequest).iteratePages()){System.out.println("Page Number: "+pageNumber++);for(Imageimage:page.getValues()){System.out.println(image.getName());}}}}Node.js
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';constcompute=require('@google-cloud/compute');asyncfunctionlistImages(){constimagesClient=newcompute.ImagesClient();// Listing only non-deprecated images to reduce the size of the reply.constimages=imagesClient.listAsync({project:projectId,maxResults:3,filter:'deprecated.state != DEPRECATED',});// Although the `maxResults` parameter is specified in the request, the iterable returned// by the `listAsync()` method hides the pagination mechanic. The library makes multiple// requests to the API for you, so you can simply iterate over all the images.forawait(constimageofimages){console.log(` -${image.name}`);}}listImages();You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
/** * TODO(developer): Uncomment and replace these variables before running the sample. */// const projectId = 'YOUR_PROJECT_ID';// const pageSize = 10;constcompute=require('@google-cloud/compute');asyncfunctionlistImagesByPage(){constimagesClient=newcompute.ImagesClient();// Listing only non-deprecated images to reduce the size of the reply.constlistRequest={project:projectId,maxResults:pageSize,filter:'deprecated.state != DEPRECATED',};constoptions={autoPaginate:false,};letpageNum=1;// Set autoPaginate option to `false` to have more granular control of// iteration over paginated results from the API. Each time you want to access the// next page, the library retrieves that page from the API.constlistCallback=(err,resources,nextPageRequest,response)=>{if(err){console.error(err);return;}console.log(`Page${pageNum}:`);pageNum+=1;for(leti=0;i <resources.length;i++){console.log(resources[i].name);}if(response.nextPageToken){imagesClient.list(nextPageRequest,options,listCallback);}};imagesClient.list(listRequest,options,listCallback);}listImagesByPage();PHP
use Google\Cloud\Compute\V1\Client\ImagesClient;use Google\Cloud\Compute\V1\ListImagesRequest;/** * Prints a list of all non-deprecated image names available in given project. * * @param string $projectId Project ID or project number of the Cloud project you want to list images from. * * @throws \Google\ApiCore\ApiException if the remote call fails. */function list_all_images(string $projectId){ $imagesClient = new ImagesClient(); // Listing only non-deprecated images to reduce the size of the reply. $optionalArgs = ['maxResults' => 100, 'filter' => 'deprecated.state != DEPRECATED']; /** * Although the maxResults parameter is specified in the request, the iterateAllElements() method * hides the pagination mechanic. The library makes multiple requests to the API for you, * so you can simply iterate over all the images. */ $request = (new ListImagesRequest()) ->setProject($projectId) ->setMaxResults($optionalArgs['maxResults']) ->setFilter($optionalArgs['filter']); $pagedResponse = $imagesClient->list($request); print('=================== Flat list of images ===================' . PHP_EOL); foreach ($pagedResponse->iterateAllElements() as $element) { printf(' - %s' . PHP_EOL, $element->getName()); }}You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
use Google\Cloud\Compute\V1\Client\ImagesClient;use Google\Cloud\Compute\V1\ListImagesRequest;/** * Prints a list of all non-deprecated image names available in a given project, * divided into pages as returned by the Compute Engine API. * * @param string $projectId Project ID or project number of the Cloud project you want to list images from. * @param int $pageSize Size of the pages you want the API to return on each call. * * @throws \Google\ApiCore\ApiException if the remote call fails. */function list_images_by_page(string $projectId, int $pageSize = 10){ $imagesClient = new ImagesClient(); $pageNum = 1; // Listing only non-deprecated images to reduce the size of the reply. $optionalArgs = ['maxResults' => $pageSize, 'filter' => 'deprecated.state != DEPRECATED']; /** * Use the 'iteratePages()' method of returned response to have more granular control of iteration over * paginated results from the API. Each time you want to access the next page, the library retrieves * that page from the API. */ $request = (new ListImagesRequest()) ->setProject($projectId) ->setMaxResults($optionalArgs['maxResults']) ->setFilter($optionalArgs['filter']); $pagedResponse = $imagesClient->list($request); print('=================== Paginated list of images ===================' . PHP_EOL); foreach ($pagedResponse->iteratePages() as $page) { printf('Page %s:' . PHP_EOL, $pageNum); foreach ($page as $element) { printf(' - %s' . PHP_EOL, $element->getName()); } $pageNum++; }}Python
importgoogle.cloud.compute_v1ascompute_v1defprint_images_list(project:str)->str:""" Prints a list of all non-deprecated image names available in given project. Args: project: project ID or project number of the Cloud project you want to list images from. Returns: The output as a string. """images_client=compute_v1.ImagesClient()# Listing only non-deprecated images to reduce the size of the reply.images_list_request=compute_v1.ListImagesRequest(project=project,max_results=100,filter="deprecated.state != DEPRECATED")output=[]# Although the `max_results` parameter is specified in the request, the iterable returned# by the `list()` method hides the pagination mechanic. The library makes multiple# requests to the API for you, so you can simply iterate over all the images.forimginimages_client.list(request=images_list_request):print(f" -{img.name}")output.append(f" -{img.name}")return"\n".join(output)You can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
importgoogle.cloud.compute_v1ascompute_v1defprint_images_list_by_page(project:str,page_size:int=10)->str:""" Prints a list of all non-deprecated image names available in a given project, divided into pages as returned by the Compute Engine API. Args: project: project ID or project number of the Cloud project you want to list images from. page_size: size of the pages you want the API to return on each call. Returns: Output as a string. """images_client=compute_v1.ImagesClient()# Listing only non-deprecated images to reduce the size of the reply.images_list_request=compute_v1.ListImagesRequest(project=project,max_results=page_size,filter="deprecated.state != DEPRECATED")output=[]# Use the `pages` attribute of returned iterable to have more granular control of# iteration over paginated results from the API. Each time you want to access the# next page, the library retrieves that page from the API.forpage_num,pageinenumerate(images_client.list(request=images_list_request).pages,start=1):print(f"Page{page_num}: ")output.append(f"Page{page_num}: ")forimginpage.items:print(f" -{img.name}")output.append(f" -{img.name}")return"\n".join(output)Ruby
require"google/cloud/compute/v1"# Prints a list of all non-deprecated image names available in given project.## @param [String] project project ID or project number of the Cloud project you want to list images from.defprint_images_listproject:client=::Google::Cloud::Compute::V1::Images::Rest::Client.new# Make the request to list all non-deprecated images in a project.request={project:project,# max_results indicates the maximum number of items that will be returned per page.max_results:100,# Listing only non-deprecated images to reduce the size of the reply.filter:"deprecated.state != DEPRECATED"}# Although the `max_results` parameter is specified in the request, the iterable returned# by the `list` method hides the pagination mechanic. The library makes multiple# requests to the API for you, so you can simply iterate over all the images.client.list(request).eachdo|image|puts" -#{image.name}"endendYou can also use thepages attribute of the returned object to have more granular control of iteration over paginated results from the API.
require"google/cloud/compute/v1"# Prints a list of all non-deprecated image names available in a given project,# divided into pages as returned by the Compute Engine API.## @param [String] project ID or project number of the Cloud project you want to list images from.# @param [Number] size of the pages you want the API to return on each call.defprint_images_list_by_pageproject:,page_size:10client=::Google::Cloud::Compute::V1::Images::Rest::Client.new# Make the request to list all non-deprecated images in a project.request={project:project,# max_results indicates the maximum number of items that will be returned per page.max_results:page_size,# Listing only non-deprecated images to reduce the size of the reply.filter:"deprecated.state != DEPRECATED"}# Call the each_page method of the returned enumerable to have more granular control# of iteration over paginated results from the API. Each time you access the next# page, the library retrieves that page from the API.page_index=0client.list(request).each_pagedo|page|puts"Page index:#{page_index}"page_index+=1page.eachdo|image|puts" -#{image.name}"endendendRust
pubusegoogle_cloud_compute_v1::client::Images;pubusegoogle_cloud_gax::paginator::ItemPaginator;pubasyncfnsample(client:&Images,project_id:&str)->anyhow::Result<()>{letmutitems=client.list().set_project(project_id)// Maximum number of results per page. Higher values reduce the number// of RPCs, but increase memory usage..set_max_results(100_u32)// Skip deprecated images..set_filter("deprecated.state != DEPRECATED").by_item();println!("iterating by item:");whileletSome(image)=items.next().await.transpose()?{println!("item = {:?}",image);}Ok(())}You can use theby_page() method to have more granular control of iteration over paginated results from the API.
pubusegoogle_cloud_compute_v1::client::Images;pubusegoogle_cloud_gax::paginator::Paginator;pubasyncfnsample(client:&Images,project_id:&str)->anyhow::Result<()>{letmutpages=client.list().set_project(project_id)// Limit the number of images in each page..set_max_results(10_u32)// Skip deprecated images..set_filter("deprecated.state != DEPRECATED").by_page();println!("iterating by page:");whileletSome(page)=pages.next().await.transpose()?{println!("items = {:?}",page.items);}Ok(())}Compute Engine in particular also offersaggregatedList methods likeinstances.aggregatedList. Pagination for thosemethods function similarly, but instead iterate over key-list pairs. For anexample, seeListing instances.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
When you're done experimenting, follow these cleanup instructions to avoidincurring charges for the virtual machine instance.
To delete a Compute Engine instance:
- In the Google Cloud console, go to theVM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, clickMore actions, clickDelete, and then follow the instructions.
What's next
- Review theAPI reference to learn how to performother tasks with the API.
Try it for yourself
If you're new to Google Cloud, create an account to evaluate how Compute Engine performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Try Compute Engine freeExcept as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.