Skip to content

Instantly share code, notes, and snippets.

@iolalla
Last active September 14, 2017 15:12
Show Gist options
  • Select an option

  • Save iolalla/dae46ee5ae75510423936c095d83e10b to your computer and use it in GitHub Desktop.

Select an option

Save iolalla/dae46ee5ae75510423936c095d83e10b to your computer and use it in GitHub Desktop.
This is a go, as go from GoLang, program to automate the load of dns export files from infoblox into Google Cloud DNS.
package main
// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Google Cloud DNS API
// and check the quota for your project at
// https://console.developers.google.com/apis/api/dns
// 2. This sample uses Application Default Credentials for authentication.
// If not already done, install the gcloud CLI from
// https://cloud.google.com/sdk/ and run
// `gcloud beta auth application-default login`.
// For more information, see
// https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install and update the Go dependencies by running `go get -u` in the
// project directory.
/** To teest it you can validate it with the following lines
header-arecord,address*,_new_address,fqdn*,_new_fqdn,comment,create_ptr,disabled,ttl,view
header-cnamerecord,fqdn*,_new_fqdn,canonical_name,comment,disabled,ttl,view
header-mxrecord,fqdn*,_new_fqdn,mx*,_new_mx,priority*,_new_priority,comment,disabled,ttl,view
header-txtrecord,fqdn*,_new_fqdn,text*,_new_text,comment,disabled,ttl,view
arecord,194.224.72.187,,test.test.jcrontab.com.,,,False,False,3600,INTERNET
arecord,194.224.72.187,,www.test.jcrontab.com.,,,False,False,3600,INTERNET
cnamerecord,ftp.test.jcrontab.com.,,www.test.jcrontab.com,,False,3600,INTERNET
mxrecord,test.jcrontab.com.,,mail10.test.jcrontab.com,,10,,,False,3600,INTERNET
mxrecord,tes1.test.jcrontab.com.,,mail11.test.jcrontab.com,,20,,,False,3600,INTERNET
mxrecord,test2.test.jcrontab.com.,,poster4.iberlayer.com,,40,,,False,,INTERNET
txtrecord,test.jcrontab.com.,,"""v=spf1 ip4:194.224.72.10/32 ip4:194.224.72.43/32 ip4:194.224.72.63/32 ip4:194.224.72.64/32 -all""",,,False,3600,INTERNET
txtrecord,test1.test.jcrontab.com.,,"""v=spf1 include:iblhelper.net """,,,False,,INTERNET
*/
import (
"os"
"bufio"
"io"
"fmt"
"log"
"strconv"
"strings"
"encoding/csv"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("To use it you must pass a file in CSV format with the DNS Entries \n ./%s YourFilename.csv \n", os.Args[0])
return
}
// Name of the file, if the file doesn't ex
fileName := os.Args[1]
if fileName == "" {
fmt.Printf("./%s YourFilename.csv \n", os.Args[0])
return
}
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fmt.Printf("File %s does not exist!! \n", os.Args[1])
return
}
f, _ := os.Open(fileName)
// Create a new reader.
r := csv.NewReader(bufio.NewReader(f))
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
entryType := record[0]
kind := "TXT"
name := ""
switch entryType {
case "txtrecord":
name = completeName(record[1])
var ttl int64 = 3600
if record[7] != "" {
ttl2, err := strconv.ParseInt(record[7], 10, 64)
if err != nil {
log.Fatal(err)
}
ttl = ttl2
}
err = addDNS(name, []string{record[3]}, ttl, kind)
if err != nil {
log.Fatal(err)
}
fmt.Println("TXT 'Record added'")
case "mxrecord":
kind = "MX"
name = completeName(record[1])
var ttl int64 = 3600
if record[9] != "" {
ttl2, err := strconv.ParseInt(record[9], 10, 64)
if err != nil {
log.Fatal(err)
}
ttl = ttl2
}
entry := record[5] + " " + completeName(record[3])
err = addDNS(name, []string{entry}, ttl, kind)
if err != nil {
log.Fatal(err)
}
fmt.Println("'MX Record added'")
case "arecord":
kind = "A"
var ttl int64 = 3600
name = completeName(record[3])
if record[8] != "" {
ttl2, err := strconv.ParseInt(record[8], 10, 64)
if err != nil {
log.Fatal(err)
}
ttl = ttl2
}
err = addDNS(name, []string{record[1]}, ttl, kind)
if err != nil {
log.Fatal(err)
}
fmt.Println("'A Record added'")
case "cnamerecord":
kind = "CNAME"
name = completeName(record[1])
var ttl int64 = 3600
if record[6] != "" {
ttl2, err := strconv.ParseInt(record[6], 10, 64)
if err != nil {
log.Fatal(err)
}
ttl = ttl2
}
err = addDNS(name, []string{completeName(record[3])}, ttl, kind)
if err != nil {
log.Fatal(err)
}
fmt.Println("'CNAME Record added'")
case "header-txtrecord", "header-mxrecord", "header-arecord", "header-cnamerecord":
break
default:
fmt.Println("Unknown command; try 'txtrecord', 'mxrecord', 'arecord' and 'cnamerecord'")
}
}
}
func addDNS(name string, rrData []string, ttl int64, kind string) error {
ctx := context.Background()
c, err := google.DefaultClient(ctx, dns.CloudPlatformScope)
if err != nil {
return err
}
dnsService, err := dns.New(c)
if err != nil {
return err
}
// Identifies the project addressed by this request.
project := "REPLACEWITHYOURPROJECTNAME"
// Identifies the managed zone addressed by this request. Can be the managed zone name or id.
managedZone := "REPLACEWITHYOURDNSZONEID"
rec := &dns.ResourceRecordSet{
Name: name,
Rrdatas: rrData,
Ttl: int64(ttl),
Type: kind,
}
rb := &dns.Change{
Additions: []*dns.ResourceRecordSet{rec},
}
resp, err := dnsService.Changes.Create(project, managedZone, rb).Context(ctx).Do()
if err != nil {
return err
}
// TODO: Change code below to process the `resp` object:
fmt.Printf("%#v\n", resp)
return nil
}
func completeName(name string) string {
if strings.HasSuffix(name, ".") {
return name
}
return name + "."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment