Skip to content

Commit 4a66765

Browse files
Added compute_template_not_consume_reservation sample, fixed tests
1 parent 41e2bd7 commit 4a66765

File tree

2 files changed

+154
-9
lines changed

2 files changed

+154
-9
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
21+
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.NO_RESERVATION;
22+
23+
import com.google.cloud.compute.v1.AccessConfig;
24+
import com.google.cloud.compute.v1.AttachedDisk;
25+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
26+
import com.google.cloud.compute.v1.InsertInstanceTemplateRequest;
27+
import com.google.cloud.compute.v1.InstanceProperties;
28+
import com.google.cloud.compute.v1.InstanceTemplate;
29+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
30+
import com.google.cloud.compute.v1.NetworkInterface;
31+
import com.google.cloud.compute.v1.Operation;
32+
import com.google.cloud.compute.v1.ReservationAffinity;
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.TimeoutException;
37+
38+
public class CreateTemplateNotConsumeReservation {
39+
public static void main(String[] args)
40+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
// Project ID or project number of the Cloud project you want to use.
43+
String projectId = "YOUR_PROJECT_ID";
44+
// Name of the template you want to query.
45+
String templateName = "YOUR_INSTANCE_TEMPLATE_NAME";
46+
// Machine type of the template.
47+
String machineType = "n2-standard-32";
48+
49+
createTemplateNotConsumeReservation(projectId, templateName, machineType);
50+
}
51+
52+
// Create a template that explicitly doesn't consume any reservations.
53+
public static void createTemplateNotConsumeReservation(
54+
String projectId, String templateName, String machineType)
55+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
56+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
57+
// Below are sample values that can be replaced.
58+
// sourceImage: path to the operating system image to mount.
59+
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
60+
// Network interface to associate with the instance.
61+
String sourceImage = String
62+
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
63+
64+
// The template describes the size and source image of the boot disk
65+
// to attach to the instance.
66+
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
67+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
68+
.setSourceImage(sourceImage)
69+
.setDiskType("pd-balanced")
70+
.setDiskSizeGb(250).build())
71+
.setAutoDelete(true)
72+
.setBoot(true).build();
73+
74+
// The template connects the instance to the `default` network,
75+
// without specifying a subnetwork.
76+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
77+
.setName("global/networks/default")
78+
// The template lets the instance use an external IP address.
79+
.addAccessConfigs(AccessConfig.newBuilder()
80+
.setName("External NAT")
81+
.setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString())
82+
.setNetworkTier(AccessConfig.NetworkTier.PREMIUM.toString()).build()).build();
83+
84+
// Set reservation affinity to "none"
85+
ReservationAffinity reservationAffinity =
86+
ReservationAffinity.newBuilder()
87+
.setConsumeReservationType(NO_RESERVATION.toString())
88+
.build();
89+
90+
InstanceProperties instanceProperties = InstanceProperties.newBuilder()
91+
.addDisks(attachedDisk)
92+
.setMachineType(machineType)
93+
.setReservationAffinity(reservationAffinity)
94+
.addNetworkInterfaces(networkInterface).build();
95+
96+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
97+
.newBuilder()
98+
.setProject(projectId)
99+
.setInstanceTemplateResource(InstanceTemplate.newBuilder()
100+
.setName(templateName)
101+
.setProperties(instanceProperties).build()).build();
102+
103+
// Create the Instance Template.
104+
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
105+
.get(3, TimeUnit.MINUTES);
106+
107+
if (response.hasError()) {
108+
System.out.println("Instance Template creation failed ! ! " + response);
109+
return;
110+
}
111+
System.out
112+
.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
113+
}
114+
}
115+
}
116+
// [END compute_template_not_consume_reservation]

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,40 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121

2222
import com.google.cloud.compute.v1.Instance;
23+
import com.google.cloud.compute.v1.InstanceTemplate;
24+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
2325
import com.google.cloud.compute.v1.InstancesClient;
2426
import compute.DeleteInstance;
27+
import compute.DeleteInstanceTemplate;
2528
import compute.Util;
2629
import java.io.IOException;
2730
import java.util.UUID;
2831
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
2933
import java.util.concurrent.TimeoutException;
3034
import org.junit.jupiter.api.AfterAll;
3135
import org.junit.jupiter.api.Assertions;
3236
import org.junit.jupiter.api.BeforeAll;
3337
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.Timeout;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.JUnit4;
3441

42+
@RunWith(JUnit4.class)
43+
@Timeout(value = 25, unit = TimeUnit.MINUTES)
3544
public class ConsumeReservationIT {
3645

3746
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3847
private static final String ZONE = "europe-southwest1-a";
3948
private static InstancesClient instancesClient;
49+
private static InstanceTemplatesClient instanceTemplatesClient;
4050
static String javaVersion = System.getProperty("java.version").substring(0, 2);
4151
private static final String INSTANCE_NOT_CONSUME_RESERVATION_NAME =
4252
"test-instance-not-consume-" + javaVersion + "-"
4353
+ UUID.randomUUID().toString().substring(0, 8);
54+
private static final String TEMPLATE_NOT_CONSUME_RESERVATION_NAME =
55+
"test-template-not-consume-" + javaVersion + "-"
56+
+ UUID.randomUUID().toString().substring(0, 8);
4457
private static final String MACHINE_TYPE = "n2-standard-32";
4558

4659
// Check if the required environment variables are set.
@@ -55,35 +68,51 @@ public static void setUp()
5568
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
5669
requireEnvVar("GOOGLE_CLOUD_PROJECT");
5770

58-
// Initialize the clients once for all tests
71+
// Initialize clients once for all tests
5972
instancesClient = InstancesClient.create();
73+
instanceTemplatesClient = InstanceTemplatesClient.create();
6074

6175
// Cleanup existing stale resources.
6276
Util.cleanUpExistingInstances("test-instance-not-consume-" + javaVersion, PROJECT_ID, ZONE);
77+
Util.cleanUpExistingInstanceTemplates("test-template-not-consume-" + javaVersion, PROJECT_ID);
78+
79+
// Create resources for testing.
80+
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
81+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
82+
CreateTemplateNotConsumeReservation.createTemplateNotConsumeReservation(
83+
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
6384
}
6485

6586
@AfterAll
6687
public static void cleanup()
6788
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6889
// Delete the instance created for testing.
6990
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
91+
DeleteInstanceTemplate.deleteInstanceTemplate(
92+
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
7093

71-
// Close the client after all tests
94+
// Close clients after all tests
7295
instancesClient.close();
96+
instanceTemplatesClient.close();
7397
}
7498

7599
@Test
76-
public void testCreateInstanceNotConsumeReservation()
77-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
78-
79-
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
80-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
81-
82-
// Verify that the instance was created with the correct consumeReservationType
100+
public void testCreateInstanceNotConsumeReservation() {
83101
Instance instance = instancesClient.get(
84102
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
85103

104+
// Verify that the instance was created with the correct consumeReservationType
86105
Assertions.assertEquals(NO_RESERVATION.toString(),
87106
instance.getReservationAffinity().getConsumeReservationType());
88107
}
108+
109+
@Test
110+
public void testCreateTemplateNotConsumeReservation() {
111+
InstanceTemplate template = instanceTemplatesClient.get(
112+
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
113+
114+
// Verify that the instance was created with the correct reservation and consumeReservationType
115+
Assertions.assertEquals(NO_RESERVATION.toString(),
116+
template.getPropertiesOrBuilder().getReservationAffinity().getConsumeReservationType());
117+
}
89118
}

0 commit comments

Comments
 (0)