use of com.google.cloud.compute.v1.InstancesClient 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, TimeoutException {
/* 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(3, TimeUnit.MINUTES);
if (response.getStatus() == Status.DONE) {
System.out.println("Instance stopped successfully ! ");
}
}
}
use of com.google.cloud.compute.v1.InstancesClient in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstancesAdvanced method createWithDisks.
// [END compute_instances_create_from_image_plus_snapshot_disk]
// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_with_subnet]
// [START compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_empty_disk]
// [START compute_instances_create_from_custom_image]
// [START compute_instances_create_from_image]
/**
* Send an instance creation request to the Compute Engine API and wait for it to complete.
*
* @param project project ID or project number of the Cloud project you want to use.
* @param zone name of the zone to create the instance in. For example: "us-west3-b"
* @param instanceName name of the new virtual machine (VM) instance.
* @param disks a list of compute_v1.AttachedDisk objects describing the disks you want to attach
* to your new instance.
* @param machineType machine type of the VM being created. This value uses the following format:
* "zones/{zone}/machineTypes/{type_name}".
* For example: "zones/europe-west3-c/machineTypes/f1-micro"
* @param network name of the network you want the new instance to use. For example:
* "global/networks/default" represents the network named "default", which is created
* automatically for each project.
* @param subnetwork name of the subnetwork you want the new instance to use. This value uses the
* following format: "regions/{region}/subnetworks/{subnetwork_name}"
* @return Instance object.
*/
private static Instance createWithDisks(String project, String zone, String instanceName, Vector<AttachedDisk> disks, String machineType, String network, String subnetwork) throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (InstancesClient instancesClient = InstancesClient.create()) {
// Use the network interface provided in the networkName argument.
NetworkInterface networkInterface;
if (subnetwork != null) {
networkInterface = NetworkInterface.newBuilder().setName(network).setSubnetwork(subnetwork).build();
} else {
networkInterface = NetworkInterface.newBuilder().setName(network).build();
}
machineType = String.format("zones/%s/machineTypes/%s", zone, machineType);
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addAllDisks(disks).addNetworkInterfaces(networkInterface).build();
System.out.printf("Creating instance: %s at %s ", instanceName, zone);
// Insert the instance in the specified project and zone.
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(insertInstanceRequest);
// Wait for the operation to complete.
Operation response = operation.get(3, TimeUnit.MINUTES);
if (response.hasError()) {
System.out.println("Instance creation failed ! ! " + response);
return null;
}
System.out.println("Operation Status: " + response.getStatus());
return instancesClient.get(project, zone, instanceName);
}
}
use of com.google.cloud.compute.v1.InstancesClient in project java-docs-samples by GoogleCloudPlatform.
the class GetDeleteProtection method getDeleteProtection.
// Returns the state of delete protection flag of given instance.
public static boolean getDeleteProtection(String projectId, String zone, String instanceName) throws IOException {
try (InstancesClient instancesClient = InstancesClient.create()) {
Instance instance = instancesClient.get(projectId, zone, instanceName);
boolean deleteProtection = instance.getDeletionProtection();
System.out.printf("Retrieved Delete Protection setting for instance: %s : %s", instanceName, deleteProtection);
return deleteProtection;
}
}
use of com.google.cloud.compute.v1.InstancesClient in project java-docs-samples by GoogleCloudPlatform.
the class CreatePreemptibleInstance method createPremptibleInstance.
// Send an instance creation request with preemptible settings to the Compute Engine API
// and wait for it to complete.
public static void createPremptibleInstance(String projectId, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
String machineType = String.format("zones/%s/machineTypes/e2-small", zone);
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
long diskSizeGb = 10L;
String networkName = "default";
try (InstancesClient instancesClient = InstancesClient.create()) {
AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(AttachedDisk.Type.PERSISTENT.toString()).setInitializeParams(// Describe the size and source image of the boot disk to attach to the instance.
AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();
// Use the default VPC network.
NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
// Collect information into the Instance object.
Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).setScheduling(Scheduling.newBuilder().setPreemptible(true).build()).build();
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
// Prepare the request to insert an instance.
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instanceResource).build();
// Wait for the create operation to complete.
Operation response = instancesClient.insertAsync(insertInstanceRequest).get(3, TimeUnit.MINUTES);
;
if (response.hasError()) {
System.out.println("Instance creation failed ! ! " + response);
return;
}
System.out.printf("Instance created : %s\n", instanceName);
System.out.println("Operation Status: " + response.getStatus());
}
}
Aggregations