Google Cloud DNS Client Library

0.36.1 · active · verified Tue Apr 14

The Google Cloud DNS API client library provides a Pythonic interface for managing DNS records and managed zones within Google Cloud DNS. It is part of the broader `google-cloud-python` monorepo, benefiting from its active development and frequent updates across Google Cloud services.

Warnings

Install

Imports

Quickstart

This quickstart initializes the Google Cloud DNS client and lists all managed zones within a specified Google Cloud project. It leverages Application Default Credentials (ADC) for authentication, automatically inferring credentials from the environment. For local execution, ensure the `GOOGLE_CLOUD_PROJECT` environment variable is set or replace the placeholder. You'll also need to authenticate with `gcloud auth application-default login`.

import os
from google.cloud import dns

# Your Google Cloud project ID
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')

if not project_id or project_id == 'your-project-id':
    print("Please set the 'GOOGLE_CLOUD_PROJECT' environment variable or replace 'your-project-id' in the code.")
    exit(1)

# Initialize the DNS client
# The client automatically handles authentication using Application Default Credentials (ADC)
# For local development, ensure you've run 'gcloud auth application-default login'
client = dns.Client(project=project_id)

try:
    # List managed zones
    print(f"Listing managed zones for project: {project_id}")
    zones = client.list_zones()

    if not zones:
        print("No managed zones found.")
    else:
        for zone in zones:
            print(f"  Zone Name: {zone.name}, DNS Name: {zone.dns_name}")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →