Check operation status

Request to wait until an operation completes and get the operation status before continuing code execution.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

Before trying this sample, follow the C# setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine C# API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
  Google.Cloud.Compute.V1 
 
 ; 
 using 
  
 System.Threading.Tasks 
 ; 
 public 
  
 class 
  
 CreateInstanceAsyncSample 
 { 
  
 public 
  
 async 
  
 Task 
  
 CreateInstanceAsync 
 ( 
  
 // TODO(developer): Set your own default values for these parameters or pass different values when calling this method. 
  
 string 
  
 projectId 
  
 = 
  
 "your-project-id" 
 , 
  
 string 
  
 zone 
  
 = 
  
 "us-central1-a" 
 , 
  
 string 
  
 machineName 
  
 = 
  
 "test-machine" 
 , 
  
 string 
  
 machineType 
  
 = 
  
 "n1-standard-1" 
 , 
  
 string 
  
 diskImage 
  
 = 
  
 "projects/debian-cloud/global/images/family/debian-12" 
 , 
  
 long 
  
 diskSizeGb 
  
 = 
  
 10 
 , 
  
 string 
  
 networkName 
  
 = 
  
 "default" 
 ) 
  
 { 
  
  Instance 
 
  
 instance 
  
 = 
  
 new 
  
  Instance 
 
  
 { 
  
 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 
  
 = 
  
 { 
  
 new 
  
  AttachedDisk 
 
  
 { 
  
 AutoDelete 
  
 = 
  
 true 
 , 
  
 Boot 
  
 = 
  
 true 
 , 
  
 Type 
  
 = 
  
  ComputeEnumConstants 
 
 . 
  AttachedDisk 
 
 . 
  Type 
 
 . 
  Persistent 
 
 , 
  
 InitializeParams 
  
 = 
  
 new 
  
  AttachedDiskInitializeParams 
 
  
  
 { 
  
 // See https://cloud.google.com/compute/docs/images for more information on available images. 
  
 SourceImage 
  
 = 
  
 diskImage 
 , 
  
 DiskSizeGb 
  
 = 
  
 diskSizeGb 
  
 } 
  
 } 
  
 }, 
  
 NetworkInterfaces 
  
 = 
  
 { 
  
 new 
  
  NetworkInterface 
 
  
 { 
  
 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. 
  
  InstancesClient 
 
  
 client 
  
 = 
  
 await 
  
  InstancesClient 
 
 . 
  CreateAsync 
 
 (); 
  
 // Insert the instance in the specified project and zone. 
  
 var 
  
 instanceCreation 
  
 = 
  
 await 
  
 client 
 . 
  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. 
  
 await 
  
 instanceCreation 
 . 
 PollUntilCompletedAsync 
 (); 
  
 } 
 } 
 

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Go API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  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. 
 func 
  
 waitForOperation 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
  
 string 
 , 
  
 operation 
  
 * 
 compute 
 . 
 Operation 
 ) 
  
 error 
  
 { 
  
 // projectID := "your_project_id" 
  
 // zone := "europe-central2-b" 
  
 // opName := "your_operation_name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 if 
  
 err 
  
 := 
  
 operation 
 . 
 Wait 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to wait for the operation: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Operation finished\n" 
 ) 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Java API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.compute.v1. Operation 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. Operation 
. Status 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. ZoneOperationsClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 WaitForOperation 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // operation: Specify the operation to wait. 
  
 String 
  
 project 
  
 = 
  
 "your-project-id" 
 ; 
  
  Operation 
 
  
 operation 
  
 = 
  
  Operation 
 
 . 
 newBuilder 
 (). 
 build 
 (); 
  
 waitForOperation 
 ( 
 project 
 , 
  
 operation 
 ); 
  
 } 
  
 // Waits for the specified operation to complete. 
  
 public 
  
 static 
  
 void 
  
 waitForOperation 
 ( 
 String 
  
 project 
 , 
  
  Operation 
 
  
 operation 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 try 
  
 ( 
  ZoneOperationsClient 
 
  
 zoneOperationsClient 
  
 = 
  
  ZoneOperationsClient 
 
 . 
 create 
 ()) 
  
 { 
  
 // Check if the operation hasn't been completed already. 
  
 if 
  
 ( 
 operation 
 . 
  getStatus 
 
 () 
  
 != 
  
  Status 
 
 . 
 DONE 
 ) 
  
 { 
  
 String 
  
 zone 
  
 = 
  
 operation 
 . 
  getZone 
 
 (); 
  
 zone 
  
 = 
  
 zone 
 . 
 substring 
 ( 
 zone 
 . 
 lastIndexOf 
 ( 
 "/" 
 ) 
  
 + 
  
 1 
 ); 
  
 // Wait for the operation to complete. 
  
  Operation 
 
  
 response 
  
 = 
  
 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

Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Node.js API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  /** 
 * TODO(developer): Uncomment and replace these variables before running the sample. 
 */ 
 // const projectId = 'YOUR_PROJECT_ID'; 
 // const operationString = 'YOUR_OPERATION_STRING' 
 const 
  
 compute 
  
 = 
  
 require 
 ( 
 ' @google-cloud/compute 
' 
 ); 
 // Parse stringified operation to the object instance. 
 let 
  
 operation 
  
 = 
  
 JSON 
 . 
 parse 
 ( 
 operationString 
 ); 
 async 
  
 function 
  
 waitForOperation 
 () 
  
 { 
  
 const 
  
 operationsClient 
  
 = 
  
 new 
  
 compute 
 . 
  ZoneOperationsClient 
 
 (); 
  
 while 
  
 ( 
 operation 
 . 
 status 
  
 !== 
  
 'DONE' 
 ) 
  
 { 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 operationsClient 
 . 
 wait 
 ({ 
  
 operation 
 : 
  
 operation 
 . 
 name 
 , 
  
 project 
 : 
  
 projectId 
 , 
  
 zone 
 : 
  
 operation 
 . 
 zone 
 . 
 split 
 ( 
 '/' 
 ). 
 pop 
 (), 
  
 }); 
  
 } 
  
 console 
 . 
 log 
 ( 
 'Operation finished.' 
 ); 
 } 
 waitForOperation 
 (); 
 

PHP

Before trying this sample, follow the PHP setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine PHP API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // 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

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Python API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
  compute_v1 
 
 def 
  
 wait_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 
 } 
 if 
 operation 
 . 
 zone 
 : 
 client 
 = 
  compute_v1 
 
 . 
  ZoneOperationsClient 
 
 () 
 # Operation.zone is a full URL address of a zone, so we need to extract just the name 
 kwargs 
 [ 
 "zone" 
 ] 
 = 
 operation 
 . 
 zone 
 . 
 rsplit 
 ( 
 "/" 
 , 
 maxsplit 
 = 
 1 
 )[ 
 1 
 ] 
 elif 
 operation 
 . 
 region 
 : 
 client 
 = 
  compute_v1 
 
 . 
  RegionOperationsClient 
 
 () 
 # Operation.region is a full URL address of a region, so we need to extract just the name 
 kwargs 
 [ 
 "region" 
 ] 
 = 
 operation 
 . 
 region 
 . 
 rsplit 
 ( 
 "/" 
 , 
 maxsplit 
 = 
 1 
 )[ 
 1 
 ] 
 else 
 : 
 client 
 = 
  compute_v1 
 
 . 
  GlobalOperationsClient 
 
 () 
 return 
 client 
 . 
  wait 
 
 ( 
 ** 
 kwargs 
 ) 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Ruby API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  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. 
 def 
  
 wait_until_done 
  
 operation 
 :, 
  
 timeout 
 : 
  
 3 
  
 * 
  
 60 
  
 retry_policy 
  
 = 
  
 :: 
 Gapic 
 :: 
 Operation 
 :: 
 RetryPolicy 
 . 
 new 
  
 initial_delay 
 : 
  
 0 
 . 
 2 
 , 
  
 multiplier 
 : 
  
 2 
 , 
  
 max_delay 
 : 
  
 1 
 , 
  
 timeout 
 : 
  
 timeout 
  
 operation 
 . 
 wait_until_done! 
  
 retry_policy 
 : 
  
 retry_policy 
 end 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: