Skip to content

Commit 790e3fa

Browse files
Merged changes from main, fixed tests
2 parents 66ae551 + 55d96d4 commit 790e3fa

File tree

23 files changed

+1004
-240
lines changed

23 files changed

+1004
-240
lines changed

appengine-java11/appengine-simple-jetty-main/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222
<maven.compiler.source>11</maven.compiler.source>
2323
<maven.compiler.target>11</maven.compiler.target>
24-
<jetty.version>9.4.54.v20240208</jetty.version>
24+
<jetty.version>9.4.56.v20240826</jetty.version>
2525
</properties>
2626

2727
<!-- [START gae_java11_server_dependencies] -->
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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_instance_not_consume_reservation]
20+
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.NO_RESERVATION;
21+
22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.cloud.compute.v1.AttachedDisk;
24+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
25+
import com.google.cloud.compute.v1.InsertInstanceRequest;
26+
import com.google.cloud.compute.v1.Instance;
27+
import com.google.cloud.compute.v1.InstancesClient;
28+
import com.google.cloud.compute.v1.NetworkInterface;
29+
import com.google.cloud.compute.v1.Operation;
30+
import com.google.cloud.compute.v1.ReservationAffinity;
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
36+
public class CreateInstanceWithoutConsumingReservation {
37+
public static void main(String[] args)
38+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
// Project ID or project number of the Cloud project you want to use.
41+
String projectId = "YOUR_PROJECT_ID";
42+
// Name of the zone you want to use.
43+
String zone = "us-central1-a";
44+
// Name of the VM instance you want to query.
45+
String instanceName = "YOUR_INSTANCE_NAME";
46+
// machineType: machine type of the VM being created.
47+
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
48+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
49+
String machineTypeName = "n1-standard-1";
50+
// sourceImage: path to the operating system image to mount.
51+
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
52+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
53+
// diskSizeGb: storage size of the boot disk to attach to the instance.
54+
long diskSizeGb = 10L;
55+
// networkName: network interface to associate with the instance.
56+
String networkName = "default";
57+
58+
createInstanceWithoutConsumingReservationAsync(projectId, zone, instanceName,
59+
machineTypeName, sourceImage, diskSizeGb, networkName);
60+
}
61+
62+
// Create a virtual machine that explicitly doesn't consume reservations
63+
public static Instance createInstanceWithoutConsumingReservationAsync(
64+
String project, String zone, String instanceName,
65+
String machineTypeName, String sourceImage, long diskSizeGb, String networkName)
66+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
67+
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
68+
69+
// Initialize client that will be used to send requests. This client only needs to be created
70+
// once, and can be reused for multiple requests.
71+
try (InstancesClient instancesClient = InstancesClient.create()) {
72+
AttachedDisk disk =
73+
AttachedDisk.newBuilder()
74+
.setBoot(true)
75+
.setAutoDelete(true)
76+
.setType(AttachedDisk.Type.PERSISTENT.toString())
77+
.setDeviceName("disk-1")
78+
.setInitializeParams(
79+
AttachedDiskInitializeParams.newBuilder()
80+
.setSourceImage(sourceImage)
81+
.setDiskSizeGb(diskSizeGb)
82+
.build())
83+
.build();
84+
85+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
86+
.setName(networkName)
87+
.build();
88+
89+
ReservationAffinity reservationAffinity =
90+
ReservationAffinity.newBuilder()
91+
.setConsumeReservationType(NO_RESERVATION.toString())
92+
.build();
93+
94+
Instance instanceResource =
95+
Instance.newBuilder()
96+
.setName(instanceName)
97+
.setMachineType(machineType)
98+
.addDisks(disk)
99+
.addNetworkInterfaces(networkInterface)
100+
.setReservationAffinity(reservationAffinity)
101+
.build();
102+
103+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
104+
.setProject(project)
105+
.setZone(zone)
106+
.setInstanceResource(instanceResource)
107+
.build();
108+
109+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
110+
insertInstanceRequest);
111+
112+
// Wait for the operation to complete.
113+
Operation response = operation.get(3, TimeUnit.MINUTES);
114+
115+
if (response.hasError()) {
116+
return null;
117+
}
118+
return instancesClient.get(project, zone, instanceName);
119+
}
120+
}
121+
}
122+
// [END compute_instance_not_consume_reservation]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package compute.reservation;
1818

19+
// [START compute_reservation_create]
1920
import com.google.cloud.compute.v1.AcceleratorConfig;
2021
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk;
2122
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
@@ -28,7 +29,6 @@
2829
import java.util.concurrent.TimeUnit;
2930
import java.util.concurrent.TimeoutException;
3031

31-
// [START compute_reservation_create]
3232
public class CreateReservation {
3333

3434
public static void main(String[] args)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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_from_vm]
20+
21+
import com.google.cloud.compute.v1.AcceleratorConfig;
22+
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk;
23+
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
24+
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
25+
import com.google.cloud.compute.v1.AttachedDisk;
26+
import com.google.cloud.compute.v1.InsertReservationRequest;
27+
import com.google.cloud.compute.v1.Instance;
28+
import com.google.cloud.compute.v1.InstancesClient;
29+
import com.google.cloud.compute.v1.Operation;
30+
import com.google.cloud.compute.v1.Reservation;
31+
import com.google.cloud.compute.v1.ReservationsClient;
32+
import java.io.IOException;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
38+
39+
public class CreateReservationFromVm {
40+
41+
public static void main(String[] args)
42+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
43+
// TODO(developer): Replace these variables before running the sample.
44+
// Project ID or project number of the Cloud project you want to use.
45+
String project = "YOUR_PROJECT_ID";
46+
// The zone of the VM. In this zone the reservation will be created.
47+
String zone = "us-central1-a";
48+
// The name of the reservation to create.
49+
String reservationName = "YOUR_RESERVATION_NAME";
50+
// The name of the VM to create the reservation from.
51+
String vmName = "YOUR_VM_NAME";
52+
53+
createComputeReservationFromVm(project, zone, reservationName, vmName);
54+
}
55+
56+
// Creates a compute reservation from an existing VM.
57+
public static void createComputeReservationFromVm(
58+
String project, String zone, String reservationName, String vmName)
59+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
60+
// Initialize client that will be used to send requests. This client only needs to be created
61+
// once, and can be reused for multiple requests.
62+
try (InstancesClient instancesClient = InstancesClient.create();
63+
ReservationsClient reservationsClient = ReservationsClient.create()) {
64+
Instance existingVm = instancesClient.get(project, zone, vmName);
65+
66+
// Extract properties from the existing VM
67+
List<AcceleratorConfig> guestAccelerators = new ArrayList<>();
68+
if (!existingVm.getGuestAcceleratorsList().isEmpty()) {
69+
for (AcceleratorConfig accelatorConfig : existingVm.getGuestAcceleratorsList()) {
70+
guestAccelerators.add(
71+
AcceleratorConfig.newBuilder()
72+
.setAcceleratorCount(accelatorConfig.getAcceleratorCount())
73+
.setAcceleratorType(accelatorConfig.getAcceleratorType()
74+
.substring(accelatorConfig.getAcceleratorType().lastIndexOf('/') + 1))
75+
.build());
76+
}
77+
}
78+
79+
List<AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk> localSsds =
80+
new ArrayList<>();
81+
if (!existingVm.getDisksList().isEmpty()) {
82+
for (AttachedDisk disk : existingVm.getDisksList()) {
83+
if (disk.getDiskSizeGb() >= 375) {
84+
localSsds.add(
85+
AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk.newBuilder()
86+
.setDiskSizeGb(disk.getDiskSizeGb())
87+
.setInterface(disk.getInterface())
88+
.build());
89+
}
90+
}
91+
}
92+
93+
AllocationSpecificSKUAllocationReservedInstanceProperties instanceProperties =
94+
AllocationSpecificSKUAllocationReservedInstanceProperties.newBuilder()
95+
.setMachineType(
96+
existingVm.getMachineType()
97+
.substring(existingVm.getMachineType().lastIndexOf('/') + 1))
98+
.setMinCpuPlatform(existingVm.getMinCpuPlatform())
99+
.addAllLocalSsds(localSsds)
100+
.addAllGuestAccelerators(guestAccelerators)
101+
.build();
102+
103+
Reservation reservation =
104+
Reservation.newBuilder()
105+
.setName(reservationName)
106+
.setSpecificReservation(
107+
AllocationSpecificSKUReservation.newBuilder()
108+
.setCount(3)
109+
.setInstanceProperties(instanceProperties)
110+
.build())
111+
.setSpecificReservationRequired(true)
112+
.build();
113+
114+
InsertReservationRequest insertReservationRequest =
115+
InsertReservationRequest.newBuilder()
116+
.setProject(project)
117+
.setZone(zone)
118+
.setReservationResource(reservation)
119+
.build();
120+
121+
Operation response = reservationsClient
122+
.insertAsync(insertReservationRequest).get(3, TimeUnit.MINUTES);
123+
124+
if (response.hasError()) {
125+
System.out.println("Reservation creation failed ! ! " + response);
126+
return;
127+
}
128+
System.out.println("Operation completed successfully.");
129+
}
130+
}
131+
}
132+
// [END compute_reservation_create_from_vm]
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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_template_not_consume_reservation]
20+
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.NO_RESERVATION;
21+
22+
import com.google.cloud.compute.v1.AccessConfig;
23+
import com.google.cloud.compute.v1.AttachedDisk;
24+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
25+
import com.google.cloud.compute.v1.InsertInstanceTemplateRequest;
26+
import com.google.cloud.compute.v1.InstanceProperties;
27+
import com.google.cloud.compute.v1.InstanceTemplate;
28+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
29+
import com.google.cloud.compute.v1.NetworkInterface;
30+
import com.google.cloud.compute.v1.Operation;
31+
import com.google.cloud.compute.v1.ReservationAffinity;
32+
import java.io.IOException;
33+
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.TimeoutException;
36+
37+
public class CreateTemplateWithoutConsumingReservation {
38+
public static void main(String[] args)
39+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
// Project ID or project number of the Cloud project you want to use.
42+
String projectId = "YOUR_PROJECT_ID";
43+
// Name of the template you want to query.
44+
String templateName = "YOUR_INSTANCE_TEMPLATE_NAME";
45+
String machineType = "e2-standard-4";
46+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
47+
48+
createTemplateWithoutConsumingReservationAsync(
49+
projectId, templateName, machineType, sourceImage);
50+
}
51+
52+
53+
// Create a template that explicitly doesn't consume any reservations.
54+
public static InstanceTemplate createTemplateWithoutConsumingReservationAsync(
55+
String projectId, String templateName, String machineType, String sourceImage)
56+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
57+
// Initialize client that will be used to send requests. This client only needs to be created
58+
// once, and can be reused for multiple requests.
59+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
60+
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
61+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
62+
.setSourceImage(sourceImage)
63+
.setDiskType("pd-balanced")
64+
.setDiskSizeGb(250)
65+
.build())
66+
.setAutoDelete(true)
67+
.setBoot(true)
68+
.build();
69+
70+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
71+
.setName("global/networks/default")
72+
.addAccessConfigs(AccessConfig.newBuilder()
73+
.setName("External NAT")
74+
.setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString())
75+
.setNetworkTier(AccessConfig.NetworkTier.PREMIUM.toString())
76+
.build())
77+
.build();
78+
79+
ReservationAffinity reservationAffinity =
80+
ReservationAffinity.newBuilder()
81+
.setConsumeReservationType(NO_RESERVATION.toString())
82+
.build();
83+
84+
InstanceProperties instanceProperties = InstanceProperties.newBuilder()
85+
.addDisks(attachedDisk)
86+
.setMachineType(machineType)
87+
.setReservationAffinity(reservationAffinity)
88+
.addNetworkInterfaces(networkInterface)
89+
.build();
90+
91+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
92+
.newBuilder()
93+
.setProject(projectId)
94+
.setInstanceTemplateResource(InstanceTemplate.newBuilder()
95+
.setName(templateName)
96+
.setProperties(instanceProperties)
97+
.build())
98+
.build();
99+
100+
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
101+
.get(3, TimeUnit.MINUTES);
102+
103+
if (response.hasError()) {
104+
return null;
105+
}
106+
return instanceTemplatesClient.get(projectId, templateName);
107+
}
108+
}
109+
}
110+
// [END compute_template_not_consume_reservation]

0 commit comments

Comments
 (0)