Skip to content

Commit 29784f1

Browse files
feat(compute): add compute reservation vms update sample (GoogleCloudPlatform#9570)
* Implemented compute_reservation_vms_update, created test * Merged changes from main * Fixed naming * Fixed naming * Fixed test's order * Changed zone * Revert "Changed zone" This reverts commit cfb2d83. * Moved update VM test to ReservationIT * Changed zone * Revert "Changed zone" This reverts commit ae0c86d. * Changed zone * Checked GetReservation in the updateVm test * Revert "Checked GetReservation in the updateVm test" This reverts commit d7eb3ef. * Moved updateVm test to ReservationIT * Fixed whitespaces * Fixed tests * Fixed style * Changed zone
1 parent d295d37 commit 29784f1

File tree

3 files changed

+123
-32
lines changed

3 files changed

+123
-32
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2024 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.reservation;
18+
19+
// [START compute_reservation_vms_update]
20+
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.ReservationsClient;
23+
import com.google.cloud.compute.v1.ReservationsResizeRequest;
24+
import com.google.cloud.compute.v1.ResizeReservationRequest;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class UpdateVmsForReservation {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Cloud project you want to use.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// The zone where the reservation is located.
38+
String zone = "us-central1-a";
39+
// Name of the reservation to update.
40+
String reservationName = "YOUR_RESERVATION_NAME";
41+
// Number of instances to update in the reservation.
42+
int numberOfVms = 3;
43+
44+
updateVmsForReservation(projectId, zone, reservationName, numberOfVms);
45+
}
46+
47+
// Updates a reservation with new VM capacity.
48+
public static void updateVmsForReservation(
49+
String projectId, String zone, String reservationName, int numberOfVms)
50+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests.
53+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
54+
55+
ResizeReservationRequest resizeReservationRequest =
56+
ResizeReservationRequest.newBuilder()
57+
.setProject(projectId)
58+
.setZone(zone)
59+
.setReservation(reservationName)
60+
.setReservationsResizeRequestResource(ReservationsResizeRequest.newBuilder()
61+
.setSpecificSkuCount(numberOfVms)
62+
.build())
63+
.build();
64+
65+
Operation response = reservationsClient.resizeAsync(resizeReservationRequest)
66+
.get(3, TimeUnit.MINUTES);
67+
68+
if (response.hasError()) {
69+
System.out.println("Reservation update failed !!" + response);
70+
return;
71+
}
72+
System.out.println("Reservation updated successfully: " + response.getStatus());
73+
}
74+
}
75+
}
76+
// [END compute_reservation_vms_update]

compute/cloud-client/src/test/java/compute/reservation/CrudOperationsReservationIT.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,28 @@
3232
import java.util.concurrent.TimeUnit;
3333
import java.util.concurrent.TimeoutException;
3434
import org.junit.Assert;
35-
import org.junit.FixMethodOrder;
3635
import org.junit.jupiter.api.AfterAll;
3736
import org.junit.jupiter.api.Assertions;
3837
import org.junit.jupiter.api.BeforeAll;
38+
import org.junit.jupiter.api.MethodOrderer;
39+
import org.junit.jupiter.api.Order;
3940
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.api.TestMethodOrder;
4042
import org.junit.jupiter.api.Timeout;
4143
import org.junit.runner.RunWith;
4244
import org.junit.runners.JUnit4;
43-
import org.junit.runners.MethodSorters;
4445

