Search in sources :

Example 6 with InstanceTemplatesClient

use of com.google.cloud.compute.v1.InstanceTemplatesClient in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceTemplate method createInstanceTemplate.

/*
    Create a new instance template with the provided name and a specific
    instance configuration.
   */
public static void createInstanceTemplate(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        String machineType = "e2-standard-4";
        String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
        // The template describes the size and source image of the boot disk
        // to attach to the instance.
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
        // The template connects the instance to the `default` network,
        // without specifying a subnetwork.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("global/networks/default").addAccessConfigs(AccessConfig.newBuilder().setName("External NAT").setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString()).setNetworkTier(NetworkTier.PREMIUM.toString()).build()).build();
        InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(attachedDisk).setMachineType(machineType).addNetworkInterfaces(networkInterface).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).build()).build();
        // Create the Instance Template.
        Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance Template creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient)

Example 7 with InstanceTemplatesClient

use of com.google.cloud.compute.v1.InstanceTemplatesClient 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());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) GlobalOperationsClient(com.google.cloud.compute.v1.GlobalOperationsClient) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 8 with InstanceTemplatesClient

use of com.google.cloud.compute.v1.InstanceTemplatesClient 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());
    }
}
Also used : GetInstanceTemplateRequest(com.google.cloud.compute.v1.GetInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 9 with InstanceTemplatesClient

use of com.google.cloud.compute.v1.InstanceTemplatesClient in project java-compute by googleapis.

the class ITSmokeInstancesTest method testResizeGroupToZero.

@Test
public void testResizeGroupToZero() throws IOException, ExecutionException, InterruptedException {
    // We test here: 1)set body field to zero
    // 2)set query param to zero
    List<String> instanceGroupManagersToClean = new ArrayList<>();
    List<String> instanceTemplatesToClean = new ArrayList<>();
    InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
    InstanceGroupManagersClient instanceGroupManagersClient = InstanceGroupManagersClient.create();
    String templateName = generateRandomName("template");
    String instanceGroupManagerName = generateRandomName("igm");
    Instance instance = insertInstance();
    InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setSourceInstance(instance.getSelfLink()).setName(templateName).build();
    Operation insertOperation = instanceTemplatesClient.insertAsync(DEFAULT_PROJECT, instanceTemplate).get();
    instanceTemplatesToClean.add(templateName);
    try {
        InstanceGroupManager instanceGroupManager = InstanceGroupManager.newBuilder().setName(instanceGroupManagerName).setBaseInstanceName("java-gapic").setInstanceTemplate(insertOperation.getTargetLink()).setTargetSize(0).build();
        instanceGroupManagersClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManager).get();
        instanceGroupManagersToClean.add(instanceGroupManagerName);
        InstanceGroupManager fetched = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
        Assert.assertEquals(0, fetched.getTargetSize());
        instanceGroupManagersClient.resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 1).get();
        InstanceGroupManager resizedIGM = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
        Assert.assertEquals(1, resizedIGM.getTargetSize());
        instanceGroupManagersClient.resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 0).get();
        InstanceGroupManager instanceGroupManagerResized = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
        Assert.assertEquals(0, instanceGroupManagerResized.getTargetSize());
    } finally {
        for (String name : instanceGroupManagersToClean) {
            instanceGroupManagersClient.deleteAsync(DEFAULT_PROJECT, DEFAULT_ZONE, name).get();
        }
        for (String name : instanceTemplatesToClean) {
            instanceTemplatesClient.deleteAsync(DEFAULT_PROJECT, name).get();
        }
    }
}
Also used : InstanceGroupManager(com.google.cloud.compute.v1.InstanceGroupManager) InstanceGroupManagersClient(com.google.cloud.compute.v1.InstanceGroupManagersClient) Instance(com.google.cloud.compute.v1.Instance) ArrayList(java.util.ArrayList) Operation(com.google.cloud.compute.v1.Operation) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate) Test(org.junit.Test)

Aggregations

InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)9 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)7 Operation (com.google.cloud.compute.v1.Operation)7 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)4 InsertInstanceTemplateRequest (com.google.cloud.compute.v1.InsertInstanceTemplateRequest)4 GlobalOperationsClient (com.google.cloud.compute.v1.GlobalOperationsClient)3 Instance (com.google.cloud.compute.v1.Instance)2 InstanceProperties (com.google.cloud.compute.v1.InstanceProperties)2 DeleteInstanceTemplateRequest (com.google.cloud.compute.v1.DeleteInstanceTemplateRequest)1 GetInstanceTemplateRequest (com.google.cloud.compute.v1.GetInstanceTemplateRequest)1 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)1 InstanceGroupManager (com.google.cloud.compute.v1.InstanceGroupManager)1 InstanceGroupManagersClient (com.google.cloud.compute.v1.InstanceGroupManagersClient)1 ListPagedResponse (com.google.cloud.compute.v1.InstanceTemplatesClient.ListPagedResponse)1 InstancesClient (com.google.cloud.compute.v1.InstancesClient)1 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)1 SourceInstanceParams (com.google.cloud.compute.v1.SourceInstanceParams)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1