gcp.py
Source: sunholo/utils/gcp.py
Functions
is_running_on_gcp()
Check if the current environment is a Google Cloud Platform (GCP) instance.
This function attempts to reach the GCP metadata server to determine if the code is running on a GCP instance.
Returns:
bool: True
if running on GCP, False
otherwise.
Example:
if is_running_on_gcp():
print("Running on GCP.")
else:
print("Not running on GCP.")
is_running_on_cloudrun()
Check if the current environment is a Google Cloud Run instance.
Returns:
bool: True
if running on Cloud Run, False
otherwise.
Example:
if is_running_on_cloudrun():
print("Running on Cloud Run.")
else:
print("Not running on Cloud Run.")
get_metadata(stem)
Retrieve metadata information from the GCP metadata server.
Args: stem (str): The metadata path to query.
Returns: str or None: The metadata information if found, None otherwise.
get_region()
Retrieve the region of the GCP instance.
This function attempts to retrieve the region by extracting it from the zone information available in the GCP metadata server.
Returns: str or None: The region if found, None otherwise.
get_service_account_email()
Retrieve the service account email from environment variables or the GCP metadata server.
Returns: str or None: The service account email if found, None otherwise.
is_gcp_logged_in()
Check if the current environment has valid Google Cloud Platform (GCP) credentials.
This function attempts to obtain the default application credentials from the environment.
It will return True
if credentials are available, otherwise it returns False
.
Returns:
bool: True
if GCP credentials are available, False
otherwise.
Example:
if is_gcp_logged_in():
print("GCP credentials found.")
else:
print("GCP credentials not found or invalid.")