|
| 1 | +package com.google.cloud.examples.compute.v1; |
| 2 | + |
| 3 | +import com.google.api.core.ApiFuture; |
| 4 | +import com.google.api.gax.core.FixedCredentialsProvider; |
| 5 | +import com.google.auth.Credentials; |
| 6 | +import com.google.auth.oauth2.GoogleCredentials; |
| 7 | +import com.google.cloud.compute.v1.Address; |
| 8 | +import com.google.cloud.compute.v1.AddressesClient; |
| 9 | +import com.google.cloud.compute.v1.AddressesSettings; |
| 10 | +import com.google.cloud.compute.v1.DeleteAddressRequest; |
| 11 | +import com.google.cloud.compute.v1.GetAddressRequest; |
| 12 | +import com.google.cloud.compute.v1.InsertAddressRequest; |
| 13 | +import com.google.cloud.compute.v1.ListAddressesRequest; |
| 14 | +import com.google.cloud.compute.v1.Operation; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +/** Working example code to make live calls on Addresses resources in a GCP Compute project. */ |
| 20 | +public class ComputeExample { |
| 21 | + |
| 22 | + // Replace the following String values with your Project and Region ids. |
| 23 | + private static String PROJECT_NAME = "my-project-id"; |
| 24 | + private static String REGION = "us-central1"; |
| 25 | + |
| 26 | + /** |
| 27 | + * List addresses, Insert an address, and then delete the address. Use ResourceNames in the |
| 28 | + * request objects. |
| 29 | + */ |
| 30 | + public static void main(String[] args) throws IOException { |
| 31 | + AddressesClient client = createCredentialedClient(); |
| 32 | + |
| 33 | + System.out.println("Running with Gapic Client."); |
| 34 | + AddressesClient.ListPagedResponse listResponse = listAddresses(client); |
| 35 | + verifyListAddressWithGets(client, listResponse); |
| 36 | + |
| 37 | + System.out.println("Running with Gapic Client and Resource Name."); |
| 38 | + String newAddressName = "new_address_name"; |
| 39 | + System.out.println("Inserting address:"); |
| 40 | + |
| 41 | + insertNewAddressJustClient(client, newAddressName); |
| 42 | + |
| 43 | + listAddresses(client); |
| 44 | + |
| 45 | + System.out.println("Deleting address:"); |
| 46 | + Operation deleteResponse = |
| 47 | + client.delete( |
| 48 | + DeleteAddressRequest.newBuilder() |
| 49 | + .setProject(PROJECT_NAME) |
| 50 | + .setAddress(newAddressName) |
| 51 | + .setRegion(REGION) |
| 52 | + .build()); |
| 53 | + System.out.format("Result of delete: %s\n", deleteResponse.toString()); |
| 54 | + int sleepTimeInSeconds = 3; |
| 55 | + System.out.format("Waiting %d seconds for server to update...\n", sleepTimeInSeconds); |
| 56 | + // Wait for the delete operation to finish on the server. |
| 57 | + try { |
| 58 | + TimeUnit.SECONDS.sleep(sleepTimeInSeconds); |
| 59 | + } catch (InterruptedException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + listAddresses(client); |
| 63 | + } |
| 64 | + |
| 65 | + private static AddressesClient createCredentialedClient() throws IOException { |
| 66 | + Credentials myCredentials = GoogleCredentials.getApplicationDefault(); |
| 67 | + String myEndpoint = AddressesSettings.getDefaultEndpoint(); |
| 68 | + |
| 69 | + AddressesSettings addressesSettings = |
| 70 | + AddressesSettings.newBuilder() |
| 71 | + .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) |
| 72 | + .setTransportChannelProvider( |
| 73 | + AddressesSettings.defaultHttpJsonTransportProviderBuilder() |
| 74 | + .setEndpoint(myEndpoint) |
| 75 | + .build()) |
| 76 | + .build(); |
| 77 | + return AddressesClient.create(addressesSettings); |
| 78 | + } |
| 79 | + |
| 80 | + private static void insertNewAddressJustClient(AddressesClient client, String newAddressName) { |
| 81 | + // Begin samplegen code for insertAddress(). |
| 82 | + Address newAddress = Address.newBuilder().setName(newAddressName).build(); |
| 83 | + Operation response = client.insert(PROJECT_NAME, REGION, newAddress); |
| 84 | + // End samplegen code for insertAddress(). |
| 85 | + System.out.format("Result of insert: %s\n", response.toString()); |
| 86 | + } |
| 87 | + |
| 88 | + /** Use an InsertAddressRequest object to make an addresses.insert method call. */ |
| 89 | + private static void insertNewAddressUsingRequest(AddressesClient client, String newAddressName) |
| 90 | + throws InterruptedException, ExecutionException { |
| 91 | + // Begin samplegen code for insertAddress(). |
| 92 | + Address address = Address.newBuilder().build(); |
| 93 | + InsertAddressRequest request = |
| 94 | + InsertAddressRequest.newBuilder().setRegion(REGION).setAddressResource(address).build(); |
| 95 | + // Do something |
| 96 | + Operation response = client.insert(request); |
| 97 | + |
| 98 | + // End samplegen code for insertAddress(). |
| 99 | + System.out.format("Result of insert: %s\n", response.toString()); |
| 100 | + } |
| 101 | + |
| 102 | + /** Use an callable object to make an addresses.insert method call. */ |
| 103 | + private static void insertAddressUsingCallable(AddressesClient client, String newAddressName) |
| 104 | + throws InterruptedException, ExecutionException { |
| 105 | + // Begin samplegen code for insertAddress(). |
| 106 | + Address address = Address.newBuilder().build(); |
| 107 | + InsertAddressRequest request = |
| 108 | + InsertAddressRequest.newBuilder() |
| 109 | + .setProject(PROJECT_NAME) |
| 110 | + .setRegion(REGION) |
| 111 | + .setAddressResource(address) |
| 112 | + .build(); |
| 113 | + ApiFuture<Operation> future = client.insertCallable().futureCall(request); |
| 114 | + // Do something |
| 115 | + Operation response = future.get(); |
| 116 | + |
| 117 | + // End samplegen code for insertAddress(). |
| 118 | + System.out.format("Result of insert: %s\n", response.toString()); |
| 119 | + } |
| 120 | + |
| 121 | + /** List Addresses in the under the project PROJECT_NAME and region REGION. */ |
| 122 | + private static AddressesClient.ListPagedResponse listAddresses(AddressesClient client) { |
| 123 | + System.out.println("Listing addresses:"); |
| 124 | + ListAddressesRequest listRequest = |
| 125 | + ListAddressesRequest.newBuilder().setProject(PROJECT_NAME).setRegion(REGION).build(); |
| 126 | + AddressesClient.ListPagedResponse response = client.list(listRequest); |
| 127 | + for (Address address : response.iterateAll()) { |
| 128 | + System.out.println("\t - " + address.toString()); |
| 129 | + } |
| 130 | + return response; |
| 131 | + } |
| 132 | + |
| 133 | + private static void verifyListAddressWithGets( |
| 134 | + AddressesClient client, AddressesClient.ListPagedResponse listResponse) { |
| 135 | + for (Address address : listResponse.iterateAll()) { |
| 136 | + System.out.format("Making get request for address \"%s\"...\n", address.getName()); |
| 137 | + |
| 138 | + Address fetchedAddress = |
| 139 | + client.get( |
| 140 | + GetAddressRequest.newBuilder() |
| 141 | + .setAddress(address.getName()) |
| 142 | + .setProject(PROJECT_NAME) |
| 143 | + .setRegion(REGION) |
| 144 | + .build()); |
| 145 | + System.out.format("addresses.get returns \n\t%s\n\n", fetchedAddress.toString()); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments