-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (140 loc) · 3.57 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
const urlPrefix string = "https://api.netlify.com/api/v1/"
type DnsZone struct {
Id string `json:"id"`
Name string `json:"name"`
}
type DnsRecord struct {
Id string `json:"id"`
DnsZoneId string `json:"dns_zone_id"`
Hostname string `json:"hostname"`
Type string `json:"type"`
Ttl int `json:"ttl"`
Priority int `json:"priority"`
Weight *int `json:"weight,omitempty"`
Port *int `json:"port,omitempty"`
Flag *string `json:"flag,omitempty"`
Tag *string `json:"tag,omitempty"`
Managed bool `json:"managed"`
Value string `json:"value"`
}
type NetlifyDnsClient struct {
client *http.Client
token string
}
func NewNetlifyDnsClient(token string) NetlifyDnsClient {
client := &http.Client{}
return NetlifyDnsClient{client: client, token: token}
}
func (n *NetlifyDnsClient) addAuthHeader(req *http.Request) {
req.Header.Add("Authorization", "Bearer "+n.token)
}
func (n *NetlifyDnsClient) getReqByteSlice(endpoint string) ([]byte, error) {
req, err := http.NewRequest("GET", urlPrefix+endpoint, nil)
if err != nil {
return nil, fmt.Errorf("error creating get request: %w", err)
}
n.addAuthHeader(req)
resp, err := n.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error doing get request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading get request body: %w", err)
}
return body, nil
}
func (n *NetlifyDnsClient) GetAllDnsZones() ([]DnsZone, error) {
body, err := n.getReqByteSlice("dns_zones")
if err != nil {
return nil, err
}
var dnsZones []DnsZone
err = json.Unmarshal(body, &dnsZones)
if err != nil {
return nil, fmt.Errorf("error unmarshalling get request body: %w", err)
}
return dnsZones, nil
}
func (n *NetlifyDnsClient) GetAllDnsRecords(zoneId string) ([]DnsRecord, error) {
body, err := n.getReqByteSlice("dns_zones/" + zoneId + "/dns_records")
if err != nil {
return nil, err
}
var records []DnsRecord
err = json.Unmarshal(body, &records)
if err != nil {
return nil, fmt.Errorf("error unmarshalling get request body: %w", err)
}
return records, nil
}
func GenerateZoneFile(zone DnsZone, records []DnsRecord) (string, error) {
var zoneFile strings.Builder
zoneFile.WriteString(fmt.Sprintf("$ORIGIN %s\n", zone.Name+"."))
for _, record := range records {
if record.Type == "NETLIFY" {
fmt.Printf("ingoring NETLIFY record: %s\n", record.Hostname)
continue
}
name := record.Hostname + "."
var value string
if record.Type == "CNAME" {
value = record.Value + "."
} else {
value = record.Value
}
var priority = ""
if record.Priority != 0 {
priority = fmt.Sprintf("\t%d", record.Priority)
}
zoneFile.WriteString(
fmt.Sprintf(
"%s\tIN\t%d\t%s%s\t%s\n",
name,
record.Ttl,
record.Type,
priority,
value,
),
)
}
return zoneFile.String(), nil
}
func main() {
token := os.Getenv("NETLIFY_TOKEN")
if token == "" {
log.Fatalln("NETLIFY_TOKEN was not set.")
}
client := NewNetlifyDnsClient(token)
zones, err := client.GetAllDnsZones()
if err != nil {
log.Fatalln(err)
}
for _, zone := range zones {
records, err := client.GetAllDnsRecords(zone.Id)
if err != nil {
log.Fatalln(err)
}
zoneContents, err := GenerateZoneFile(zone, records)
if err != nil {
log.Fatalln(err)
}
fileName := zone.Id + ".zone"
err = os.WriteFile(fileName, []byte(zoneContents), 0644)
if err != nil {
log.Fatalln(err)
}
fmt.Println(fileName)
}
}