Skip to content

Commit 4dece7d

Browse files
authored
docs(compute-samples): added snippets for suspend resume instance (GoogleCloudPlatform#7010)
* docs(compute-samples): added VM suspend and resume samples and test * lint fix and changed debian version to suspend supported VM
1 parent 9a75128 commit 4dece7d

5 files changed

Lines changed: 171 additions & 10 deletions

File tree

compute/cloud-client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<dependency>
2424
<artifactId>google-cloud-compute</artifactId>
2525
<groupId>com.google.cloud</groupId>
26-
<version>1.7.0</version>
26+
<version>1.8.1</version>
2727
</dependency>
2828
<dependency>
2929
<artifactId>google-cloud-storage</artifactId>
@@ -63,7 +63,7 @@
6363
<groupId>com.google.cloud</groupId>
6464
<scope>import</scope>
6565
<type>pom</type>
66-
<version>25.0.0</version>
66+
<version>25.1.0</version>
6767
</dependency>
6868
</dependencies>
6969
</dependencyManagement>

compute/cloud-client/src/main/java/compute/CreateInstance.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ public static void main(String[] args)
4646
public static void createInstance(String project, String zone, String instanceName)
4747
throws IOException, InterruptedException, ExecutionException {
4848
// Below are sample values that can be replaced.
49-
// machineType: machine type of the VM being created. This value uses the format zones/{zone}/machineTypes/{type_name}. For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
50-
// sourceImage: path to the operating system image to mount. For details about images you can mount, see https://cloud.google.com/compute/docs/images
49+
// machineType: machine type of the VM being created.
50+
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
51+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
52+
// sourceImage: path to the operating system image to mount.
53+
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
5154
// diskSizeGb: storage size of the boot disk to attach to the instance.
5255
// networkName: network interface to associate with the instance.
5356
String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
5457
String sourceImage = String
55-
.format("projects/debian-cloud/global/images/family/%s", "debian-10");
58+
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
5659
long diskSizeGb = 10L;
5760
String networkName = "default";
5861

@@ -69,12 +72,15 @@ public static void createInstance(String project, String zone, String instanceNa
6972
.setType(Type.PERSISTENT.toString())
7073
.setDeviceName("disk-1")
7174
.setInitializeParams(
72-
AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage)
73-
.setDiskSizeGb(diskSizeGb).build())
75+
AttachedDiskInitializeParams.newBuilder()
76+
.setSourceImage(sourceImage)
77+
.setDiskSizeGb(diskSizeGb)
78+
.build())
7479
.build();
7580

7681
// Use the network interface provided in the networkName argument.
77-
NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName)
82+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
83+
.setName(networkName)
7884
.build();
7985

