Skip to content

Commit

Permalink
Merged changes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
TetyanaYahodska committed Nov 18, 2024
2 parents 807f221 + 372f0b5 commit 456fd9d
Show file tree
Hide file tree
Showing 30 changed files with 1,483 additions and 249 deletions.
59 changes: 59 additions & 0 deletions auth/src/main/java/UndeleteApiKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START apikeys_undelete_api_key]
import com.google.api.apikeys.v2.ApiKeysClient;
import com.google.api.apikeys.v2.Key;
import com.google.api.apikeys.v2.UndeleteKeyRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class UndeleteApiKey {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Google Cloud project.
String projectId = "YOUR_PROJECT_ID";
// The API key id to undelete.
String keyId = "YOUR_KEY_ID";

undeleteApiKey(projectId, keyId);
}

// Undeletes an API key.
public static void undeleteApiKey(String projectId, String keyId)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (ApiKeysClient apiKeysClient = ApiKeysClient.create()) {

// Initialize the undelete request and set the argument.
UndeleteKeyRequest undeleteKeyRequest = UndeleteKeyRequest.newBuilder()
.setName(String.format("projects/%s/locations/global/keys/%s", projectId, keyId))
.build();

// Make the request and wait for the operation to complete.
Key undeletedKey = apiKeysClient.undeleteKeyAsync(undeleteKeyRequest)
.get(3, TimeUnit.MINUTES);

System.out.printf("Successfully undeleted the API key: %s", undeletedKey.getName());
}
}
}
// [END apikeys_undelete_api_key]
12 changes: 9 additions & 3 deletions auth/src/test/java/ApiKeySnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
public class ApiKeySnippetsIT {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String CREDENTIALS = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
private static Key API_KEY;
private static String API_KEY_STRING;
private ByteArrayOutputStream stdOut;
Expand Down Expand Up @@ -79,8 +78,15 @@ public static void cleanup()

String apiKeyId = getApiKeyId(API_KEY);
DeleteApiKey.deleteApiKey(PROJECT_ID, apiKeyId);
String goal = String.format("Successfully deleted the API key: %s", API_KEY.getName());
assertThat(stdOut.toString()).contains(goal);

UndeleteApiKey.undeleteApiKey(PROJECT_ID, apiKeyId);
String undeletedKey = String.format("Successfully undeleted the API key: %s",
API_KEY.getName());
assertThat(stdOut.toString()).contains(undeletedKey);

DeleteApiKey.deleteApiKey(PROJECT_ID, apiKeyId);
String deletedKey = String.format("Successfully deleted the API key: %s", API_KEY.getName());
assertThat(stdOut.toString()).contains(deletedKey);

stdOut.close();
System.setOut(out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compute.reservation;

// [START compute_consume_any_matching_reservation]
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.ANY_RESERVATION;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.compute.v1.AttachedDisk;
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
import com.google.cloud.compute.v1.InsertInstanceRequest;
import com.google.cloud.compute.v1.Instance;
import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.NetworkInterface;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ReservationAffinity;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ConsumeAnyMatchingReservation {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// Zone where the VM instance will be created.
String zone = "us-central1-a";
// Name of the VM instance you want to query.
String instanceName = "YOUR_INSTANCE_NAME";
// machineType: machine type of the VM being created.
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
String machineTypeName = "n1-standard-4";
// sourceImage: path to the operating system image to mount.
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
// diskSizeGb: storage size of the boot disk to attach to the instance.
long diskSizeGb = 10L;
// networkName: network interface to associate with the instance.
String networkName = "default";
// Minimum CPU platform of the instances.
String minCpuPlatform = "Intel Skylake";

createInstanceAsync(projectId, zone, instanceName, machineTypeName, sourceImage,
diskSizeGb, networkName, minCpuPlatform);
}

// Create a virtual machine targeted with the reserveAffinity field.
// In this consumption model, existing and new VMs automatically consume a reservation
// if their properties match the VM properties specified in the reservation.
public static Instance createInstanceAsync(String projectId, String zone,
String instanceName, String machineTypeName, String sourceImage,
long diskSizeGb, String networkName, String minCpuPlatform)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (InstancesClient instancesClient = InstancesClient.create()) {
AttachedDisk disk =
AttachedDisk.newBuilder()
.setBoot(true)
.setAutoDelete(true)
.setType(AttachedDisk.Type.PERSISTENT.toString())
.setDeviceName("disk-1")
.setInitializeParams(
AttachedDiskInitializeParams.newBuilder()
.setSourceImage(sourceImage)
.setDiskSizeGb(diskSizeGb)
.build())
.build();

NetworkInterface networkInterface = NetworkInterface.newBuilder()
.setName(networkName)
.build();

ReservationAffinity reservationAffinity =
ReservationAffinity.newBuilder()
.setConsumeReservationType(ANY_RESERVATION.toString())
.build();

Instance instanceResource =
Instance.newBuilder()
.setName(instanceName)
.setMachineType(machineType)
.addDisks(disk)
.addNetworkInterfaces(networkInterface)
.setMinCpuPlatform(minCpuPlatform)
.setReservationAffinity(reservationAffinity)
.build();

InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
.setProject(projectId)
.setZone(zone)
.setInstanceResource(instanceResource)
.build();

OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
insertInstanceRequest);

Operation response = operation.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
return null;
}
return instancesClient.get(projectId, zone, instanceName);
}
}
}
// [END compute_consume_any_matching_reservation]
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compute.reservation;

