Skip to content

Commit be97563

Browse files
Merge branch 'main' into tpu-queued-resources-crud-operations
2 parents 404d6c6 + 372f0b5 commit be97563

30 files changed

+1483
-250
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// [START apikeys_undelete_api_key]
18+
import com.google.api.apikeys.v2.ApiKeysClient;
19+
import com.google.api.apikeys.v2.Key;
20+
import com.google.api.apikeys.v2.UndeleteKeyRequest;
21+
import java.io.IOException;
22+
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.TimeoutException;
25+
26+
public class UndeleteApiKey {
27+
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 Google Cloud project.
32+
String projectId = "YOUR_PROJECT_ID";
33+
// The API key id to undelete.
34+
String keyId = "YOUR_KEY_ID";
35+
36+
undeleteApiKey(projectId, keyId);
37+
}
38+
39+
// Undeletes an API key.
40+
public static void undeleteApiKey(String projectId, String keyId)
41+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (ApiKeysClient apiKeysClient = ApiKeysClient.create()) {
45+
46+
// Initialize the undelete request and set the argument.
47+
UndeleteKeyRequest undeleteKeyRequest = UndeleteKeyRequest.newBuilder()
48+
.setName(String.format("projects/%s/locations/global/keys/%s", projectId, keyId))
49+
.build();
50+
51+
// Make the request and wait for the operation to complete.
52+
Key undeletedKey = apiKeysClient.undeleteKeyAsync(undeleteKeyRequest)
53+
.get(3, TimeUnit.MINUTES);
54+
55+
System.out.printf("Successfully undeleted the API key: %s", undeletedKey.getName());
56+
}
57+
}
58+
}
59+
// [END apikeys_undelete_api_key]

