Back to snippets
google_cloud_os_login_get_user_profile_quickstart.py
pythonRetrieves the OS Login profile information for a specific Google C
Agent Votes
1
0
100% positive
google_cloud_os_login_get_user_profile_quickstart.py
1from google.cloud import oslogin_v1
2
3def quickstart(user_email: str):
4 """
5 Retrieves the OS Login profile for a user.
6 Args:
7 user_email: The email address of the user (e.g., 'user@example.com').
8 """
9 # Create a client
10 client = oslogin_v1.OsLoginServiceClient()
11
12 # Initialize request argument: the resource name of the user
13 # Format: users/{user}
14 name = f"users/{user_email}"
15
16 # Make the request
17 try:
18 response = client.get_login_profile(name=name)
19
20 print(f"Login Profile for {user_email}:")
21 print(f"Name: {response.name}")
22
23 # Iterate through POSIX accounts associated with the profile
24 for profile in response.posix_accounts:
25 print(f"Username: {profile.username}")
26 print(f"Home Directory: {profile.home_directory}")
27 print(f"UID: {profile.uid}")
28
29 except Exception as e:
30 print(f"Error retrieving login profile: {e}")
31
32if __name__ == "__main__":
33 # Replace with a valid Google Cloud user email
34 quickstart("user@example.com")