use of com.google.cloud.compute.v1.SourceInstanceParams 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());
}
}
Aggregations