import time


def wait_for_operation(compute, project, zone, operation):
  """Waits for GCP operation to finish."""

  while True:
    result = compute.zoneOperations().get(
        project=project,
        zone=zone,
        operation=operation
    ).execute()

    if result["status"] == "DONE":
      if "error" in result:
        raise Exception(result["error"])
      return result

    time.sleep(2)