4546
@RunWith(JUnit4.class)
46-
@Timeout(value = 25, unit = TimeUnit.MINUTES)
47-
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
47+
@Timeout(value = 6, unit = TimeUnit.MINUTES)
48+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
4849
public class CrudOperationsReservationIT {
4950

5051
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
51-
private static final String ZONE = "us-west1-a";
52-
private static String RESERVATION_NAME;
53-
private static final int NUMBER_OF_VMS = 3;
52+
private static final String ZONE = "us-central1-b";
5453
static String javaVersion = System.getProperty("java.version").substring(0, 2);
54+
private static final String RESERVATION_NAME = "test-reservation-" + javaVersion + "-"
55+
+ UUID.randomUUID().toString().substring(0, 8);
56+
private static final int NUMBER_OF_VMS = 3;
5557

5658
// Check if the required environment variables are set.
5759
public static void requireEnvVar(String envVarName) {
@@ -64,8 +66,6 @@ public static void setUp()
6466
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6567
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
6668
requireEnvVar("GOOGLE_CLOUD_PROJECT");
67-
RESERVATION_NAME = "test-reservation-" + javaVersion + "-"
68-
+ UUID.randomUUID().toString().substring(0, 8);
6969

7070
// Cleanup existing stale resources.
7171
Util.cleanUpExistingReservations("test-reservation-" + javaVersion, PROJECT_ID, ZONE);
@@ -84,7 +84,8 @@ public static void cleanup()
8484
}
8585

8686
@Test
87-
public void firstCreateReservationTest()
87+
@Order(1)
88+
public void testCreateReservation()
8889
throws IOException, ExecutionException, InterruptedException, TimeoutException {
8990
final PrintStream out = System.out;
9091
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
@@ -99,7 +100,8 @@ public void firstCreateReservationTest()
99100
}
100101

101102
@Test
102-
public void secondGetReservationTest()
103+
@Order(3)
104+
public void testGetReservation()
103105
throws IOException {
104106
Reservation reservation = GetReservation.getReservation(
105107
PROJECT_ID, RESERVATION_NAME, ZONE);
@@ -109,7 +111,8 @@ public void secondGetReservationTest()
109111
}
110112

111113
@Test
112-
public void thirdListReservationTest() throws IOException {
114+
@Order(4)
115+
public void testListReservation() throws IOException {
113116
List<Reservation> reservations =
114117
ListReservations.listReservations(PROJECT_ID, ZONE);
115118

compute/cloud-client/src/test/java/compute/reservation/ReservationIT.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,37 @@
3838
import org.junit.jupiter.api.AfterAll;
3939
import org.junit.jupiter.api.Assertions;
4040
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.MethodOrderer;
42+
import org.junit.jupiter.api.Order;
4143
import org.junit.jupiter.api.Test;
44+
import org.junit.jupiter.api.TestMethodOrder;
4245
import org.junit.jupiter.api.Timeout;
4346
import org.junit.runner.RunWith;
4447
import org.junit.runners.JUnit4;
4548

4649
@RunWith(JUnit4.class)
47-
@Timeout(value = 25, unit = TimeUnit.MINUTES)
50+
@Timeout(value = 6, unit = TimeUnit.MINUTES)
51+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
4852
public class ReservationIT {
4953

5054
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
51-
private static final String ZONE = "us-west1-a";
55+
private static final String ZONE = "asia-south1-a";
5256
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
53-
private static ReservationsClient reservationsClient;
54-
private static String RESERVATION_NAME_GLOBAL;
55-
private static String RESERVATION_NAME_REGIONAL;
56-
private static String GLOBAL_INSTANCE_TEMPLATE_URI;
57-
private static String REGIONAL_INSTANCE_TEMPLATE_URI;
5857
static String javaVersion = System.getProperty("java.version").substring(0, 2);
58+
private static ReservationsClient reservationsClient;
59+
private static final String RESERVATION_NAME_GLOBAL = "test-reservation-global-" + javaVersion
60+
+ "-" + UUID.randomUUID().toString().substring(0, 8);
61+
private static final String RESERVATION_NAME_REGIONAL = "test-reservation-regional-"
62+
+ javaVersion + "-" + UUID.randomUUID().toString().substring(0, 8);
5963
private static final String GLOBAL_INSTANCE_TEMPLATE_NAME =
6064
"test-global-inst-temp-" + javaVersion + "-" + UUID.randomUUID().toString().substring(0, 8);
61-
private static final String REGIONAL_INSTANCE_TEMPLATE_NAME =
62-
"test-regional-inst-temp-" + javaVersion + "-"
63-
+ UUID.randomUUID().toString().substring(0, 8);
65+
private static final String REGIONAL_INSTANCE_TEMPLATE_NAME = "test-regional-inst-temp-"
66+
+ javaVersion + "-" + UUID.randomUUID().toString().substring(0, 8);
67+
private static final String GLOBAL_INSTANCE_TEMPLATE_URI = String.format(
68+
"projects/%s/global/instanceTemplates/%s", PROJECT_ID, GLOBAL_INSTANCE_TEMPLATE_NAME);
69+
private static final String REGIONAL_INSTANCE_TEMPLATE_URI =
70+
String.format("projects/%s/regions/%s/instanceTemplates/%s",
71+
PROJECT_ID, REGION, REGIONAL_INSTANCE_TEMPLATE_NAME);
6472
private static final int NUMBER_OF_VMS = 3;
6573

6674
// Check if the required environment variables are set.
@@ -89,16 +97,6 @@ public static void setUp()
8997
// Initialize the client once for all tests
9098
reservationsClient = ReservationsClient.create();
9199

92-
RESERVATION_NAME_GLOBAL = "test-reservation-global-" + javaVersion + "-"
93-
+ UUID.randomUUID().toString().substring(0, 8);
94-
RESERVATION_NAME_REGIONAL = "test-reservation-regional-" + javaVersion + "-"
95-
+ UUID.randomUUID().toString().substring(0, 8);
96-
GLOBAL_INSTANCE_TEMPLATE_URI = String.format("projects/%s/global/instanceTemplates/%s",
97-
PROJECT_ID, GLOBAL_INSTANCE_TEMPLATE_NAME);
98-
REGIONAL_INSTANCE_TEMPLATE_URI =
99-
String.format("projects/%s/regions/%s/instanceTemplates/%s",
100-
PROJECT_ID, REGION, REGIONAL_INSTANCE_TEMPLATE_NAME);
101-
102100
// Create instance template with GLOBAL location.
103101
CreateInstanceTemplate.createInstanceTemplate(PROJECT_ID, GLOBAL_INSTANCE_TEMPLATE_NAME);
104102
assertThat(stdOut.toString())
@@ -152,6 +150,7 @@ public static void cleanup()
152150
}
153151

154152
@Test
153+
@Order(1)
155154
public void testCreateReservationWithGlobalInstanceTemplate()
156155
throws IOException, ExecutionException, InterruptedException, TimeoutException {
157156
CreateReservationForInstanceTemplate.createReservationForInstanceTemplate(
@@ -177,4 +176,17 @@ public void testCreateReservationWithRegionInstanceTemplate()
177176
Assert.assertTrue(reservation.getZone().contains(ZONE));
178177
Assert.assertEquals(RESERVATION_NAME_REGIONAL, reservation.getName());
179178
}
179+
180+
@Test
181+
@Order(2)
182+
public void testUpdateVmsForReservation()
183+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
184+
int newNumberOfVms = 5;
185+
UpdateVmsForReservation.updateVmsForReservation(
186+
PROJECT_ID, ZONE, RESERVATION_NAME_GLOBAL, newNumberOfVms);
187+
Reservation reservation = GetReservation.getReservation(
188+
PROJECT_ID, RESERVATION_NAME_GLOBAL, ZONE);
189+
190+
Assert.assertEquals(newNumberOfVms, reservation.getSpecificReservation().getCount());
191+
}
180192
}

0 commit comments

Comments
 (0)