8000 feat(compute): add compute_reservation_delete (#9479) · suztomo/java-docs-samples@7212265 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 7212265

Browse files
feat(compute): add compute_reservation_delete (GoogleCloudPlatform#9479)
* Implemented compute_reservation_create and compute_reservation_delete samples, added test * Fixed class naming, divided tests * Fixed comment,deleted Create Reservation.java to move in the separated PR, reverted changes in the HypedisksIT * Fixed whitespaces * Fixed whitespaces * Fixed whitespaces * Fixed whitespaces * Fixed comment * Fixed brace * Fixed spaces * Fixed spaces * Fixed spaces * Fixed spaces * Changed zone * Created test * Fixed test naming * Fixed tests * Revert "Fixed tests" This reverts commit 5019ef4. * Changed zones in HyperdisksIT tests * Changed zones in HyperdisksIT tests * Changed zones in HyperdisksIT tests * Changed zones in HyperdisksIT tests * Increased Timeout * Increased Timeout
1 parent aa4a590 commit 7212265

File tree

4 files changed

+176
-2
lines changed

4 files changed

+176
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_delete]
20+
21+
import com.google.cloud.compute.v1.DeleteReservationRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.ReservationsClient;
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 DeleteReservation {
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 ID or project number of the Cloud project you want to use.
35+
String projectId = "YOUR_PROJECT_ID";
36+
// Name of the reservation you want to delete.
37+
String reservationName = "YOUR_RESERVATION_NAME";
38+
// Name of the zone.
39+
String zone = "us-central1-a";
40+
41+
deleteReservation(projectId, zone, reservationName);
42+
}
43+
44+
// Delete a reservation from the project.
45+
public static void deleteReservation(String projectId, String zone, String reservationName)
46+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
47+
/* Initialize client that will be used to send requests. This client only needs to be created
48+
once, and can be reused for multiple requests. */
49+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
50+
51+
DeleteReservationRequest deleteReservationRequest = DeleteReservationRequest.newBuilder()
52+
.setProject(projectId)
53+
.setZone(zone)
54+
.setReservation(reservationName)
55+
.build();
56+
57+
Operation response = reservationsClient.deleteAsync(
58+
deleteReservationRequest).get(5, TimeUnit.MINUTES);
59+
60+
if (response.getStatus() == Operation.Status.DONE) {
61+
System.out.println("Deleted reservation: " + reservationName);
62+
}
63+
}
64+
}
65+
}
66+
// [END compute_reservation_delete]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void testWaitForOperation()
209209
OperationFuture<Operation, Operation> operation = instancesClient.deleteAsync(PROJECT_ID, ZONE,
210210
MACHINE_NAME_WAIT_FOR_OP);
211211
// Wait for the operation to complete.
212-
operation.get(3, TimeUnit.MINUTES);
212+
operation.get(5, TimeUnit.MINUTES);
213213
assertThat(stdOut.toString().contains("Operation Status: DONE"));
214214
}
215215

compute/cloud-client/src/test/java/compute/disks/HyperdisksIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,4 @@ public void stage2_CreateHyperdiskStoragePoolTest()
136136
Assert.assertTrue(disk.getType().contains("hyperdisk-balanced"));
137137
Assert.assertTrue(disk.getZone().contains(ZONE_2));
138138
}
139-
}
139+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static compute.Util.getZone;
21+
22+
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
23+
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
24+
import com.google.cloud.compute.v1.InsertReservationRequest;
25+
import com.google.cloud.compute.v1.Operation;
26+
import com.google.cloud.compute.v1.Reservation;
27+
import com.google.cloud.compute.v1.ReservationsClient;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.PrintStream;
31+
import java.util.UUID;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
import org.junit.jupiter.api.AfterEach;
36+
import org.junit.jupiter.api.BeforeAll;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Test;
39+
40+
public class ReservationIT {
41+
42+
private static String PROJECT_ID;
43+
private static String ZONE;
44+
private static String RESERVATION_NAME;
45+
46+
private ByteArrayOutputStream stdOut;
47+
48+
@BeforeAll
49+
public static void setUp()
50+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
51+
PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
52+
ZONE = getZone();
53+
RESERVATION_NAME = "test-reservation-" + UUID.randomUUID();
54+
55+
// Create the reservation.
56+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
57+
58+
Reservation reservation = Reservation.newBuilder()
59+
.setName(RESERVATION_NAME)
60+
.setSpecificReservation(
61+
AllocationSpecificSKUReservation.newBuilder()
62+
.setCount(1)
63+
.setInstanceProperties(
64+
AllocationSpecificSKUAllocationReservedInstanceProperties.newBuilder()
65+
.setMachineType("n1-standard-1")
66+
.build())
67+
.build())
68+
.build();
69+
70+
InsertReservationRequest reservationRequest = InsertReservationRequest.newBuilder()
71+
.setProject(PROJECT_ID)
72+
.setZone(ZONE)
73+
.setReservationResource(reservation)
74+
.build();
75+
76+
Operation response = reservationsClient.insertAsync(reservationRequest)
77+
.get(3, TimeUnit.MINUTES);
78+
79+
if (response.getStatus() == Operation.Status.DONE) {
80+
System.out.println("Reservation created.");
81+
} else {
82+
System.out.println("Reservation creation failed!");
83+
}
84+
85+
assertThat(reservation.getName()).isEqualTo(RESERVATION_NAME);
86+
}
87+
}
88+
89+
@BeforeEach
90+
public void beforeEach() {
91+
stdOut = new ByteArrayOutputStream();
92+
System.setOut(new PrintStream(stdOut));
93+
}
94+
95+
@AfterEach
96+
public void afterEach() {
97+
stdOut = null;
98+
System.setOut(null);
99+
}
100+
101+
@Test
102+
public void testDeleteReservation()
103+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
104+
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME);
105+
106+
assertThat(stdOut.toString()).contains("Deleted reservation: " + RESERVATION_NAME);
107+
}
108+
}

0 commit comments

Comments
 (0)
0