-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare.go
92 lines (78 loc) · 2.29 KB
/
cloudflare.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
package main
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
)
type Record struct {
ID string
Name string
}
func GetZoneID(cf *cloudflare.API, zoneName string) (string, error) {
zones, err := cf.ListZones(context.Background())
if err != nil {
return "", fmt.Errorf("failed to list zones: %w", err)
}
for _, z := range zones {
if z.Name == zoneName {
return z.ID, nil
}
}
return "", fmt.Errorf("zone %s not found", zoneName)
}
func IterateRecords(cf *cloudflare.API, zoneID string, fn func(record Record) error) error {
recs, _, err := cf.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{})
if err != nil {
return fmt.Errorf("failed to list DNS records: %w", err)
}
for _, r := range recs {
r := Record{ID: r.ID, Name: r.Name}
if err := fn(r); err != nil {
// TODO better error handling
return fmt.Errorf("error in callback for record %+v: %w", r, err)
}
}
return nil
}
func CreateRecord(cf *cloudflare.API, zoneID string, hostname string, ip string) error {
// Check if the record already exists
recs, _, err := cf.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{
Name: hostname,
})
if err != nil {
return fmt.Errorf("failed to list DNS records: %w", err)
}
if len(recs) > 0 {
// update the record
_, err := cf.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.UpdateDNSRecordParams{
ID: recs[0].ID,
Type: "A",
Name: hostname,
Content: ip,
TTL: 1,
Proxied: cloudflare.BoolPtr(false),
})
if err != nil {
return fmt.Errorf("failed to update DNS record: %w", err)
}
} else {
_, err := cf.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.CreateDNSRecordParams{
Type: "A",
Name: hostname,
Content: ip,
TTL: 1,
Proxied: cloudflare.BoolPtr(false),
})
if err != nil {
return fmt.Errorf("failed to create DNS record: %w", err)
}
}
return nil
}
func DeleteRecord(cf *cloudflare.API, zoneID string, recordID string) error {
err := cf.DeleteDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), recordID)
if err != nil {
return fmt.Errorf("failed to delete DNS record: %w", err)
}
return nil
}