Skip to content

Commit c617f2a

Browse files
feat(compute): add compute regional template create (#9526)
* Implemented compute_regional_template_create sample, created delete and get classes, fixed test * Added compute_regional_template_get and compute_regional_template_delete tags * Added comments * Fixed naming and methods parameters * Fixed naming * Fixed test * Changed zone * Changed zone * Changed zone * Changed region * Changed to getZone() * Added clean method * Changed zone to us-central1-a * Deleted clean up methods * Created new branch for testing purposes * Fixed comments, deleted assertions * Added cleanUpExistingInstances for RegionalInstance * Added cleanUpExistingInstances in ReservationIT class * Fixed imports order * Changed zone * Changed zone * Changed timeout * Fixed timeout * Added cleanup method for reservation * Fixed zone for tests classes * added timeout to DeleteProtectionIT * fixed test * fixed cleanup method * Increased timeout * Increased timeout, changed zone * Changed timeout * Changed zone * Changed timeout * Added timeout * Increased timeout * changed name * added timeout * deleted test for create Reservtion * Revert "deleted test for create Reservtion" This reverts commit 761df70. * deleted tests for create Reservation * Revert "deleted tests for create Reservation" This reverts commit df076ba. * added cleanup methods * changed zone * changed zone * disabled test * Splitted test * Fixed test * Changed deletion time * Fixed cleanup methods * Fixed cleanup methods * Fixed cleanup methods * Changed default zones * Changed zones * fixed tests * Fixed tests * Fixed tests * Fixed tests * Fixed tests * Fixed tests
1 parent 09700ae commit c617f2a

File tree

9 files changed

+424
-195
lines changed

9 files changed

+424
-195
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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;
18+
19+
// [START compute_regional_template_create]
20+
21+
import com.google.cloud.compute.v1.AttachedDisk;
22+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
23+
import com.google.cloud.compute.v1.InsertRegionInstanceTemplateRequest;
24+
import com.google.cloud.compute.v1.InstanceProperties;
25+
import com.google.cloud.compute.v1.InstanceTemplate;
26+
import com.google.cloud.compute.v1.NetworkInterface;
27+
import com.google.cloud.compute.v1.Operation;
28+
import com.google.cloud.compute.v1.RegionInstanceTemplatesClient;
29+
import java.io.IOException;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
33+
34+
public class CreateRegionalInstanceTemplate {
35+
36+
public static void main(String[] args)
37+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
// Project ID or project number of the Cloud project you want to use.
40+
String projectId = "YOUR_PROJECT_ID";
41+
// Name of the instance you want to create.
42+
String instanceName = "YOUR_INSTANCE_NAME";
43+
// Name of the region.
44+
String region = "us-central1";
45+
46+
createRegionalInstanceTemplate(projectId, region, instanceName);
47+
}
48+
49+
// Create a new regional instance template with the provided name and a specific
50+
// instance configuration.
51+
public static void createRegionalInstanceTemplate(
52+
String projectId, String region, String templateName)
53+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests.
56+
try (RegionInstanceTemplatesClient templatesClientRegion =
57+
RegionInstanceTemplatesClient.create()) {
58+
59+
String machineType = "n1-standard-1"; // Example machine type
60+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11"; // Example image
61+
62+
// Define the boot disk for the instance template
63+
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
64+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
65+
.setSourceImage(sourceImage)
66+
.setDiskType("pd-balanced") // Example disk type
67+
.setDiskSizeGb(100L) // Example disk size
68+
.build())
69+
.setAutoDelete(true)
70+
.setBoot(true)
71+
.build();
72+
73+
// Define the network interface for the instance template
74+
// Note: The subnetwork must be in the same region as the instance template.
75+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
76+
.setName("my-network-test")
77+
.setSubnetwork(String.format("projects/%s/regions/%s/subnetworks/default",
78+
projectId, region))
79+
.build();
80+
81+
// Define the instance properties for the template
82+
InstanceProperties instanceProperties = InstanceProperties.newBuilder()
83+
.addDisks(attachedDisk)
84+
.setMachineType(machineType)
85+
.addNetworkInterfaces(networkInterface)
86+
.build();
87+
88+
// Build the instance template object
89+
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder()
90+
.setName(templateName)
91+
.setProperties(instanceProperties)
92+
.build();
93+
94+
// Create the request to insert the instance template
95+
InsertRegionInstanceTemplateRequest insertInstanceTemplateRequest =
96+
InsertRegionInstanceTemplateRequest
97+
.newBuilder()
98+
.setProject(projectId)
99+
.setRegion(region)
100+
.setInstanceTemplateResource(instanceTemplate)
101+
.build();
102+
103+
// Send the request and wait for the operation to complete
104+
Operation response = templatesClientRegion.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.printf("Instance Template Operation Status: %s%n", response.getStatus());
112+
}
113+
}
114+
}
115+
// [END compute_regional_template_create]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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;
18+
19+
// [START compute_regional_template_delete]
20+
21+
import com.google.cloud.compute.v1.DeleteRegionInstanceTemplateRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.RegionInstanceTemplatesClient;
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 DeleteRegionalInstanceTemplate {
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Cloud project you want to use.
34+
String projectId = "YOUR_PROJECT_ID";
35+
// Name of the instance you want to delete.
36+
String instanceName = "YOUR_INSTANCE_NAME";
37+
// Name of the region.
38+
String region = "us-central1";
39+
40+
deleteRegionalInstanceTemplate(projectId, region, instanceName);
41+
}
42+
43+
// Delete a regional instance template.
44+
public static void deleteRegionalInstanceTemplate(
45+
String projectId, String region, String templateName)
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 (RegionInstanceTemplatesClient regionInstanceTemplatesClient =
50+
RegionInstanceTemplatesClient.create()) {
51+
52+
DeleteRegionInstanceTemplateRequest deleteInstanceTemplateRequest =
53+
DeleteRegionInstanceTemplateRequest
54+
.newBuilder()
55+
.setProject(projectId)
56+
.setRegion(region)
57+
.setInstanceTemplate(templateName)
58+
.build();
59+
60+
Operation response = regionInstanceTemplatesClient.deleteAsync(
61+
deleteInstanceTemplateRequest).get(3, TimeUnit.MINUTES);
62+
63+
if (response.hasError()) {
64+
System.out.println("Instance template deletion failed ! ! " + response);
65+
return;
66+
}
67+
System.out.printf("Instance template deletion operation status for %s: %s ", templateName,
68+
response.getStatus());
69+
}
70+
}
71+
}
72+
// [END compute_regional_template_delete]
73+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;
18+
19+
// [START compute_regional_template_get]
20+
21+
import com.google.cloud.compute.v1.InstanceTemplate;
22+
import com.google.cloud.compute.v1.RegionInstanceTemplatesClient;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.TimeoutException;
26+
27+
public class GetRegionalInstanceTemplate {
28+
public static void main(String[] args)
29+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
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 projectId = "YOUR_PROJECT_ID";
33+
// Name of the instance you want to get.
34+
String instanceName = "YOUR_INSTANCE_NAME";
35+
// Name of the region.
36+
String region = "us-central1";
37+
38+
getRegionalInstanceTemplate(projectId, region, instanceName);
39+
}
40+
41+
// Get a regional instance template.
42+
public static InstanceTemplate getRegionalInstanceTemplate(
43+
String project, String region, String instanceName) throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (RegionInstanceTemplatesClient instancesClient = RegionInstanceTemplatesClient.create()) {
47+
return instancesClient.get(project, region, instanceName);
48+
}
49+
}
50+
}
51+
// [END compute_regional_template_get]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void main(String[] args)
3939
// Name of the zone in which you want to create the disk.
4040
String zone = "us-central1-a";
4141
// Name of the reservation you want to create.
42-
String reservationName = "test-reservation-name";
42+
String reservationName = "YOUR_RESERVATION_NAME";
4343
// Number of instances in the reservation.
4444
int numberOfVms = 3;
4545

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compute;
1818

