Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit DNS records with gcloud by reading/piping from file

I am able to edit DNS records with gcloud tool by interactively editing JSON file from vi/mate using the command:

gcloud dns records --zone=myzone edit

However, I would like to be able to do bulk updates, something like this:

gcloud dns records --zone=myzone edit < my-additional-records.txt

...where my-additional-records.txt contain the DNS records I want to add.

I think it is not so simple as the JSON file to edit contains both addition and deletion of DNS records. So, any tips will be appreciated.

like image 427
kctang Avatar asked Jun 21 '14 09:06

kctang


2 Answers

I think you could try using the linux line editor 'ed' like this:

EDITOR=ed gcloud dns records --zone=myzone edit <<-EOF
12i
, {
        "kind": "dns#resourceRecordSet",
        "name": "a.mydomain.org.",
        "rrdatas": [
            "111.222.111.222"
        ],
        "ttl": 21600,
        "type": "A"
}
.
,wq
EOF

This assumes that the top of the edit file looks something like this (so you an append your addition to the 12th line)

{
    "additions": [
        {
            "kind": "dns#resourceRecordSet",
            "name": "mydomain.org.",
            "rrdatas": [
                "ns-cloud-c1.googledomains.com. dns-admin.google.com. 2 21600 3600 1209600 300"
            ],
            "ttl": 21600,
            "type": "SOA"
        },

More details on how to use ed here: http://www.gnu.org/software/ed/manual/ed_manual.html

like image 122
nwaltham Avatar answered Oct 07 '22 22:10

nwaltham


Providing this new answer since gcloud dns ... has been significantly improved since this question was asked. There are now two ways in which you can edit record-sets.

  1. Using export/import commands as follows:

    gcloud dns record-sets export --zone my-zone RECORDS-FILE
    

    By default, RECORDS-FILE is in yaml format. You can also get it in zone file format by using the --zone-file-format flag.

    You can then edit RECORDS-FILE using any editor of your choice, and import the changed records using:

    gcloud dns record-sets import --zone my-zone --delete-all-existing RECORDS-FILE
    

    Import also accepts --zone-file-format. If you want to just add/edit some records but not all of them, you can omit the --delete-all-existing flag.

  2. Using the transaction command group as follows:

    gcloud dns record-sets transaction start # Start transaction
    gcloud dns record-sets transaction add/remove # Add remove records
    gcloud dns record-sets transaction execute # Execute transaction
    
like image 22
Vilas Avatar answered Oct 07 '22 22:10

Vilas