Skip to content

Commit 4d10950

Browse files
feat(auth): add apikeys undelete api key sample. (#9619)
* Implemented apikeys_undelete_api_key sample, created test * Fixed header
1 parent 2df21d1 commit 4d10950

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
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);

0 commit comments

Comments
 (0)