1919
import static com.google.common.truth.Truth.assertWithMessage;
20-
import static compute.Util.getZone;
2120

2221
import com.google.api.gax.longrunning.OperationFuture;
2322
import com.google.cloud.compute.v1.Disk;

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.compute.v1.AttachedDisk;
2525
import com.google.cloud.compute.v1.Instance;
2626
import com.google.cloud.compute.v1.Instance.Status;
27+
import com.google.cloud.compute.v1.InstanceTemplate;
2728
import com.google.cloud.compute.v1.InstancesClient;
2829
import com.google.cloud.compute.v1.Operation;
2930
import com.google.cloud.compute.v1.UsageExportLocation;
@@ -59,6 +60,7 @@ public class SnippetsIT {
5960
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
6061
private static final String TEST_IMAGE_PROJECT_NAME = "JAVA_DOCS_COMPUTE_TEST_IMAGE_PROJECT";
6162
private static final String ZONE = "asia-south1-a";
63+
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
6264
private static String MACHINE_NAME;
6365
private static String MACHINE_NAME_LIST_INSTANCE;
6466
private static String MACHINE_NAME_WAIT_FOR_OP;
@@ -67,6 +69,7 @@ public class SnippetsIT {
6769
private static String BUCKET_NAME;
6870
private static String IMAGE_PROJECT_NAME;
6971
private static String RAW_KEY;
72+
private static String REGIONAL_LOCATION_NAME;
7073

7174
private ByteArrayOutputStream stdOut;
7275

@@ -90,21 +93,24 @@ public static void setUp()
9093
MACHINE_NAME_WAIT_FOR_OP = "my-new-test-instance" + UUID.randomUUID();
9194
MACHINE_NAME_ENCRYPTED = "encrypted-test-instance" + UUID.randomUUID();
9295
MACHINE_NAME_WITH_SSD = "test-instance-with-ssd" + UUID.randomUUID();
96+
REGIONAL_LOCATION_NAME = "test-inst-temp-regional-" + UUID.randomUUID();
9397
BUCKET_NAME = "my-new-test-bucket" + UUID.randomUUID();
9498
IMAGE_PROJECT_NAME = getEnvVar(TEST_IMAGE_PROJECT_NAME, "windows-sql-cloud");
9599
RAW_KEY = Util.getBase64EncodedKey();
96100

97101
// Cleanup existing stale resources.
98102
Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE);
99103
Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE);
100-
Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE);
101104
Util.cleanUpExistingInstances("test-instance-with-ssd", PROJECT_ID, ZONE);
105+
Util.cleanUpExistingInstanceTemplates("test-inst-temp-regional", PROJECT_ID);
102106

