Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions compute/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-httpjson</artifactId>
<version>0.100.0</version>
<version>0.99.0</version>
</dependency>


Expand All @@ -54,6 +54,25 @@
<scope>test</scope>
<version>4.13.2</version>
</dependency>

<!--
JUnit Jupiter dependencies to run BeforeEach and AfterEach methods
(in tandem with mvn surefire) before every test.
Without these, mvn surefire skips these methods and leads to concurrency
issues.
-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand All @@ -63,7 +82,7 @@
<groupId>com.google.cloud</groupId>
<scope>import</scope>
<type>pom</type>
<version>25.1.0</version>
<version>25.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand All @@ -88,4 +107,33 @@

<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
<configuration>
<!-- run both classes and methods in parallel -->
<parallel>all</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<forkCount>10C</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/*IT.java</include>
</includes>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateEncryptedInstance {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "your-project-id";
String zone = "zone-name";
Expand All @@ -48,7 +50,7 @@ public static void main(String[] args)
// in the specified project and zone.
public static void createEncryptedInstance(String project, String zone, String instanceName,
String diskEncryptionKey)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
/* Below are sample values that can be replaced.
machineType: machine type of the VM being created.
(This value uses the format zones/{zone}/machineTypes/{type_name}.
Expand Down Expand Up @@ -111,7 +113,7 @@ public static void createEncryptedInstance(String project, String zone, String i
instancesClient.insertAsync(insertInstanceRequest);

// Wait for the operation to complete.
Operation response = operation.get();
Operation response = operation.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance creation failed ! ! " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateFirewallRule {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample
/* project: project ID or project number of the Cloud project you want to use.
firewallRuleName: name of the rule that is created.
Expand All @@ -49,7 +51,7 @@ public static void main(String[] args)
// Creates a simple firewall rule allowing for incoming HTTP and
// HTTPS access from the entire Internet.
public static void createFirewall(String project, String firewallRuleName, String network)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `firewallsClient.close()` method on the client to safely
Expand Down Expand Up @@ -78,7 +80,7 @@ public static void createFirewall(String project, String firewallRuleName, Strin
.setFirewallResource(firewallRule)
.setProject(project).build();

firewallsClient.insertAsync(insertFirewallRequest).get();
firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES);

System.out.println("Firewall rule created successfully -> " + firewallRuleName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstance {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "your-project-id";
String zone = "zone-name";
Expand All @@ -44,7 +46,7 @@ public static void main(String[] args)

// Create a new instance with the provided "instanceName" value in the specified project and zone.
public static void createInstance(String project, String zone, String instanceName)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Below are sample values that can be replaced.
// machineType: machine type of the VM being created.
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
Expand Down Expand Up @@ -105,7 +107,7 @@ public static void createInstance(String project, String zone, String instanceNa
insertInstanceRequest);

// Wait for the operation to complete.
Operation response = operation.get();
Operation response = operation.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance creation failed ! ! " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstanceFromTemplate {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* TODO(developer): Replace these variables before running the sample.
projectId - ID or number of the project you want to use.
zone - Name of the zone you want to check, for example: us-west3-b
Expand All @@ -51,7 +53,7 @@ public static void main(String[] args)
// Create a new instance from template in the specified project and zone.
public static void createInstanceFromTemplate(String projectId, String zone, String instanceName,
String instanceTemplateUrl)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {

try (InstancesClient instancesClient = InstancesClient.create()) {

Expand All @@ -61,7 +63,8 @@ public static void createInstanceFromTemplate(String projectId, String zone, Str
.setInstanceResource(Instance.newBuilder().setName(instanceName).build())
.setSourceInstanceTemplate(instanceTemplateUrl).build();

Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
Operation response = instancesClient.insertAsync(insertInstanceRequest)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance creation from template failed ! ! " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstanceFromTemplateWithOverrides {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* TODO(developer): Replace these variables before running the sample.
* projectId - ID or number of the project you want to use.
* zone - Name of the zone you want to check, for example: us-west3-b
Expand Down Expand Up @@ -62,7 +64,7 @@ public static void main(String[] args)
// but overrides the disk and machine type options in the template.
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone,
String instanceName, String instanceTemplateName)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {

try (InstancesClient instancesClient = InstancesClient.create();
InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
Expand Down Expand Up @@ -100,7 +102,8 @@ public static void createInstanceFromTemplateWithOverrides(String projectId, Str
.setInstanceResource(instance)
.setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();

Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
Operation response = instancesClient.insertAsync(insertInstanceRequest)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance creation from template with overrides failed ! ! " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstanceTemplate {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// projectId: project ID or project number of the Cloud project you use.
// templateName: name of the new template to create.
Expand All @@ -47,7 +49,7 @@ public static void main(String[] args)
instance configuration.
*/
public static void createInstanceTemplate(String projectId, String templateName)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {

String machineType = "e2-standard-4";
Expand Down Expand Up @@ -85,7 +87,8 @@ public static void createInstanceTemplate(String projectId, String templateName)
.setProperties(instanceProperties).build()).build();

