use of com.google.cloud.compute.v1.Disk in project java-docs-samples by GoogleCloudPlatform.
the class CreateTemplateWithSubnet method createTemplateWithSubnet.
// Create an instance template that uses a provided subnet.
public static void createTemplateWithSubnet(String projectId, String network, String subnetwork, String templateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(String.format("projects/%s/global/images/family/%s", "debian-cloud", "debian-11")).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(disk).setMachineType("e2-standard-4").addNetworkInterfaces(NetworkInterface.newBuilder().setNetwork(network).setSubnetwork(subnetwork).build()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES);
Operation response = globalOperationsClient.wait(projectId, operation.getName());
if (response.hasError()) {
System.out.println("Template creation from subnet failed ! ! " + response);
return;
}
System.out.printf("Template creation from subnet operation status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.Disk 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());
}
}
use of com.google.cloud.compute.v1.Disk in project java-compute by googleapis.
the class ITSmokeInstancesTest method insertInstance.
private Instance insertInstance() throws ExecutionException, InterruptedException {
Instance instanceResource = Instance.newBuilder().setName(INSTANCE).setMachineType(MACHINE_TYPE).addDisks(DISK).addNetworkInterfaces(NETWORK_INTERFACE).setDescription("test").build();
instancesClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceResource).get();
instances.add(instanceResource);
return getInstance();
}
Aggregations