From 4d109500e2b73ad5fb42d0b9178fa2c7546552be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B5=D1=82=D1=8F=D0=BD=D0=B0=20=D0=AF=D0=B3=D0=BE?= =?UTF-8?q?=D0=B4=D1=81=D1=8C=D0=BA=D0=B0?= <49729677+TetyanaYahodska@users.noreply.github.com> Date: Fri, 8 Nov 2024 14:44:37 +0100 Subject: [PATCH] feat(auth): add apikeys undelete api key sample. (#9619) * Implemented apikeys_undelete_api_key sample, created test * Fixed header --- auth/src/main/java/UndeleteApiKey.java | 59 ++++++++++++++++++++++++ auth/src/test/java/ApiKeySnippetsIT.java | 12 +++-- 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 auth/src/main/java/UndeleteApiKey.java diff --git a/auth/src/main/java/UndeleteApiKey.java b/auth/src/main/java/UndeleteApiKey.java new file mode 100644 index 00000000000..cd509c705b3 --- /dev/null +++ b/auth/src/main/java/UndeleteApiKey.java @@ -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] \ No newline at end of file diff --git a/auth/src/test/java/ApiKeySnippetsIT.java b/auth/src/test/java/ApiKeySnippetsIT.java index 46a059d2203..7f65313d0e1 100644 --- a/auth/src/test/java/ApiKeySnippetsIT.java +++ b/auth/src/test/java/ApiKeySnippetsIT.java @@ -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; @@ -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);