xref: /aosp_15_r20/external/google-cloud-java/java-compute/.readme-partials.yaml (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1*55e87721SMatt Gilbridecustom_content: |
2*55e87721SMatt Gilbride  ### Compute alpha to beta migration
3*55e87721SMatt Gilbride
4*55e87721SMatt Gilbride  Java compute library is GA from version 1.7.0 and backwards incompatible with 0.x.x. Also it is incompatible with 1.5.x-alpha and prior in a following way: 
5*55e87721SMatt Gilbride  - Everything except polling methods which used to return `Operation` now returns `OperationFuture`.
6*55e87721SMatt Gilbride  - Library automatically polls Operation status.
7*55e87721SMatt Gilbride  - `Operation op = client.myMethod(args)` should be replaced with `OperationFuture<Operation, Operation> opFuture = client.myMethodAsync(args);`
8*55e87721SMatt Gilbride  - Polling is now done automatically, manual polling is no longer required. Calling `opFuture.get()` will wait for automatic polling to complete. It will return the result of the long running operation once the operation is completed on the server side or throw an exception if an error occurs during polling.
9*55e87721SMatt Gilbride  - To check for intermediate status on the future use either `opFuture.peekMetadata()` (non-blocking) or `opFuture.getMetadata()` (blocking)
10*55e87721SMatt Gilbride  - If you wish to stop automatic polling call `opFuture.cancel()` - it will cancel the future on the client side but it will not affect the execution of the operation on the server side in any way (server will keep working on the operation).
11*55e87721SMatt Gilbride  - The calls still may be done without relying on automatic polling and/or OperationFuture. To do so, use `client.myMethodCallable(MyMethodRequest).call()` semantics instead. Note this semantics does not have flattened method declarations and the request message must be instantiated explicitly by the users code.
12*55e87721SMatt Gilbride
13*55e87721SMatt Gilbride  ### Example
14*55e87721SMatt Gilbride  The following example creates a GCE address, then lists all the available addresses in the region and in the whole project and eventually deletes the newly created address.
15*55e87721SMatt Gilbride
16*55e87721SMatt Gilbride  ```java
17*55e87721SMatt Gilbride  import com.google.cloud.compute.v1.Address;
18*55e87721SMatt Gilbride  import com.google.cloud.compute.v1.AddressesClient;
19*55e87721SMatt Gilbride  import com.google.cloud.compute.v1.AddressesScopedList;
20*55e87721SMatt Gilbride  import com.google.cloud.compute.v1.Operation;
21*55e87721SMatt Gilbride  import com.google.protobuf.util.JsonFormat;
22*55e87721SMatt Gilbride  import java.io.IOException;
23*55e87721SMatt Gilbride  import java.util.Map;
24*55e87721SMatt Gilbride
25*55e87721SMatt Gilbride  public class ComputeExample {
26*55e87721SMatt Gilbride
27*55e87721SMatt Gilbride    public static void main(String[] args) throws IOException, InterruptedException {
28*55e87721SMatt Gilbride      final String project = "PROJECT_NAME";
29*55e87721SMatt Gilbride      final String region = "REGION";
30*55e87721SMatt Gilbride      final String address = "test-address-21";
31*55e87721SMatt Gilbride
32*55e87721SMatt Gilbride      AddressesClient addressesClient = AddressesClient.create();
33*55e87721SMatt Gilbride
34*55e87721SMatt Gilbride      // AddressClient#insert()
35*55e87721SMatt Gilbride      System.out.println("\n===============\nAddressClient#insert()\n===============");
36*55e87721SMatt Gilbride      OperationFuture<Operation,Operation> insertResponse =
37*55e87721SMatt Gilbride          addressesClient.insertAsync(project, region, Address.newBuilder().setName(address).build());
38*55e87721SMatt Gilbride      Operation insertResponseOperation = insertResponse.get();
39*55e87721SMatt Gilbride      System.out.println(JsonFormat.printer().print(insertResponse) + "\n");
40*55e87721SMatt Gilbride
41*55e87721SMatt Gilbride      // AddressClient#list()
42*55e87721SMatt Gilbride      System.out.println("\n===============\nAddressClient#list()\n===============");
43*55e87721SMatt Gilbride      for (Address addr : addressesClient.list(project, region).iterateAll()) {
44*55e87721SMatt Gilbride        System.out.println(JsonFormat.printer().print(addr));
45*55e87721SMatt Gilbride      }
46*55e87721SMatt Gilbride
47*55e87721SMatt Gilbride      // AddressClient#aggregatedList()
48*55e87721SMatt Gilbride      System.out.println("\n===============\nAddressClient#aggregatedList()\n===============");
49*55e87721SMatt Gilbride      for (Map.Entry<String, AddressesScopedList> addr :
50*55e87721SMatt Gilbride          addressesClient.aggregatedList(project).iterateAll()) {
51*55e87721SMatt Gilbride        System.out.println("KEY: " + addr.getKey());
52*55e87721SMatt Gilbride        System.out.println(JsonFormat.printer().print(addr.getValue()));
53*55e87721SMatt Gilbride      }
54*55e87721SMatt Gilbride
55*55e87721SMatt Gilbride      // AddressClient#delete()
56*55e87721SMatt Gilbride      System.out.println("\n===============\nAddressClient#delete()\n===============");
57*55e87721SMatt Gilbride      OperationFuture<Operation,Operation> deleteResponse = addressesClient.deleteAsync(project, region, address);
58*55e87721SMatt Gilbride      Operation deleteResponseOperation = deleteResponse.get();
59*55e87721SMatt Gilbride      System.out.println(JsonFormat.printer().print(deleteResponse) + "\n");
60*55e87721SMatt Gilbride    }
61*55e87721SMatt Gilbride  }
62*55e87721SMatt Gilbride
63*55e87721SMatt Gilbride  ```
64