// Create the Instance Template.
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
Expand All @@ -97,7 +100,7 @@ public static void createInstanceTemplate(String projectId, String templateName)
}

public static void createInstanceTemplateWithDiskType(String projectId, String templateName)
throws IOException, ExecutionException, InterruptedException {
throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {

Expand All @@ -122,7 +125,8 @@ public static void createInstanceTemplateWithDiskType(String projectId, String t
.setProject(projectId)
.setInstanceTemplateResource(instanceTemplate).build();

Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.io.IOException;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstancesAdvanced {
// [END compute_instances_create_from_image]
Expand Down Expand Up @@ -195,7 +197,7 @@ private static AttachedDisk diskFromSnapshot(String diskType, int diskSizeGb, bo
*/
private static Instance createWithDisks(String project, String zone, String instanceName,
Vector<AttachedDisk> disks, String machineType, String network, String subnetwork)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (InstancesClient instancesClient = InstancesClient.create()) {
// Use the network interface provided in the networkName argument.
NetworkInterface networkInterface;
Expand Down Expand Up @@ -231,7 +233,7 @@ private static Instance createWithDisks(String project, String zone, String inst
insertInstanceRequest);

// Wait for the operation to complete.
Operation response = operation.get();
Operation response = operation.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
System.out.println("Instance creation failed ! ! " + response);
Expand Down Expand Up @@ -260,7 +262,7 @@ private static Instance createWithDisks(String project, String zone, String inst
* @return Instance object.
*/
public static Instance createFromPublicImage(String project, String zone, String instanceName)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create()) {
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
Expand All @@ -287,7 +289,7 @@ public static Instance createFromPublicImage(String project, String zone, String
*/
public static Instance createFromCustomImage(String project, String zone, String instanceName,
String customImage)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
Vector<AttachedDisk> disks = new Vector<>();
disks.add(diskFromImage(diskType, 10, true, customImage));
Expand All @@ -307,7 +309,7 @@ public static Instance createFromCustomImage(String project, String zone, String
* @return Instance object.
*/
public static Instance createWithAdditionalDisk(String project, String zone, String instanceName)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create()) {
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
Expand Down Expand Up @@ -335,7 +337,7 @@ public static Instance createWithAdditionalDisk(String project, String zone, Str
*/
public static Instance createFromSnapshot(String project, String zone, String instanceName,
String snapshotName)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
Vector<AttachedDisk> disks = new Vector<>();
disks.add(diskFromSnapshot(diskType, 11, true, snapshotName));
Expand All @@ -358,7 +360,7 @@ public static Instance createFromSnapshot(String project, String zone, String in
*/
public static Instance createWithSnapshottedDataDisk(String project, String zone,
String instanceName, String snapshotName)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create()) {
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
Expand Down Expand Up @@ -389,7 +391,7 @@ public static Instance createWithSnapshottedDataDisk(String project, String zone
*/
public static Instance createWithSubnetwork(String project, String zone, String instanceName,
String networkLink, String subnetworkLink)
throws IOException, InterruptedException, ExecutionException {
throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create()) {
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
Expand Down
Loading