use of com.google.cloud.compute.v1.GlobalOperationsClient in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstanceTemplate method createInstanceTemplateWithDiskType.
public static void createInstanceTemplateWithDiskType(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage("projects/debian-cloud/global/images/family/debian-10").build()).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(InstanceProperties.newBuilder().setMachineType("n1-standard-1").addDisks(disk).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).build()).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
return;
}
System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.GlobalOperationsClient in project java-docs-samples by GoogleCloudPlatform.
the class CreateTemplateFromInstance method createTemplateFromInstance.
// Create a new instance template based on an existing instance.
// This new template specifies a different boot disk.
public static void createTemplateFromInstance(String projectId, String templateName, String instance) throws IOException, ExecutionException, InterruptedException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder().addDiskConfigs(DiskInstantiationConfig.newBuilder().setDeviceName("disk-1").setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE.toString()).setCustomImage(String.format("projects/%s/global/images/family/%s", "rocky-linux-cloud", "rocky-linux-8")).setAutoDelete(true).build()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setSourceInstance(instance).setSourceInstanceParams(sourceInstanceParams).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get();
Operation response = globalOperationsClient.wait(projectId, operation.getName());
if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
return;
}
System.out.printf("Instance Template creation operation status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.GlobalOperationsClient 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 {
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();
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());
}
}
Aggregations