6

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.

kctang
  • 10,894
  • 8
  • 44
  • 63

3 Answers3

4

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

nwaltham
  • 2,067
  • 1
  • 22
  • 40
2

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
    
Vilas
  • 1,405
  • 12
  • 15
0

I haven't tried that myself, but if the JSON format is the one here: https://developers.google.com/cloud-dns/getting-started

How about just setting deletions : [] (empty JSON array). Wouldn't that achieve what you want (no deletions)? Is that failing somehow?

Update 1:

If the problem is the actual piping input to the program and you can't find a shell based way to do it, I found this migration script, that shows how you can script this action (using python).

snowdragon
  • 753
  • 7
  • 18