diff --git a/README.md b/README.md index 1989ae824dba..80a0b4d5c5e3 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ This library supports the following Google Cloud Platform services with clients This library supports the following Google Cloud Platform services with clients at an [Alpha](#versioning) quality level: +- [Cloud Compute](google-cloud-clients/google-cloud-compute) (Alpha) - [Cloud Dataproc](google-cloud-clients/google-cloud-dataproc) (Alpha) - [Cloud DNS](google-cloud-clients/google-cloud-dns) (Alpha) - [Cloud OS Login](google-cloud-clients/google-cloud-os-login) (Alpha) @@ -46,10 +47,6 @@ This library supports the following Google Cloud Platform services with clients - [Dialogflow](google-cloud-clients/google-cloud-dialogflow) (Alpha) - [Web Security Scanner](google-cloud-clients/google-cloud-websecurityscanner) (Alpha) -These libraries are deprecated and no longer receive updates: - -- [Cloud Compute](google-cloud-clients/google-cloud-compute) (Deprecated) - Quickstart ---------- diff --git a/google-cloud-clients/google-cloud-compute/README.md b/google-cloud-clients/google-cloud-compute/README.md index 8c40bc895b0a..05f1dabb5af6 100644 --- a/google-cloud-clients/google-cloud-compute/README.md +++ b/google-cloud-clients/google-cloud-compute/README.md @@ -12,7 +12,6 @@ Java idiomatic client for [Google Cloud Compute][cloud-compute]. - [Product Documentation][compute-product-docs] - [Client Library Documentation][compute-client-lib-docs] -> Note: This client is no longer receiving updates; new features in the Compute API will not be added to this client. Check https://cloud.google.com/compute/docs/api/libraries for the recommended Java client library to use for accessing Compute. @@ -89,10 +88,24 @@ These credentials are automatically inferred from your environment, so you only code to create your service object: ```java -import com.google.cloud.compute.deprecated.Compute; -import com.google.cloud.compute.deprecated.ComputeOptions; - -Compute compute = ComputeOptions.getDefaultInstance().getService(); +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.compute.v1.AddressClient; +import com.google.cloud.compute.v1.AddressSettings; + +Credentials myCredentials = GoogleCredentials.getApplicationDefault(); + String myEndpoint = AddressSettings.getDefaultEndpoint(); + + AddressSettings addressSettings = + AddressSettings.newBuilder() + .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) + .setTransportChannelProvider( + AddressSettings.defaultHttpJsonTransportProviderBuilder() + .setEndpoint(myEndpoint) + .build()) + .build(); + return AddressClient.create(addressSettings); ``` For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) @@ -106,9 +119,9 @@ Engine. In this code snippet, we will create a new external region address. Add the following imports at the top of your file: ```java -import com.google.cloud.compute.deprecated.AddressInfo; -import com.google.cloud.compute.deprecated.Operation; -import com.google.cloud.compute.deprecated.RegionAddressId; +import com.google.cloud.compute.v1.InsertAddressHttpRequest; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ProjectRegionAddressName; ``` Then add the following code to create an address. Most Compute Engine calls return an `Operation` @@ -116,14 +129,19 @@ object that can be used to wait for operation completion and to check whether op succeeded: ```java -RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address"); -Operation operation = compute.create(AddressInfo.of(addressId)); -// Wait for operation to complete -operation = operation.waitFor(); -if (operation.getErrors() == null) { +ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); +Address address = Address.newBuilder().build(); +InsertAddressHttpRequest request = + InsertAddressHttpRequest.newBuilder() + .setRegion(region.toString()) + .setAddressResource(address) + .build(); + +Operation response = client.insertAddress(request); +if (operation.getError() == null) { System.out.println("Address " + addressId + " was successfully created"); } else { - // inspect operation.getErrors() + // inspect operation.getError() throw new RuntimeException("Address creation failed"); } ``` @@ -138,94 +156,42 @@ a publicly-available image. Add the following imports at the top of your file: ```java -import com.google.cloud.compute.deprecated.DiskInfo; -import com.google.cloud.compute.deprecated.DiskId; -import com.google.cloud.compute.deprecated.ImageDiskConfiguration; -import com.google.cloud.compute.deprecated.ImageId; +import com.google.api.core.ApiFuture; +import com.google.cloud.compute.v1.Disk; +import com.google.cloud.compute.v1.DiskClient; +import com.google.cloud.compute.v1.InsertDiskHttpRequest; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ProjectZoneName; ``` Then add the following code to create a disk and wait for disk creation to terminate. ```java -ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); -DiskId diskId = DiskId.of("us-central1-a", "test-disk"); -ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId); -DiskInfo disk = DiskInfo.of(diskId, diskConfiguration); -Operation operation = compute.create(disk); -// Wait for operation to complete -operation = operation.waitFor(); -if (operation.getErrors() == null) { - System.out.println("Disk " + diskId + " was successfully created"); -} else { - // inspect operation.getErrors() +ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]"); +Disk diskResource = Disk.newBuilder().build(); +InsertDiskHttpRequest request = InsertDiskHttpRequest.newBuilder() + .setZone(zone.toString()) + .setDiskResource(diskResource) + .build(); +ApiFuture future = client.insertDiskCallable().futureCall(request); +Operation response; +try { + response = future.get(); +} catch (InterruptedException | ExecutionException e) { + // inspect operation.getError() throw new RuntimeException("Disk creation failed"); } ``` -#### Creating a virtual machine instance -A Google Compute Engine instance is a virtual machine (VM) hosted on Google's infrastructure. An -instance can be created given its identity, a machine type, one boot disk and a network interface. -In this code snippet, we will create a virtual machine instance in the default network using as a -boot disk the disk we have just created and assigning to it the just created IP address. - -Add the following imports at the top of your file: - -```java -import com.google.cloud.compute.deprecated.AttachedDisk; -import com.google.cloud.compute.deprecated.AttachedDisk.PersistentDiskConfiguration; -import com.google.cloud.compute.deprecated.InstanceId; -import com.google.cloud.compute.deprecated.InstanceInfo; -import com.google.cloud.compute.deprecated.MachineTypeId; -import com.google.cloud.compute.deprecated.NetworkConfiguration; -import com.google.cloud.compute.deprecated.NetworkConfiguration.AccessConfig; -import com.google.cloud.compute.deprecated.NetworkId; -import com.google.cloud.compute.deprecated.NetworkInterface; -``` - -Then add the following code to create an instance and wait for instance creation to terminate. - -```java -Address externalIp = compute.getAddress(addressId); -InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance"); -NetworkId networkId = NetworkId.of("default"); -PersistentDiskConfiguration attachConfiguration = - PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build(); -AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration); -NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId) - .setAccessConfigurations(AccessConfig.of(externalIp.getAddress())) - .build(); -MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); -InstanceInfo instance = - InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface); -Operation operation = compute.create(instance); -// Wait for operation to complete -operation = operation.waitFor(); -if (operation.getErrors() == null) { - System.out.println("Instance " + instanceId + " was successfully created"); -} else { - // inspect operation.getErrors() - throw new RuntimeException("Instance creation failed"); -} -``` - #### Complete source code In -[CreateAddressDiskAndInstance.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateAddressDiskAndInstance.java) +[ComputeExample.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.java) we put together all the code shown above into one program. The program assumes that you are running on Compute Engine or from your own desktop. To run the example on App Engine, simply move the code from the main method to your application's servlet class and change the print statements to display on your webpage. -#### Other examples - -Other examples are available too: - -- [CreateSnapshot.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateSnapshot.java) shows -how to create a snapshot from an existing disk -- [CreateInstance.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateInstance.java) shows -how to create a virtual machine instance (shorter sample than the one above) - Troubleshooting --------------- @@ -276,4 +242,4 @@ Apache 2.0 - See [LICENSE] for more information. [cloud-compute]: https://cloud.google.com/compute/ [compute-product-docs]: https://cloud.google.com/compute/docs/ -[compute-client-lib-docs]: https://googlecloudplatform.github.io/google-cloud-java/google-cloud-clients/apidocs/index.html?com/google/cloud/compute/deprecated/package-summary.html +[compute-client-lib-docs]: https://googlecloudplatform.github.io/google-cloud-java/google-cloud-clients/apidocs/index.html?com/google/cloud/compute/v1/package-summary.html diff --git a/google-cloud-examples/pom.xml b/google-cloud-examples/pom.xml index 8ff50f8c3c44..52278a9222ce 100644 --- a/google-cloud-examples/pom.xml +++ b/google-cloud-examples/pom.xml @@ -134,7 +134,11 @@ BigQueryExample - com.google.cloud.examples.compute.ComputeExample + com.google.cloud.examples.compute.deprecated.ComputeExample + ComputeExample + + + com.google.cloud.examples.compute.v1.ComputeExample ComputeExample diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/ComputeExample.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/deprecated/ComputeExample.java similarity index 99% rename from google-cloud-examples/src/main/java/com/google/cloud/examples/compute/ComputeExample.java rename to google-cloud-examples/src/main/java/com/google/cloud/examples/compute/deprecated/ComputeExample.java index 2a2f53810925..167fc8775bfd 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/ComputeExample.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/deprecated/ComputeExample.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.cloud.examples.compute; +package com.google.cloud.examples.compute.deprecated; import com.google.api.gax.paging.Page; import com.google.cloud.Tuple; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.java new file mode 100644 index 000000000000..cdcd380b8a47 --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.java @@ -0,0 +1,144 @@ +package com.google.cloud.examples.compute.v1; + +import com.google.api.core.ApiFuture; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.compute.v1.Address; +import com.google.cloud.compute.v1.AddressClient; +import com.google.cloud.compute.v1.AddressSettings; +import com.google.cloud.compute.v1.InsertAddressHttpRequest; +import com.google.cloud.compute.v1.ListAddressesHttpRequest; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.ProjectRegionAddressName; +import com.google.cloud.compute.v1.ProjectRegionName; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +/** Working example code to make live calls on Addresses resources in a GCP Compute project. */ +public class ComputeExample { + + // Replace the following String values with your Project and Region ids. + private static String PROJECT_NAME = "my-project-id"; + private static String REGION = "us-central1"; + + /** + * List addresses, Insert an address, and then delete the address. Use ResourceNames in the + * request objects. + */ + public static void main(String[] args) throws IOException { + AddressClient client = createCredentialedClient(); + + System.out.println("Running with Gapic Client."); + AddressClient.ListAddressesPagedResponse listResponse = listAddresses(client); + verifyListAddressWithGets(client, listResponse); + + System.out.println("Running with Gapic Client and Resource Name."); + String newAddressName = "new_address_name"; + System.out.println("Inserting address:"); + + insertNewAddressJustClient(client, newAddressName); + + listAddresses(client); + + System.out.println("Deleting address:"); + Operation deleteResponse = + client.deleteAddress(ProjectRegionAddressName.of(newAddressName, PROJECT_NAME, REGION)); + System.out.format("Result of delete: %s\n", deleteResponse.toString()); + int sleepTimeInSeconds = 3; + System.out.format("Waiting %d seconds for server to update...\n", sleepTimeInSeconds); + // Wait for the delete operation to finish on the server. + try { + TimeUnit.SECONDS.sleep(sleepTimeInSeconds); + } catch (InterruptedException e) { + e.printStackTrace(); + } + listAddresses(client); + } + + private static AddressClient createCredentialedClient() throws IOException { + Credentials myCredentials = GoogleCredentials.getApplicationDefault(); + String myEndpoint = AddressSettings.getDefaultEndpoint(); + + AddressSettings addressSettings = + AddressSettings.newBuilder() + .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) + .setTransportChannelProvider( + AddressSettings.defaultHttpJsonTransportProviderBuilder() + .setEndpoint(myEndpoint) + .build()) + .build(); + return AddressClient.create(addressSettings); + } + + private static void insertNewAddressJustClient(AddressClient client, String newAddressName) { + // Begin samplegen code for insertAddress(). + Address newAddress = Address.newBuilder().setName(newAddressName).build(); + ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); + Operation response = client.insertAddress(region, newAddress); + // End samplegen code for insertAddress(). + System.out.format("Result of insert: %s\n", response.toString()); + } + + /** Use an InsertAddressHttpRequest object to make an addresses.insert method call. */ + private static void insertNewAddressUsingRequest(AddressClient client, String newAddressName) + throws InterruptedException, ExecutionException { + // Begin samplegen code for insertAddress(). + ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); + Address address = Address.newBuilder().build(); + InsertAddressHttpRequest request = + InsertAddressHttpRequest.newBuilder() + .setRegion(region.toString()) + .setAddressResource(address) + .build(); + // Do something + Operation response = client.insertAddress(request); + + // End samplegen code for insertAddress(). + System.out.format("Result of insert: %s\n", response.toString()); + } + + /** Use an callable object to make an addresses.insert method call. */ + private static void insertAddressUsingCallable(AddressClient client, String newAddressName) + throws InterruptedException, ExecutionException { + // Begin samplegen code for insertAddress(). + ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION); + Address address = Address.newBuilder().build(); + InsertAddressHttpRequest request = + InsertAddressHttpRequest.newBuilder() + .setRegion(region.toString()) + .setAddressResource(address) + .build(); + ApiFuture future = client.insertAddressCallable().futureCall(request); + // Do something + Operation response = future.get(); + + // End samplegen code for insertAddress(). + System.out.format("Result of insert: %s\n", response.toString()); + } + + /** List Addresses in the under the project PROJECT_NAME and region REGION. */ + private static AddressClient.ListAddressesPagedResponse listAddresses(AddressClient client) { + System.out.println("Listing addresses:"); + ProjectRegionName regionName = + ProjectRegionName.newBuilder().setRegion(REGION).setProject(PROJECT_NAME).build(); + ListAddressesHttpRequest listRequest = + ListAddressesHttpRequest.newBuilder().setRegion(regionName.toString()).build(); + AddressClient.ListAddressesPagedResponse response = client.listAddresses(listRequest); + for (Address address : response.iterateAll()) { + System.out.println("\t - " + address.toString()); + } + return response; + } + + private static void verifyListAddressWithGets( + AddressClient client, AddressClient.ListAddressesPagedResponse listResponse) { + for (Address address : listResponse.iterateAll()) { + System.out.format("Making get request for address \"%s\"...\n", address.getName()); + Address fetchedAddress = + client.getAddress(ProjectRegionAddressName.of(address.getName(), PROJECT_NAME, REGION)); + System.out.format("addresses.get returns \n\t%s\n\n", fetchedAddress.toString()); + } + } +}