python-3.xgoogle-cloud-platformgoogle-cloud-dns

How to create DNS record-set in GCP using python script


I am trying to develop a Python Automation script that adds a DNS record-sets of "A" type into my existing GCP DNS Managed-Zone "my-sites"

import json
from google.oauth2 import service_account
from google.cloud import dns
from google.cloud.exceptions import NotFound

gcp_dns_credentials={
  "type": "service_account",
  "project_id": "mygcpprojectid-1122",
  "private_key_id": "myprivkeyid",
  "private_key": "-----BEGIN PRIVATE KEY-----\nmyprivatekey\n-----END PRIVATE KEY-----\n",
  "client_email": "client-mail@mygcpprojectid-1122.iam.gserviceaccount.com",
  "client_id": "myclientid",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/client-mail%40mygcpprojectid-1122.iam.gserviceaccount.com"
}

project_id="mygcpprojectid-1122"
zone_name="my-sites"
dns_credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)

client = dns.Client(project=project_id,credentials=dns_credentials)
zone = client.zone(zone_name)
create_records=dns.resource_record_set.ResourceRecordSet(name="mydnsrecord2.mygcpproject.com",record_type="A",ttl=300,rrdatas=["13.66.xx.xx"],zone=zone)

This script execution neither throws the error nor creates DNS record-set. I referred this doc - https://cloud.google.com/python/docs/reference/dns/latest/resource-record-set

Can someone help me :)


Solution

  • No error is reported because nothing has been done yet on the Google Cloud DNS side.

    DNS changes are made atomically, which means you can make multiple changes (add, delete, etc) and apply them all at once. All changes take effect or none do (rollback).

    Operations with DNS are performed via Change Sets. This means creating a list of the changes (e.g. create / modify / delete a resource record).

    The add_record_set() method appends to the change set link.

    The create() method applies the change set link. This method is what actually modifies your DNS server resource records.

    Google Cloud DNS Change Sets