Search in sources :

Example 41 with Operation

use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstance method createInstance.

// Create a new instance with the provided "instanceName" value in the specified project and zone.
public static void createInstance(String project, String zone, String instanceName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // Below are sample values that can be replaced.
    // machineType: machine type of the VM being created.
    // *   This value uses the format zones/{zone}/machineTypes/{type_name}.
    // *   For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
    // sourceImage: path to the operating system image to mount.
    // *   For details about images you can mount, see https://cloud.google.com/compute/docs/images
    // diskSizeGb: storage size of the boot disk to attach to the instance.
    // networkName: network interface to associate with the instance.
    String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
    String sourceImage = String.format("projects/debian-cloud/global/images/family/%s", "debian-11");
    long diskSizeGb = 10L;
    String networkName = "default";
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Instance creation requires at least one persistent disk and one network interface.
        AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(Type.PERSISTENT.toString()).setDeviceName("disk-1").setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).build();
        System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
        // Insert the instance in the specified project and zone.
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();
        OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(insertInstanceRequest);
        // Wait for the operation to complete.
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance creation failed ! ! " + response);
            return;
        }
        System.out.println("Operation Status: " + response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 42 with Operation

use of com.google.container.v1.Operation 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, TimeoutException {
    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(3, TimeUnit.MINUTES);
        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());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) Operation(com.google.cloud.compute.v1.Operation) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 43 with Operation

use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceTemplate method createInstanceTemplateWithDiskType.

public static void createInstanceTemplateWithDiskType(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    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(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 : 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 44 with Operation

use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.

the class CreateFirewallRuleForWindowsActivationHost method createFirewallRuleForWindowsActivationHost.

// Creates a new allow egress firewall rule with the highest priority for host
// kms.windows.googlecloud.com (35.190.247.13) for Windows activation.
public static void createFirewallRuleForWindowsActivationHost(String projectId, String firewallRuleName, String networkName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Instantiates a client.
    try (FirewallsClient firewallsClient = FirewallsClient.create()) {
        Firewall firewall = Firewall.newBuilder().setName(firewallRuleName).addAllowed(Allowed.newBuilder().setIPProtocol("tcp").addPorts("1688").build()).setDirection("EGRESS").setNetwork(networkName).addDestinationRanges("35.190.247.13/32").setPriority(0).build();
        InsertFirewallRequest request = InsertFirewallRequest.newBuilder().setProject(projectId).setFirewallResource(firewall).build();
        // Wait for the operation to complete.
        Operation operation = firewallsClient.insertAsync(request).get(3, TimeUnit.MINUTES);
        if (operation.hasError()) {
            System.out.println("Firewall rule creation failed ! ! " + operation.getError());
            return;
        }
        System.out.printf("Firewall rule created %s", firewallRuleName);
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) Operation(com.google.cloud.compute.v1.Operation) InsertFirewallRequest(com.google.cloud.compute.v1.InsertFirewallRequest) Firewall(com.google.cloud.compute.v1.Firewall)

Example 45 with Operation

use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.

the class CreateRouteToWindowsActivationHost method createRouteToWindowsActivationHost.

// Creates a new route to kms.windows.googlecloud.com (35.190.247.13) for Windows activation.
public static void createRouteToWindowsActivationHost(String projectId, String routeName, String networkName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Instantiates a client.
    try (RoutesClient routesClient = RoutesClient.create()) {
        // If you have Windows instances without external IP addresses,
        // you must also enable Private Google Access so that instances
        // with only internal IP addresses can send traffic to the external
        // IP address for kms.windows.googlecloud.com.
        // More information: https://cloud.google.com/vpc/docs/configure-private-google-access#enabling
        Route route = Route.newBuilder().setName(routeName).setDestRange("35.190.247.13/32").setNetwork(networkName).setNextHopGateway(String.format("projects/%s/global/gateways/default-internet-gateway", projectId)).build();
        InsertRouteRequest request = InsertRouteRequest.newBuilder().setProject(projectId).setRouteResource(route).build();
        // Wait for the operation to complete.
        Operation operation = routesClient.insertAsync(request).get(3, TimeUnit.MINUTES);
        if (operation.hasError()) {
            System.out.printf("Error in creating route %s", operation.getError());
            return;
        }
        System.out.printf("Route created %s", routeName);
    }
}
Also used : RoutesClient(com.google.cloud.compute.v1.RoutesClient) Operation(com.google.cloud.compute.v1.Operation) InsertRouteRequest(com.google.cloud.compute.v1.InsertRouteRequest) Route(com.google.cloud.compute.v1.Route)

Aggregations

ArrayList (java.util.ArrayList)81 Test (org.junit.Test)75 AbstractMessage (com.google.protobuf.AbstractMessage)65 Operation (com.google.cloud.compute.v1.Operation)49 Operation (com.google.container.v1.Operation)43 StatusCondition (com.google.container.v1.StatusCondition)40 Operation (com.google.container.v1beta1.Operation)24 StatusCondition (com.google.container.v1beta1.StatusCondition)23 InstancesClient (com.google.cloud.compute.v1.InstancesClient)20 Operation (com.reprezen.kaizen.oasparser.model3.Operation)16 Path (com.reprezen.kaizen.oasparser.model3.Path)15 Operation (net.opengis.ows.v_1_0_0.Operation)15 Instance (com.google.cloud.compute.v1.Instance)13 Parameter (com.reprezen.kaizen.oasparser.model3.Parameter)13 DomainType (net.opengis.ows.v_1_0_0.DomainType)13 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)12 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)11 Operation (org.osate.aadl2.Operation)8 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)7 BooleanLiteral (org.osate.aadl2.BooleanLiteral)7