Skip to content

Commit

Permalink
feat(auth): add apikeys undelete api key sample. (#9619)
Browse files Browse the repository at this point in the history
* Implemented apikeys_undelete_api_key sample, created test

* Fixed header
  • Loading branch information
TetyanaYahodska authored Nov 8, 2024
1 parent 2df21d1 commit 4d10950
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 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

0 comments on commit 4d10950

Please sign in to comment.