// [START compute_consume_single_project_reservation]
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.compute.v1.AttachedDisk;
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
import com.google.cloud.compute.v1.InsertInstanceRequest;
import com.google.cloud.compute.v1.Instance;
import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.NetworkInterface;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ReservationAffinity;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ConsumeSingleProjectReservation {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String projectId = "YOUR_PROJECT_ID";
// Name of the zone where the reservation is located.
String zone = "us-central1-a";
// Name of the reservation you want to query.
String reservationName = "YOUR_RESERVATION_NAME";
// Name of the VM instance you want to query.
String instanceName = "YOUR_INSTANCE_NAME";
// machineType: machine type of the VM being created.
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
String machineTypeName = "n1-standard-4";
// sourceImage: path to the operating system image to mount.
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
// diskSizeGb: storage size of the boot disk to attach to the instance.
long diskSizeGb = 10L;
// networkName: network interface to associate with the instance.
String networkName = "default";
// Minimum CPU platform of the instances.
String minCpuPlatform = "Intel Skylake";

createInstanceAsync(projectId, zone, instanceName, reservationName, machineTypeName,
sourceImage, diskSizeGb, networkName, minCpuPlatform);
}

// Create a virtual machine targeted with the reserveAffinity field.
// Ensure that the VM's properties match the reservation's VM properties.
public static Instance createInstanceAsync(String projectId, String zone, String instanceName,
String reservationName, String machineTypeName, String sourceImage, long diskSizeGb,
String networkName, String minCpuPlatform)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (InstancesClient instancesClient = InstancesClient.create()) {
AttachedDisk disk =
AttachedDisk.newBuilder()
.setBoot(true)
.setAutoDelete(true)
.setType(AttachedDisk.Type.PERSISTENT.toString())
.setDeviceName("disk-1")
.setInitializeParams(
AttachedDiskInitializeParams.newBuilder()
.setSourceImage(sourceImage)
.setDiskSizeGb(diskSizeGb)
.build())
.build();

NetworkInterface networkInterface = NetworkInterface.newBuilder()
.setName(networkName)
.build();

ReservationAffinity reservationAffinity =
ReservationAffinity.newBuilder()
.setConsumeReservationType(SPECIFIC_RESERVATION.toString())
.setKey("compute.googleapis.com/reservation-name")
// Set specific reservation
.addValues(reservationName)
.build();

Instance instanceResource =
Instance.newBuilder()
.setName(instanceName)
.setMachineType(machineType)
.addDisks(disk)
.addNetworkInterfaces(networkInterface)
.setMinCpuPlatform(minCpuPlatform)
.setReservationAffinity(reservationAffinity)
.build();

InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
.setProject(projectId)
.setZone(zone)
.setInstanceResource(instanceResource)
.build();

OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
insertInstanceRequest);
Operation response = operation.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
return null;
}
return instancesClient.get(projectId, zone, instanceName);
}
}
}
// [END compute_consume_single_project_reservation]
Loading

0 comments on commit 456fd9d

Please sign in to comment.