-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcloud.py
37 lines (28 loc) · 1.24 KB
/
gcloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from google.cloud import api_keys_v2
from google.cloud.api_keys_v2 import Key
def create_api_key(project_id: str, suffix: str) -> Key:
"""
Creates and restrict an API key. Add the suffix for uniqueness.
TODO(Developer):
1. Before running this sample,
set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc
2. Make sure you have the necessary permission to create API keys.
Args:
project_id: Google Cloud project id.
Returns:
response: Returns the created API Key.
"""
# Create the API Keys client.
client = api_keys_v2.ApiKeysClient()
key = api_keys_v2.Key()
key.display_name = f"My first API key - {suffix}"
# Initialize request and set arguments.
request = api_keys_v2.CreateKeyRequest()
request.parent = f"projects/{project_id}/locations/global"
request.key = key
# Make the request and wait for the operation to complete.
response = client.create_key(request=request).result()
print(f"Successfully created an API key: {response.name}")
# For authenticating with the API key, use the value in "response.key_string".
# To restrict the usage of this API key, use the value in "response.name".
return response