@@ -44,66 +44,73 @@ public static void main(String[] args)
4444 String zone = "us-central1-a" ;
4545 // Name of the VM instance you want to query.
4646 String instanceName = "YOUR_INSTANCE_NAME" ;
47- // Machine type of the instances in the reservation.
48- String machineType = "n2-standard-32" ;
4947
50- createInstanceNotConsumeReservation (projectId , zone , instanceName , machineType );
48+ createInstanceNotConsumeReservation (projectId , zone , instanceName );
5149 }
5250
5351 // Create a virtual machine that explicitly doesn't consume reservations
5452 public static void createInstanceNotConsumeReservation (
55- String projectId , String zone , String instanceName , String machineType )
53+ String project , String zone , String instanceName )
5654 throws IOException , InterruptedException , ExecutionException , TimeoutException {
5755 // Below are sample values that can be replaced.
56+ // machineType: machine type of the VM being created.
57+ // * This value uses the format zones/{zone}/machineTypes/{type_name}.
58+ // * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
5859 // sourceImage: path to the operating system image to mount.
5960 // * For details about images you can mount, see https://cloud.google.com/compute/docs/images
60- // Network interface to associate with the instance.
61+ // diskSizeGb: storage size of the boot disk to attach to the instance.
62+ // networkName: network interface to associate with the instance.
63+ String machineType = String .format ("zones/%s/machineTypes/n1-standard-1" , zone );
6164 String sourceImage = String
6265 .format ("projects/debian-cloud/global/images/family/%s" , "debian-11" );
63- String network = "global/networks/default" ; // Example network
6466 long diskSizeGb = 10L ;
67+ String networkName = "default" ;
68+
6569 // Initialize client that will be used to send requests. This client only needs to be created
6670 // once, and can be reused for multiple requests.
6771 try (InstancesClient instancesClient = InstancesClient .create ()) {
68-
69- // Create the attached disk object
70- AttachedDisk attachedDisk =
72+ // Instance creation requires at least one persistent disk and one network interface.
73+ AttachedDisk disk =
7174 AttachedDisk .newBuilder ()
7275 .setBoot (true )
7376 .setAutoDelete (true )
7477 .setType (AttachedDisk .Type .PERSISTENT .toString ())
78+ .setDeviceName ("disk-1" )
7579 .setInitializeParams (
7680 AttachedDiskInitializeParams .newBuilder ()
7781 .setSourceImage (sourceImage )
7882 .setDiskSizeGb (diskSizeGb )
7983 .build ())
8084 .build ();
8185
82- // Create the network interface object
83- NetworkInterface networkInterface =
84- NetworkInterface .newBuilder ().setName (network ).build ();
86+ // Use the network interface provided in the networkName argument.
87+ NetworkInterface networkInterface = NetworkInterface .newBuilder ()
88+ .setName (networkName )
89+ .build ();
8590
8691 // Set reservation affinity to "none"
8792 ReservationAffinity reservationAffinity =
8893 ReservationAffinity .newBuilder ()
8994 .setConsumeReservationType (NO_RESERVATION .toString ())
9095 .build ();
9196
92- // Create the instance object
93- Instance instance =
97+ // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
98+ Instance instanceResource =
9499 Instance .newBuilder ()
95100 .setName (instanceName )
96- .setMachineType ("zones/" + zone + "/machineTypes/" + machineType )
97- .addDisks (attachedDisk )
101+ .setMachineType (machineType )
102+ .addDisks (disk )
98103 .addNetworkInterfaces (networkInterface )
99104 .setReservationAffinity (reservationAffinity )
100105 .build ();
101106
107+ System .out .printf ("Creating instance: %s at %s %n" , instanceName , zone );
108+
102109 // Insert the instance in the specified project and zone.
103110 InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest .newBuilder ()
104- .setProject (projectId )
111+ .setProject (project )
105112 .setZone (zone )
106- .setInstanceResource (instance )
113+ .setInstanceResource (instanceResource )
107114 .build ();
108115
109116 OperationFuture <Operation , Operation > operation = instancesClient .insertAsync (
@@ -114,6 +121,7 @@ public static void createInstanceNotConsumeReservation(
114121
115122 if (response .hasError ()) {
116123 System .out .println ("Instance creation failed ! ! " + response );
124+ return ;
117125 }
118126 System .out .println ("Operation Status: " + response .getStatus ());
119127 }
0 commit comments