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] 
0 commit comments