8000 feat(compute): add compute reservation samples (get, list and create … · suztomo/java-docs-samples@ba36764 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba36764

Browse files
feat(compute): add compute reservation samples (get, list and create with template) (GoogleCloudPlatform#9507)
* Implemented compute_reservation_get sample, created test * Fixed tests * Added assertion to delete method * Revert "Added assertion to delete method" This reverts commit f7bf476. * Fixed Uri * Changed zone * Added comment * Disabled Hyperdisk tests * Changed zone * Added getZone method * Fixed comments * Fixed comments, deleted redundant code * Fixed test, initialized client
1 parent df19da6 commit ba36764

File tree

6 files changed

+440
-27
lines changed

6 files changed

+440
-27
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_create_template]
20+
21+
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Reservation;
24+
import com.google.cloud.compute.v1.ReservationsClient;
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 CreateReservationForInstanceTemplate {
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+
// Name of the zone in which you want to create the reservation.
38+
String zone = "us-central1-a";
39+
// Name of the reservation you want to create.
40+
String reservationName = "YOUR_RESERVATION_NAME";
41+
// The number of virtual machines you want to create.
42+
int numberOfVms = 3;
43+
// The URI of the instance template with GLOBAL location
44+
// to be used for creating the reservation.
45+
String instanceTemplateUri =
46+
"projects/YOUR_PROJECT_ID/global/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME";
47+
48+
// The URI of the instance template with REGIONAL location
49+
// to be used for creating the reservation. For us-central1 region in this case.
50+
// String instanceTemplateUri =
51+
// "projects/YOUR_PROJECT_ID/regions/us-central1/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME"
52+
53+
createReservationForInstanceTemplate(
54+
projectId, reservationName, instanceTemplateUri, numberOfVms, zone);
55+
}
56+
57+
// Creates a reservation in a project for the instance template.
58+
public static void createReservationForInstanceTemplate(
59+
String projectId, String reservationName, String instanceTemplateUri,
60+
int numberOfVms, String zone)
61+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
62+
// Initialize client that will be used to send requests. This client only needs to be created
63+
// once, and can be reused for multiple requests.
64+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
65+
66+
// Create the reservation.
67+
Reservation reservation =
68+
Reservation.newBuilder()
69+
.setName(reservationName)
70+
.setZone(zone)
71+
.setSpecificReservation(
72+
AllocationSpecificSKUReservation.newBuilder()
73+
// Set the number of instances
74+
.setCount(numberOfVms)
75+
// Set the instance template to be used for creating the reservation.
76+
.setSourceInstanceTemplate(instanceTemplateUri)
77+
.build())
78+
.build();
79+
80+
// Wait for the create reservation operation to complete.
81+
Operation response =
82+
reservationsClient.insertAsync(projectId, zone, reservation).get(3, TimeUnit.MINUTES);
83+
84+
if (response.hasError()) {
85+
System.out.println("Reservation creation failed!" + response);
86+
return;
87+
}
88+
System.out.println("Reservation created. Operation Status: " + response.getStatus());
89+
}
90+
}
91+
}
92+
// [END compute_reservation_create_template]

compute/cloud-client/src/main/java/compute/reservation/DeleteReservation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void deleteReservation(String projectId, String zone, String reser
5555
.build();
5656

5757
Operation response = reservationsClient.deleteAsync(
58-
deleteReservationRequest).get(7, TimeUnit.MINUTES);
58+
deleteReservationRequest).get(5, TimeUnit.MINUTES);
5959

6060
if (response.getStatus() == Operation.Status.DONE) {
6161
System.out.println("Deleted reservation: " + reservationName);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_get]
20+
21+
import com.google.cloud.compute.v1.Reservation;
22+
import com.google.cloud.compute.v1.ReservationsClient;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.TimeoutException;
26+
27+
public class GetReservation {
28+
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Cloud project you want to use.
33+
String projectId = "YOUR_PROJECT_ID";
34+
// Name of the zone in which you want to create the reservation.
35+
String zone = "us-central1-a";
36+
// Name of the reservation you want to create.
37+
String reservationName = "test-reservation-name";
38+
39+
getReservation(projectId, reservationName, zone);
40+
}
41+
42+
// Retrieve a reservation with the given name in the given zone.
43+
public static Reservation getReservation(
44+
String projectId, String reservationName, String zone)
45+
throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
49+
50+
// Get the reservation.
51+
Reservation reservation = reservationsClient.get(projectId, zone, reservationName);
52+
53+
System.out.println("Reservation: " + reservation.toString());
54+
return reservation;
55+
}
56+
}
57+
}
58+
// [END compute_reservation_get]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_list]
20+
21+
import com.google.cloud.compute.v1.Reservation;
22+
import com.google.cloud.compute.v1.ReservationsClient;
23+
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class ListReservations {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// Project ID or project number of the Cloud project you want to use.
32+
String project = "YOUR_PROJECT_ID";
33+
// Zone in which reservations are located.
34+
String zone = "us-central1-a";
35+
36+
listReservations(project, zone);
37+
}
38+
39+
// List all reservations in the given project and zone.
40+
public static List<Reservation> listReservations(String project, String zone) throws IOException {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
List<Reservation> listOfReservations = new ArrayList<>();
44+
45+
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
46+
for (Reservation reservation : reservationsClient.list(project, zone).iterateAll()) {
47+
listOfReservations.add(reservation);
48+
System.out.println("Reservation: " + reservation.getName());
49+
}
50+
}
51+
return listOfReservations;
52+
}
53+
}
54+
// [END compute_reservation_list]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void cleanup()
7373
// Delete all disks created for testing.
7474
DeleteDisk.deleteDisk(PROJECT_ID, ZONE_1, HYPERDISK_NAME);
7575
// DeleteDisk.deleteDisk(PROJECT_ID, ZONE_2, HYPERDISK_IN_POOL_NAME);
76-
76+
//
7777
// try (StoragePoolsClient client = StoragePoolsClient.create()) {
7878
// client.deleteAsync(PROJECT_ID, ZONE_2, STORAGE_POOL_NAME);
7979
// }

0 commit comments

Comments
 (0)
0