@@ -104,26 +104,33 @@ Add the following imports at the top of your file:
104104``` java
105105import com.google.cloud.compute.AddressInfo ;
106106import com.google.cloud.compute.Operation ;
107+ import com.google.cloud.compute.Operation.OperationError ;
108+ import com.google.cloud.compute.Operation.OperationWarning ;
107109import com.google.cloud.compute.RegionAddressId ;
110+
111+ import java.util.List ;
108112```
109113
110114Then add the following code to create an address. Most Compute Engine calls return an ` Operation `
111115object that can be used to wait for operation completion and to check whether operation failed or
112116succeeded:
113117
114118``` java
115- RegionAddressId addressId = RegionAddressId . of(" us-central1" , " test-address" );
119+ final RegionAddressId addressId = RegionAddressId . of(" us-central1" , " test-address" );
116120Operation operation = compute. create(AddressInfo . of(addressId));
117- while (! operation. isDone()) {
118- Thread . sleep(1000L );
119- }
120- operation = operation. reload();
121- if (operation. errors() == null ) {
122- System . out. println(" Address " + addressId + " was successfully created" );
123- } else {
124- // inspect operation.errors()
125- throw new RuntimeException (" Address creation failed" );
126- }
121+ // Wait for operation to complete
122+ operation. whenDone(new Operation .CompletionCallback () {
123+ @Override
124+ public void success (Operation operation ) {
125+ System . out. println(" Address " + addressId + " was successfully created" );
126+ }
127+
128+ @Override
129+ public void error (List<OperationError > errors , List<OperationWarning > warnings ) {
130+ // inspect errors
131+ throw new RuntimeException (" Address creation failed" );
132+ }
133+ });
127134```
128135
129136#### Creating a persistent disk
@@ -145,21 +152,23 @@ import com.google.cloud.compute.ImageId;
145152Then add the following code to create a disk and wait for disk creation to terminate.
146153
147154``` java
148- ImageId imageId = ImageId . of(" debian-cloud" , " debian-8-jessie-v20160329" );
155+ final ImageId imageId = ImageId . of(" debian-cloud" , " debian-8-jessie-v20160329" );
149156DiskId diskId = DiskId . of(" us-central1-a" , " test-disk" );
150157ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration . of(imageId);
151158DiskInfo disk = DiskInfo . of(diskId, diskConfiguration);
152159Operation operation = compute. create(disk);
153- while (! operation. isDone()) {
154- Thread . sleep(1000L );
155- }
156- operation = operation. reload();
157- if (operation. errors() == null ) {
158- System . out. println(" Disk " + diskId + " was successfully created" );
159- } else {
160- // inspect operation.errors()
161- throw new RuntimeException (" Disk creation failed" );
162- }
160+ operation. whenDone(new Operation .CompletionCallback () {
161+ @Override
162+ public void success (Operation operation ) {
163+ System . out. println(" Disk " + diskId + " was successfully created" );
164+ }
165+
166+ @Override
167+ public void error (List<OperationError > errors , List<OperationWarning > warnings ) {
168+ // inspect errors
169+ throw new RuntimeException (" Disk creation failed" );
170+ }
171+ });
163172```
164173
165174#### Creating a virtual machine instance
@@ -186,7 +195,7 @@ Then add the following code to create an instance and wait for instance creation
186195
187196``` java
188197Address externalIp = compute. getAddress(addressId);
189- InstanceId instanceId = InstanceId . of(" us-central1-a" , " test-instance" );
198+ final InstanceId instanceId = InstanceId . of(" us-central1-a" , " test-instance" );
190199NetworkId networkId = NetworkId . of(" default" );
191200PersistentDiskConfiguration attachConfiguration =
192201 PersistentDiskConfiguration . builder(diskId). boot(true ). build();
@@ -198,16 +207,18 @@ MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1")
198207InstanceInfo instance =
199208 InstanceInfo . of(instanceId, machineTypeId, attachedDisk, networkInterface);
200209Operation operation = compute. create(instance);
201- while (! operation. isDone()) {
202- Thread . sleep(1000L );
203- }
204- operation = operation. reload();
205- if (operation. errors() == null ) {
206- System . out. println(" Instance " + instanceId + " was successfully created" );
207- } else {
208- // inspect operation.errors()
209- throw new RuntimeException (" Instance creation failed" );
210- }
210+ operation. whenDone(new Operation .CompletionCallback () {
211+ @Override
212+ public void success (Operation operation ) {
213+ System . out. println(" Instance " + instanceId + " was successfully created" );
214+ }
215+
216+ @Override
217+ public void error (List<OperationError > errors , List<OperationWarning > warnings ) {
218+ // inspect errors
219+ throw new RuntimeException (" Instance creation failed" );
220+ }
221+ });
211222```
212223
213224#### Complete source code
0 commit comments