Home > Software engineering >  How to create DNS record-set in GCP using python script
How to create DNS record-set in GCP using python script

Time:03-20

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": "[email protected]",
  "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/[email protected]"
}

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 :)

CodePudding user response:

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

CodePudding user response:

Just reiterating @JohnHanley solution with python code

from google.oauth2 import service_account
from googleapiclient import discovery

gcp_dns_credentials={
  "blah blah": "all dummy credentials in json format already mentioned in the question "
}
project_id="mygcpprojectid-1122"
zone_name="my-sites"

credentials = service_account.Credentials.from_service_account_info(gcp_dns_credentials)
service = discovery.build('dns', 'v1', credentials=credentials)

change_body = {
    "additions": [
        {
      "name": "mydnsrecord2.mygcpproject.com.",
      "type": "A",
      "ttl": 300,
      "rrdata": ["13.66.xx.xx"]
      }
  ]
}

request = service.changes().create(project=project_id, managedZone=zone_name, body=change_body)
response = request.execute()

This script execution would create mydnsrecord2.mygcpproject.com record-set

Referred this doc https://cloud.google.com/dns/docs/reference/v1/changes/create#python

  • Related