/java-compute

Primary LanguageJavaApache License 2.0Apache-2.0

Google Compute Engine Client for Java

Java idiomatic client for Compute Engine.

Maven Stability

Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.

Quickstart

If you are using Maven with BOM, add this to your pom.xml file

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>9.1.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-compute</artifactId>
  </dependency>

If you are using Maven without BOM, add this to your dependencies:

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-compute</artifactId>
  <version>0.118.0-alpha</version>
</dependency>

If you are using Gradle, add this to your dependencies

compile 'com.google.cloud:google-cloud-compute:0.119.0-alpha'

If you are using SBT, add this to your dependencies

libraryDependencies += "com.google.cloud" % "google-cloud-compute" % "0.119.0-alpha"

Authentication

See the Authentication section in the base directory's README.

Getting Started

Prerequisites

You will need a Google Cloud Platform Console project with the Compute Engine API enabled. You will need to enable billing to use Google Compute Engine. Follow these instructions to get your project set up. You will also need to set up the local development environment by installing the Google Cloud SDK and running the following commands in command line: gcloud auth login and gcloud config set project [YOUR PROJECT ID].

Installation and setup

You'll need to obtain the google-cloud-compute library. See the Quickstart section to add google-cloud-compute as a dependency in your code.

About Compute Engine

Compute Engine delivers virtual machines running in Google's innovative data centers and worldwide fiber network. Compute Engine's tooling and workflow support enable scaling from single instances to global, load-balanced cloud computing. Compute Engine's VMs boot quickly, come with persistent disk storage, deliver consistent performance and are available in many configurations.

See the Compute Engine client library docs to learn how to use this Compute Engine Client Library.

Creating an authorized service object

To make authenticated requests to Google Cloud Compute Engine, you must create a service object with credentials. You can then make API calls by calling methods on the Compute service object. The simplest way to authenticate is to use Application Default Credentials. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:

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 page.

Creating a region IP address

An external region IP address can be associated to a Google Compute Engine instance to communicate with instances in different regions or to communicate with the instance from ouside of Compute Engine. In this code snippet, we will create a new external region address.

Add the following imports at the top of your file:

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 object that can be used to wait for operation completion and to check whether operation failed or succeeded:

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.getError()
  throw new RuntimeException("Address creation failed");
}

Creating a persistent disk

A persistent disk can be used as primary storage for your virtual machine instances. Persistent disks can be created empty, from a disk image or from a disk snapshot. Compute Engine offers publicly-available images of certain operating systems that you can use. In this code snippet, we will create a new persistent disk from a publicly-available image.

Add the following imports at the top of your file:

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.

ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");
Disk diskResource = Disk.newBuilder().build();
InsertDiskHttpRequest request = InsertDiskHttpRequest.newBuilder()
   .setZone(zone.toString())
       .setDiskResource(diskResource)
       .build();
ApiFuture<Operation> future = client.insertDiskCallable().futureCall(request);
Operation response;
try {
  response = future.get();
} catch (InterruptedException | ExecutionException e) {
  // inspect operation.getError()
  throw new RuntimeException("Disk creation failed");
}

Complete source code

In 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.

Troubleshooting

To get help, follow the instructions in the shared Troubleshooting document.

Transport

Compute Engine uses HTTP/JSON for the transport layer.

Java Versions

Java 7 or above is required for using this client.

Versioning

This library follows Semantic Versioning.

It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

License

Apache 2.0 - See LICENSE for more information.

CI Status

Java Version Status
Java 7 Kokoro CI
Java 8 Kokoro CI
Java 8 OSX Kokoro CI
Java 8 Windows Kokoro CI
Java 11 Kokoro CI