examples: Add example for checking operation progress
Fixes #238
Change-Id: If6eccb8861c9b044742b81bf2ea52f98910982cb
Reviewed-on: https://code-review.googlesource.com/c/35990
Reviewed-by: Jean de Klerk <deklerk@google.com>
diff --git a/examples/operation_progress.go b/examples/operation_progress.go
new file mode 100644
index 0000000..beb1fbe
--- /dev/null
+++ b/examples/operation_progress.go
@@ -0,0 +1,45 @@
+// Copyright 2018 Google Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "context"
+ "log"
+
+ "golang.org/x/oauth2/google"
+ compute "google.golang.org/api/compute/v1"
+)
+
+const (
+ projectID = "some-project-id"
+ zone = "some-zone"
+ operationID = "some-operation-id"
+)
+
+func operationProgressMain() {
+ ctx := context.Background()
+
+ client, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
+ if err != nil {
+ log.Fatal(err)
+ }
+ svc, err := compute.New(client)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ for {
+ resp, err := svc.ZoneOperations.Get(projectID, zone, operationID).Do()
+ if err != nil {
+ log.Fatal(err)
+ }
+ // Note: the response Status may differ between APIs. The string values
+ // checked here may need to be changed depending on the API.
+ if resp.Status != "WORKING" && resp.Status != "QUEUED" {
+ break
+ }
+ }
+ log.Println("operation complete")
+}