103107
compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME);
104108
compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE);
105109
compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_WAIT_FOR_OP);
106110
compute.CreateEncryptedInstance
107111
.createEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY);
112+
CreateRegionalInstanceTemplate
113+
.createRegionalInstanceTemplate(PROJECT_ID, REGION, REGIONAL_LOCATION_NAME);
108114

109115
TimeUnit.SECONDS.sleep(30);
110116

@@ -130,6 +136,8 @@ public static void cleanup()
130136
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME);
131137
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE);
132138
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_WITH_SSD);
139+
DeleteRegionalInstanceTemplate
140+
.deleteRegionalInstanceTemplate(PROJECT_ID, REGION, REGIONAL_LOCATION_NAME);
133141

134142
// Delete the Google Cloud Storage bucket created for usage reports.
135143
Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
@@ -254,4 +262,12 @@ public void testListImagesByPage() throws IOException {
254262
Assert.assertTrue(stdOut.toString().contains("Page Number: 1"));
255263
}
256264

265+
@Test
266+
public void testGetRegionalInstanceTemplate() throws IOException {
267+
// Check if the instance was successfully created during the setup.
268+
InstanceTemplate instanceTemplate = GetRegionalInstanceTemplate
269+
.getRegionalInstanceTemplate(PROJECT_ID, REGION,
270+
REGIONAL_LOCATION_NAME);
271+
Assert.assertEquals(REGIONAL_LOCATION_NAME, instanceTemplate.getName());
272+
}
257273
}

0 commit comments

Comments
 (0)