Back to snippets

google_cloud_service_identity_generation_quickstart.py

python

Generates a service identity for a specific Google Cloud service in a p

15d ago35 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_service_identity_generation_quickstart.py
1# Import the Google Cloud Service Identity library
2from google.cloud import service_identity_v1
3
4def quickstart(project_id: str, service_name: str):
5    """
6    Generates a service identity for a Google Cloud service.
7    Args:
8        project_id: The project ID or project number.
9        service_name: The name of the service (e.g., 'compute.googleapis.com').
10    """
11    # Create a client
12    client = service_identity_v1.ServiceIdentityClient()
13
14    # Initialize request argument(s)
15    # The parent takes the format: projects/{project_id}/services/{service_name}
16    parent = f"projects/{project_id}/services/{service_name}"
17    
18    request = service_identity_v1.GenerateServiceIdentityRequest(
19        parent=parent,
20    )
21
22    # Make the request
23    operation = client.generate_service_identity(request=request)
24
25    print("Waiting for operation to complete...")
26
27    response = operation.result()
28
29    # Handle the response
30    print(f"Service Identity Email: {response.email}")
31    print(f"Service Identity Unique ID: {response.unique_id}")
32
33if __name__ == "__main__":
34    # Replace with your project ID and the service you want to generate an identity for
35    quickstart("your-project-id", "compute.googleapis.com")