use of com.google.cloud.compute.v1.InstanceTemplate 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.InstanceTemplate 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.InstanceTemplate in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstanceFromTemplateWithOverrides method createInstanceFromTemplateWithOverrides.
// Creates a Compute Engine VM instance from an instance template,
// but overrides the disk and machine type options in the template.
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone, String instanceName, String instanceTemplateName) throws IOException, ExecutionException, InterruptedException {
try (InstancesClient instancesClient = InstancesClient.create();
InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
String machineType = "n1-standard-1";
String newDiskSourceImage = "projects/debian-cloud/global/images/family/debian-10";
// Retrieve an instance template.
InstanceTemplate instanceTemplate = instanceTemplatesClient.get(projectId, instanceTemplateName);
AttachedDisk newdisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage(newDiskSourceImage).build()).setAutoDelete(true).setBoot(false).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
Instance instance = Instance.newBuilder().setName(instanceName).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addAllDisks(instanceTemplate.getProperties().getDisksList()).addDisks(newdisk).build();
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instance).setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();
Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
if (response.hasError()) {
System.out.println("Instance creation from template with overrides failed ! ! " + response);
return;
}
System.out.printf("Instance creation from template with overrides: Operation Status %s: %s ", instanceName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.InstanceTemplate in project java-docs-samples by GoogleCloudPlatform.
the class ListInstanceTemplates method listInstanceTemplates.
// Get a list of InstanceTemplate objects available in a project.
public static ListPagedResponse listInstanceTemplates(String projectId) throws IOException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
int count = 0;
System.out.println("Listing instance templates...");
ListPagedResponse templates = instanceTemplatesClient.list(projectId);
for (InstanceTemplate instanceTemplate : templates.iterateAll()) {
System.out.printf("%s. %s%n", ++count, instanceTemplate.getName());
}
return templates;
}
}
use of com.google.cloud.compute.v1.InstanceTemplate in project java-docs-samples by GoogleCloudPlatform.
the class GetInstanceTemplate method getInstanceTemplate.
// Retrieve an instance template, which you can use to create virtual machine
// (VM) instances and managed instance groups (MIGs).
public static void getInstanceTemplate(String projectId, String templateName) throws IOException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
GetInstanceTemplateRequest getInstanceTemplateRequest = GetInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplate(templateName).build();
InstanceTemplate instanceTemplate = instanceTemplatesClient.get(getInstanceTemplateRequest);
System.out.println("Instance Template retrieved: " + instanceTemplate.getName());
}
}
Aggregations