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()); }