Google Cloud Parametermanager

0.4.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart initializes the Parameter Manager client and lists all parameters configured within a specified Google Cloud project and the 'global' location. It demonstrates basic client instantiation and interaction, highlighting the importance of setting the project ID and having appropriate IAM permissions. Ensure Application Default Credentials (ADC) are configured for authentication.

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)

view raw JSON →