8086
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
@@ -92,7 +98,8 @@ public static void createInstance(String project, String zone, String instanceNa
9298
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
9399
.setProject(project)
94100
.setZone(zone)
95-
.setInstanceResource(instanceResource).build();
101+
.setInstanceResource(instanceResource)
102+
.build();
96103

97104
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
98105
insertInstanceRequest);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compute;
18+
19+
// [START compute_resume_instance]
20+
21+
import com.google.cloud.compute.v1.Instance.Status;
22+
import com.google.cloud.compute.v1.InstancesClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class ResumeInstance {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// project: project ID or project number of the Cloud project your instance belongs to.
35+
// zone: name of the zone your instance belongs to.
36+
// instanceName: name of the instance your want to resume.
37+
38+
String project = "your-project-id";
39+
String zone = "zone-name";
40+
String instanceName = "instance-name";
41+
42+
resumeInstance(project, zone, instanceName);
43+
}
44+
45+
// Resume a suspended Google Compute Engine instance (with unencrypted disks).
46+
// Instance state changes to RUNNING, if successfully resumed.
47+
public static void resumeInstance(String project, String zone, String instanceName)
48+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
49+
// Instantiates a client.
50+
try (InstancesClient instancesClient = InstancesClient.create()) {
51+
52+
String currentInstanceState = instancesClient.get(project, zone, instanceName).getStatus();
53+
54+
// Check if the instance is currently suspended.
55+
if (!currentInstanceState.equalsIgnoreCase(Status.SUSPENDED.toString())) {
56+
throw new RuntimeException(
57+
String.format("Only suspended instances can be resumed. Instance %s is in %s state.",
58+
instanceName, currentInstanceState));
59+
}
60+
61+
Operation operation = instancesClient.resumeAsync(project, zone, instanceName)
62+
.get(300, TimeUnit.SECONDS);
63+
64+
if (operation.hasError() || !instancesClient.get(project, zone, instanceName).getStatus()
65+
.equalsIgnoreCase(
66+
Status.RUNNING.toString())) {
67+
System.out.println("Cannot resume instance. Try again!");
68+
return;
69+
}
70+
71+
System.out.printf("Instance resumed successfully ! %s", instanceName);
72+
}
73+
}
74+
}
75+
// [END compute_resume_instance]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compute;
18+
19+
// [START compute_suspend_instance]
20+
21+
import com.google.cloud.compute.v1.Instance.Status;
22+
import com.google.cloud.compute.v1.InstancesClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class SuspendInstance {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// project: project ID or project number of the Cloud project your instance belongs to.
35+
// zone: name of the zone your instance belongs to.
36+
// instanceName: name of the instance your want to suspend.
37+
38+
String project = "your-project-id";
39+
String zone = "zone-name";
40+
String instanceName = "instance-name";
41+
42+
suspendInstance(project, zone, instanceName);
43+
}
44+
45+
// Suspend a running Google Compute Engine instance.
46+
// For limitations and compatibility on which instances can be suspended,
47+
// see: https://cloud.google.com/compute/docs/instances/suspend-resume-instance#limitations
48+
public static void suspendInstance(String project, String zone, String instanceName)
49+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
50+
// Instantiates a client.
51+
try (InstancesClient instancesClient = InstancesClient.create()) {
52+
53+
Operation operation = instancesClient.suspendAsync(project, zone, instanceName)
54+
.get(300, TimeUnit.SECONDS);
55+
56+
if (operation.hasError() || !instancesClient.get(project, zone, instanceName).getStatus()
57+
.equalsIgnoreCase(Status.SUSPENDED.toString())) {
58+
System.out.println("Cannot suspend instance. Try again!");
59+
return;
60+
}
61+
62+
System.out.printf("Instance suspended successfully ! %s", instanceName);
63+
}
64+
}
65+
}
66+
// [END compute_suspend_instance]

compute/cloud-client/src/test/java/compute/SnippetsIT.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.UUID;
4949
import java.util.concurrent.ExecutionException;
5050
import java.util.concurrent.TimeUnit;
51+
import java.util.concurrent.TimeoutException;
5152
import java.util.stream.IntStream;
5253
import org.junit.After;
5354
import org.junit.AfterClass;
@@ -490,7 +491,19 @@ public void testListImagesByPage() throws IOException {
490491

491492
@Test
492493
public void testInstanceOperations()
493-
throws IOException, ExecutionException, InterruptedException {
494+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
495+
Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString());
496+
497+
// Suspending the instance.
498+
// Once the machine is running, give it some time to fully start all processes
499+
// before trying to suspend it.
500+
TimeUnit.SECONDS.sleep(45);
501+
SuspendInstance.suspendInstance(PROJECT_ID, ZONE, MACHINE_NAME);
502+
TimeUnit.SECONDS.sleep(10);
503+
Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.SUSPENDED.toString());
504+
505+
// Resuming the instance.
506+
ResumeInstance.resumeInstance(PROJECT_ID, ZONE, MACHINE_NAME);
494507
Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString());
495508

496509
// Stopping the instance.

0 commit comments

Comments
 (0)