Google Cloud Parametermanager
google-cloud-parametermanager is the Python client library for the Google Cloud Parametermanager API. This service, an extension to Secret Manager, provides a centralized and secure store for application configuration parameters such as environment variables, feature flags, and API keys. It offers features like data encryption, versioning, support for structured formats (YAML, JSON), and fine-grained Identity and Access Management (IAM) controls. The current version is 0.4.0, and as part of the `google-cloud-python` monorepo, its sub-libraries receive frequent updates.
Common errors
-
google.api_core.exceptions.PermissionDenied: 403 Permission 'parametermanager.parameters.list' denied for resource...
cause The authenticated principal (user or service account) lacks the necessary IAM permissions to perform the requested operation on the Parameter Manager resource.fixGrant the appropriate IAM role, such as `roles/parametermanager.viewer` (for reading) or `roles/parametermanager.editor` (for modifying), to the principal on the Google Cloud project or specific Parameter Manager resource. -
ModuleNotFoundError: No module named 'google.cloud.parametermanager'
cause The `google-cloud-parametermanager` library is not installed, or the import path is incorrect, often missing the version suffix.fixInstall the library using `pip install google-cloud-parametermanager`. Ensure the import statement correctly uses the versioned path: `from google.cloud.parametermanager_v1 import ParameterManagerServiceClient`. -
ValueError: A parameter name must be provided to the API.
cause Methods expecting a parameter resource name (e.g., `get_parameter`, `delete_parameter`) were called without providing the full resource name in the format `projects/{project_id}/locations/{location}/parameters/{parameter_id}`.fixConstruct the full resource name string correctly, including the project ID, location, and parameter ID, and pass it to the relevant client method's `name` argument.
Warnings
- breaking The `google-cloud-parametermanager` library is currently in 'Public Preview' status. This means it is under active development, and any release is subject to backwards-incompatible changes at any time. Features, API surfaces, and behavior may change without extensive deprecation periods.
- gotcha Improper IAM permissions for the service account or user account used to access the Parameter Manager API will result in `PermissionDenied` errors.
- gotcha Authentication to Google Cloud APIs, including Parameter Manager, relies on Application Default Credentials (ADC). Misconfiguration of ADC often leads to authentication errors or unexpected behavior.
Install
-
pip install google-cloud-parametermanager
Imports
- ParameterManagerServiceClient
from google.cloud.parametermanager import ParameterManagerServiceClient
from google.cloud.parametermanager_v1 import ParameterManagerServiceClient
Quickstart
import os
from google.cloud.parametermanager_v1 import ParameterManagerServiceClient
def quickstart_parametermanager(project_id: str):
"""
Initializes the Parameter Manager client and lists parameters in a project.
Requires GOOGLE_APPLICATION_CREDENTIALS to be set or ADC to be configured.
"""
if not project_id or project_id == "your-gcp-project-id":
print("Please provide a valid Google Cloud Project ID.")
print("Set the GOOGLE_CLOUD_PROJECT environment variable or update the project_id variable.")
return
client = ParameterManagerServiceClient()
# The parent format is projects/{project_id}/locations/{location}
# 'global' is a common location for many Parameter Manager operations.
parent = f"projects/{project_id}/locations/global"
try:
print(f"Listing parameters in project {project_id} (location: global)...")
# List all parameters in the specified project and location
for parameter in client.list_parameters(parent=parent):
print(f" Parameter Name: {parameter.name}")
print(f" Format: {parameter.format.name}")
print(f" Create Time: {parameter.create_time}")
if parameter.labels:
print(f" Labels: {parameter.labels}")
print("---")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure the Parameter Manager API is enabled for your project and your credentials have 'roles/parametermanager.viewer' or similar permissions.")
if __name__ == "__main__":
# Replace 'your-gcp-project-id' with your actual Google Cloud Project ID.
# For local development, ensure Application Default Credentials are set up:
# 1. `gcloud auth application-default login`
# 2. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account key file path.
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-gcp-project-id")
quickstart_parametermanager(project_id)