source: gs3-extensions/vm-manager/trunk/src/example-java-code/CreateInstance.java@ 38640

Last change on this file since 38640 was 38640, checked in by davidb, 4 months ago

Initial set of CLI scripts, originally developed within selfcontained-gcloud extension

File size: 4.5 KB
Line 
1/*
2 Example from:
3 https://cloud.google.com/compute/docs/samples/compute-instances-create#compute_instances_create-java
4*/
5
6import com.google.api.gax.longrunning.OperationFuture;
7import com.google.cloud.compute.v1.AttachedDisk;
8import com.google.cloud.compute.v1.AttachedDisk.Type;
9import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
10import com.google.cloud.compute.v1.InsertInstanceRequest;
11import com.google.cloud.compute.v1.Instance;
12import com.google.cloud.compute.v1.InstancesClient;
13import com.google.cloud.compute.v1.NetworkInterface;
14import com.google.cloud.compute.v1.Operation;
15import java.io.IOException;
16import java.util.concurrent.ExecutionException;
17import java.util.concurrent.TimeUnit;
18import java.util.concurrent.TimeoutException;
19
20public class CreateInstance {
21
22 public static void main(String[] args)
23 throws IOException, InterruptedException, ExecutionException, TimeoutException {
24 // TODO(developer): Replace these variables before running the sample.
25 String project = "your-project-id";
26 String zone = "zone-name";
27 String instanceName = "instance-name";
28 createInstance(project, zone, instanceName);
29 }
30
31
32 // Create a new instance with the provided "instanceName" value in the specified project and zone.
33 public static void createInstance(String project, String zone, String instanceName)
34 throws IOException, InterruptedException, ExecutionException, TimeoutException {
35 // Below are sample values that can be replaced.
36 // machineType: machine type of the VM being created.
37 // * This value uses the format zones/{zone}/machineTypes/{type_name}.
38 // * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
39 // sourceImage: path to the operating system image to mount.
40 // * For details about images you can mount, see https://cloud.google.com/compute/docs/images
41 // diskSizeGb: storage size of the boot disk to attach to the instance.
42 // networkName: network interface to associate with the instance.
43 String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
44 String sourceImage = String
45 .format("projects/debian-cloud/global/images/family/%s", "debian-11");
46 long diskSizeGb = 10L;
47 String networkName = "default";
48
49 // Initialize client that will be used to send requests. This client only needs to be created
50 // once, and can be reused for multiple requests. After completing all of your requests, call
51 // the `instancesClient.close()` method on the client to safely
52 // clean up any remaining background resources.
53 try (InstancesClient instancesClient = InstancesClient.create()) {
54 // Instance creation requires at least one persistent disk and one network interface.
55 AttachedDisk disk =
56 AttachedDisk.newBuilder()
57 .setBoot(true)
58 .setAutoDelete(true)
59 .setType(Type.PERSISTENT.toString())
60 .setDeviceName("disk-1")
61 .setInitializeParams(
62 AttachedDiskInitializeParams.newBuilder()
63 .setSourceImage(sourceImage)
64 .setDiskSizeGb(diskSizeGb)
65 .build())
66 .build();
67
68 // Use the network interface provided in the networkName argument.
69 NetworkInterface networkInterface = NetworkInterface.newBuilder()
70 .setName(networkName)
71 .build();
72
73 // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
74 Instance instanceResource =
75 Instance.newBuilder()
76 .setName(instanceName)
77 .setMachineType(machineType)
78 .addDisks(disk)
79 .addNetworkInterfaces(networkInterface)
80 .build();
81
82 System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
83
84 // Insert the instance in the specified project and zone.
85 InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
86 .setProject(project)
87 .setZone(zone)
88 .setInstanceResource(instanceResource)
89 .build();
90
91 OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
92 insertInstanceRequest);
93
94 // Wait for the operation to complete.
95 Operation response = operation.get(3, TimeUnit.MINUTES);
96
97 if (response.hasError()) {
98 System.out.println("Instance creation failed ! ! " + response);
99 return;
100 }
101 System.out.println("Operation Status: " + response.getStatus());
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.