Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
)
type record struct {
domain string
ttl int64
dnsType string
content string
}
func check(e error) {
if e != nil {
panic(e)
}
}
func initAWSSession() (*route53.Route53, error) {
sess, err := session.NewSessionWithOptions(session.Options{
Profile: "relinklabs",
})
check(err)
//sess := session.Must(session.NewSessionWithOptions(session.Options{Profile: "relinklabs"}))
svc := route53.New(sess)
credentials, err := svc.Config.Credentials.Get()
check(err)
fmt.Println("signing " + credentials.AccessKeyID)
return svc, nil
}
func (dns record) String() string {
result := fmt.Sprintf("\nName:\t%s\nTTL:\t%d\nType:\t%s\nContent:%s\n",
dns.domain,
dns.ttl,
dns.dnsType,
dns.content)
return result
}
func (dns record) createAWSRecord(svc *route53.Route53) {
if dns.dnsType == "TXT" {
dns.content = `"` + dns.content + `"`
}
params := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("UPSERT"), // Required
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(dns.domain),
Type: aws.String(dns.dnsType),
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(dns.content), // Required
},
},
TTL: aws.Int64(dns.ttl),
},
},
},
},
HostedZoneId: aws.String(""), // Required
}
resp, err := svc.ChangeResourceRecordSets(params)
check(err)
fmt.Println(resp)
}
func main() {
var dns record
recordFile, err := os.Open("zonefile.txt")
check(err)
defer recordFile.Close()
svc, err := initAWSSession()
check(err)
scanner := bufio.NewScanner(recordFile)
for scanner.Scan() {
line := scanner.Text()
lineSplitted := strings.Split(line, "\t")
lineClean := lineSplitted[0:3]
dns.domain = lineClean[0]
dns.ttl, err = strconv.ParseInt(lineClean[1], 10, 64)
check(err)
dns.dnsType = lineClean[2]
dns.content = strings.Join(lineSplitted[3:], " ")
fmt.Println(dns)
if (dns.dnsType != "NS") && (dns.dnsType != "SOA") && (dns.dnsType != "SRV") {
dns.createAWSRecord(svc)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.