public Operation waitComplete(Operation operation, int duration, TimeUnit unit)
      throws TimeoutException, OpsException {
    long timeoutAt = System.currentTimeMillis() + unit.toMillis(duration);

    // TODO: Timeout?
    while (operation.getStatus().equals("RUNNING")) {
      if (timeoutAt < System.currentTimeMillis()) {
        throw new TimeoutException("Timeout while waiting for operation to complete");
      }
      log.debug("Polling for operation completion: " + operation);
      try {
        operation = compute.operations().get(projectId, operation.getName()).execute();
      } catch (IOException e) {
        throw new OpsException("Error waiting for operation to complete", e);
      }

      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new OpsException("Interrupted while waiting for operation to complete", e);
      }
    }

    return operation;
  }
  public void execute() throws IOException {
    log.info(
        String.format(
            "Creating: %s %s %s %s %s",
            GOOGLE_PROJECT_NAME, ZONE, INSTANCE_NAME, DISK_NAME, instanceType));

    natIP = "23.251.130.148";
    Instance content = createPayload_instance(INSTANCE_NAME, DISK_NAME, instanceType, natIP);
    Compute.Instances.Insert insert =
        compute.instances().insert(GOOGLE_PROJECT_NAME, ZONE, content);
    Operation operation = insert.execute();
    System.out.println("OK " + operation.getStatus());
  }
 protected Compute.RegionOperations.Get createRegionOperations(
     Compute compute, GcpCredential gcpCredential, Operation operation, CloudRegion region)
     throws IOException {
   return compute
       .regionOperations()
       .get(gcpCredential.getProjectId(), region.region(), operation.getName());
 }
示例#4
0
 public static String checkForErrors(Operation operation) {
   if (operation == null) {
     LOGGER.error("Operation is null!");
     return null;
   }
   String msg = null;
   if (operation.getError() != null) {
     StringBuilder error = new StringBuilder();
     if (operation.getError().getErrors() != null) {
       for (Operation.Error.Errors errors : operation.getError().getErrors()) {
         error.append(
             String.format(
                 "code: %s -> message: %s %s",
                 errors.getCode(), errors.getMessage(), System.lineSeparator()));
       }
       msg = error.toString();
     } else {
       LOGGER.debug("No errors found, Error: {}", operation.getError());
     }
   }
   if (operation.getHttpErrorStatusCode() != null) {
     msg +=
         String.format(
             " HTTP error message: %s, HTTP error status code: %s",
             operation.getHttpErrorMessage(), operation.getHttpErrorStatusCode());
   }
   return msg;
 }
示例#5
0
 public static boolean analyzeOperation(Operation operation) throws Exception {
   String errorMessage = checkForErrors(operation);
   if (errorMessage != null) {
     throw new Exception(errorMessage);
   } else {
     Integer progress = operation.getProgress();
     return progress == FINISHED;
   }
 }
 protected Compute.GlobalOperations.Get createGlobalOperations(
     Compute compute, GcpCredential gcpCredential, Operation operation) throws IOException {
   return compute.globalOperations().get(gcpCredential.getProjectId(), operation.getName());
 }