Search in sources :

Example 1 with StopInstanceRequest

use of com.google.cloud.compute.v1.StopInstanceRequest in project java-docs-samples by GoogleCloudPlatform.

the class StopInstance method stopInstance.

// Stops a started Google Compute Engine instance.
public static void stopInstance(String project, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException {
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `instancesClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (InstancesClient instancesClient = InstancesClient.create()) {
        StopInstanceRequest stopInstanceRequest = StopInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
        OperationFuture<Operation, Operation> operation = instancesClient.stopAsync(stopInstanceRequest);
        Operation response = operation.get();
        if (response.getStatus() == Status.DONE) {
            System.out.println("Instance stopped successfully ! ");
        }
    }
}
Also used : InstancesClient(com.google.cloud.compute.v1.InstancesClient) Operation(com.google.cloud.compute.v1.Operation) StopInstanceRequest(com.google.cloud.compute.v1.StopInstanceRequest)

Example 2 with StopInstanceRequest

use of com.google.cloud.compute.v1.StopInstanceRequest in project java-notebooks by googleapis.

the class ITNotebookServiceClientTest method stopInstance.

private String stopInstance() throws ExecutionException, InterruptedException {
    StopInstanceRequest request = StopInstanceRequest.newBuilder().setName(INSTANCE_NAME).build();
    Instance response = client.stopInstanceAsync(request).get();
    return response.getState().name();
}
Also used : Instance(com.google.cloud.notebooks.v1beta1.Instance) StopInstanceRequest(com.google.cloud.notebooks.v1beta1.StopInstanceRequest)

Aggregations

InstancesClient (com.google.cloud.compute.v1.InstancesClient)1 Operation (com.google.cloud.compute.v1.Operation)1 StopInstanceRequest (com.google.cloud.compute.v1.StopInstanceRequest)1 Instance (com.google.cloud.notebooks.v1beta1.Instance)1 StopInstanceRequest (com.google.cloud.notebooks.v1beta1.StopInstanceRequest)1