-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcpDNS.go
82 lines (65 loc) · 1.77 KB
/
gcpDNS.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"log"
"time"
"golang.org/x/net/context"
"google.golang.org/api/dns/v1"
)
type gcpData struct {
projectName string
zoneName string
ttlValue int64
}
// UpdateDNSRecord will update the DNS record in a Google Cloud DNS Managed Zone.
func UpdateDNSRecord(ipInfo *ipData, gcpInfo *gcpData, ipType string) {
var newPublicIP string
var previousIP string
newPublicIP = ipInfo.publicIP
previousIP = ipInfo.domainIP
ctx := context.Background()
dnsService, err := dns.NewService(ctx)
if err != nil {
log.Fatal(err)
}
// GCP requires that the domain name be fully qualified, i.e. includes a period at the end for the root zone
domainName := ipInfo.domainName + "."
addResource := &dns.ResourceRecordSet{
Kind: "dns#resourceRecordSet",
Name: domainName,
Rrdatas: []string{
newPublicIP,
},
Ttl: gcpInfo.ttlValue,
Type: ipType,
}
deleteResource := &dns.ResourceRecordSet{
Kind: "dns#resourceRecordSet",
Name: domainName,
Rrdatas: []string{
previousIP,
},
Ttl: gcpInfo.ttlValue,
Type: ipType,
}
rb := &dns.Change{
Additions: []*dns.ResourceRecordSet{
addResource,
},
Deletions: []*dns.ResourceRecordSet{
deleteResource,
},
IsServing: true,
Kind: "dns#change",
}
resp, err := dnsService.Changes.Create(gcpInfo.projectName, gcpInfo.zoneName, rb).Context(ctx).Do()
if err != nil {
log.Fatal(err)
}
log.Printf("Request to update DNS record %s (%s) with IP %s sent. Status: %s", domainName, ipType, newPublicIP, resp.Status)
time.Sleep(10 * time.Second)
getStatus, err := dnsService.Changes.Get(gcpInfo.projectName, gcpInfo.zoneName, resp.Id).Context(ctx).Do()
if err != nil {
log.Fatal(err)
}
log.Printf("Status of request to update DNS record %s (%s): %s", domainName, ipType, getStatus.Status)
}