auth/src/test/java/ApiKeySnippetsIT.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
public class ApiKeySnippetsIT {
3737

3838
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
39-
private static final String CREDENTIALS = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
4039
private static Key API_KEY;
4140
private static String API_KEY_STRING;
4241
private ByteArrayOutputStream stdOut;
@@ -79,8 +78,15 @@ public static void cleanup()
7978

8079
String apiKeyId = getApiKeyId(API_KEY);
8180
DeleteApiKey.deleteApiKey(PROJECT_ID, apiKeyId);
82-
String goal = String.format("Successfully deleted the API key: %s", API_KEY.getName());
83-
assertThat(stdOut.toString()).contains(goal);
81+
82+
UndeleteApiKey.undeleteApiKey(PROJECT_ID, apiKeyId);
83+
String undeletedKey = String.format("Successfully undeleted the API key: %s",
84+
API_KEY.getName());
85+
assertThat(stdOut.toString()).contains(undeletedKey);
86+
87+
DeleteApiKey.deleteApiKey(PROJECT_ID, apiKeyId);
88+
String deletedKey = String.format("Successfully deleted the API key: %s", API_KEY.getName());
89+
assertThat(stdOut.toString()).contains(deletedKey);
8490

8591
stdOut.close();
8692
System.setOut(out);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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_consume_any_matching_reservation]
20+
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.ANY_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 ConsumeAnyMatchingReservation {
37+
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+
// Zone where the VM instance will be created.
44+
String zone = "us-central1-a";
45+
// Name of the VM instance you want to query.
46+
String instanceName = "YOUR_INSTANCE_NAME";
47+
// machineType: machine type of the VM being created.
48+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
49+
String machineTypeName = "n1-standard-4";
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+
// Minimum CPU platform of the instances.
58+
String minCpuPlatform = "Intel Skylake";
59+
60+
createInstanceAsync(projectId, zone, instanceName, machineTypeName, sourceImage,
61+
diskSizeGb, networkName, minCpuPlatform);
62+
}
63+
64+
// Create a virtual machine targeted with the reserveAffinity field.
65+
// In this consumption model, existing and new VMs automatically consume a reservation
66+
// if their properties match the VM properties specified in the reservation.
67+
public static Instance createInstanceAsync(String projectId, String zone,
68+
String instanceName, String machineTypeName, String sourceImage,
69+
long diskSizeGb, String networkName, String minCpuPlatform)
70+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
71+
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
72+
// Initialize client that will be used to send requests. This client only needs to be created
73+
// once, and can be reused for multiple requests.
74+
try (InstancesClient instancesClient = InstancesClient.create()) {
75+
AttachedDisk disk =
76+
AttachedDisk.newBuilder()
77+
.setBoot(true)
78+
.setAutoDelete(true)
79+
.setType(AttachedDisk.Type.PERSISTENT.toString())
80+
.setDeviceName("disk-1")
81+
.setInitializeParams(
82+
AttachedDiskInitializeParams.newBuilder()
83+
.setSourceImage(sourceImage)
84+
.setDiskSizeGb(diskSizeGb)
85+
.build())
86+
.build();
87+
88+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
89+
.setName(networkName)
90+
.build();
91+
92+
ReservationAffinity reservationAffinity =
93+
ReservationAffinity.newBuilder()
94+
.setConsumeReservationType(ANY_RESERVATION.toString())
95+
.build();
96+
97+
Instance instanceResource =
98+
Instance.newBuilder()
99+
.setName(instanceName)
100+
.setMachineType(machineType)
101+
.addDisks(disk)
102+
.addNetworkInterfaces(networkInterface)
103+
.setMinCpuPlatform(minCpuPlatform)
104+
.setReservationAffinity(reservationAffinity)
105+
.build();
106+
107+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
108+
.setProject(projectId)
109+
.setZone(zone)
110+
.setInstanceResource(instanceResource)
111+
.build();
112+
113+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
114+
insertInstanceRequest);
115+
116+
Operation response = operation.get(3, TimeUnit.MINUTES);
117+
118+
if (response.hasError()) {
119+
return null;
120+
}
121+
return instancesClient.get(projectId, zone, instanceName);
122+
}
123+
}
124+
}
125+
// [END compute_consume_any_matching_reservation]
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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_consume_single_project_reservation]
20+
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_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 ConsumeSingleProjectReservation {
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 where the reservation is located.
43+
String zone = "us-central1-a";
44+
// Name of the reservation you want to query.
45+
String reservationName = "YOUR_RESERVATION_NAME";
46+
// Name of the VM instance you want to query.
47+
String instanceName = "YOUR_INSTANCE_NAME";
48+
// machineType: machine type of the VM being created.
49+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
50+
String machineTypeName = "n1-standard-4";
51+
// sourceImage: path to the operating system image to mount.
52+
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
53+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
54+
// diskSizeGb: storage size of the boot disk to attach to the instance.
55+
long diskSizeGb = 10L;
56+
// networkName: network interface to associate with the instance.
57+
String networkName = "default";
58+
// Minimum CPU platform of the instances.
59+
String minCpuPlatform = "Intel Skylake";
60+
61+
createInstanceAsync(projectId, zone, instanceName, reservationName, machineTypeName,
62+
sourceImage, diskSizeGb, networkName, minCpuPlatform);
63+
}
64+
65+
// Create a virtual machine targeted with the reserveAffinity field.
66+
// Ensure that the VM's properties match the reservation's VM properties.
67+
public static Instance createInstanceAsync(String projectId, String zone, String instanceName,
68+
String reservationName, String machineTypeName, String sourceImage, long diskSizeGb,
69+
String networkName, String minCpuPlatform)
70+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
71+
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
72+
// Initialize client that will be used to send requests. This client only needs to be created
73+
// once, and can be reused for multiple requests.
74+
try (InstancesClient instancesClient = InstancesClient.create()) {
75+
AttachedDisk disk =
76+
AttachedDisk.newBuilder()
77+
.setBoot(true)
78+
.setAutoDelete(true)
79+
.setType(AttachedDisk.Type.PERSISTENT.toString())
80+
.setDeviceName("disk-1")
81+
.setInitializeParams(
82+
AttachedDiskInitializeParams.newBuilder()
83+
.setSourceImage(sourceImage)
84+
.setDiskSizeGb(diskSizeGb)
85+
.build())
86+
.build();
87+
88+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
89+
.setName(networkName)
90+
.build();
91+
92+
ReservationAffinity reservationAffinity =
93+
ReservationAffinity.newBuilder()
94+
.setConsumeReservationType(SPECIFIC_RESERVATION.toString())
95+
.setKey("compute.googleapis.com/reservation-name")
96+
// Set specific reservation
97+
.addValues(reservationName)
98+
.build();
99+
100+
Instance instanceResource =
101+
Instance.newBuilder()
102+
.setName(instanceName)
103+
.setMachineType(machineType)
104+
.addDisks(disk)
105+
.addNetworkInterfaces(networkInterface)
106+
.setMinCpuPlatform(minCpuPlatform)
107+
.setReservationAffinity(reservationAffinity)
108+
.build();
109+
110+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
111+
.setProject(projectId)
112+
.setZone(zone)
113+
.setInstanceResource(instanceResource)
114+
.build();
115+
116+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
117+
insertInstanceRequest);
118+
Operation response = operation.get(3, TimeUnit.MINUTES);
119+
120+
if (response.hasError()) {
121+
return null;
122+
}
123+
return instancesClient.get(projectId, zone, instanceName);
124+
}
125+
}
126+
}
127+
// [END compute_consume_single_project_reservation]

0 commit comments

Comments
 (0)