-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudflare.go
118 lines (90 loc) · 1.96 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
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
package main
import (
"context"
"net"
"github.com/cloudflare/cloudflare-go"
)
type CFConf struct {
API_KEY string `yaml:"CF_API_KEY"`
API_EMAIL string `yaml:"CF_API_EMAIL"`
}
func GetRecords(domain string, name string, recordType string) ([]cloudflare.DNSRecord, error) {
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
if err != nil {
return nil, err
}
ctx := context.Background()
_, err = api.UserDetails(ctx)
if err != nil {
return nil, err
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
return nil, err
}
host := cloudflare.DNSRecord{
Name: name,
Type: recordType,
}
records, err := api.DNSRecords(ctx, zoneID, host)
return records, err
}
func CreateRecord(domain string, name string, recordType string, dst net.IP) error {
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
if err != nil {
return err
}
ctx := context.Background()
_, err = api.UserDetails(ctx)
if err != nil {
return err
}
zoneID, err := api.ZoneIDByName(domain)
if err != nil {
return err
}
rec := cloudflare.DNSRecord{
Name: name,
Type: recordType,
Content: dst.String(),
}
_, err = api.CreateDNSRecord(ctx, zoneID, rec)
if err != nil {
return err
}
return nil
}
func UpdateRecord(record cloudflare.DNSRecord, dst net.IP) error {
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
if err != nil {
return err
}
ctx := context.Background()
_, err = api.UserDetails(ctx)
if err != nil {
return err
}
rec := record
rec.Content = dst.String()
err = api.UpdateDNSRecord(ctx, record.ZoneID, record.ID, rec)
if err != nil {
return err
}
return nil
}
func DeleteRecord(record cloudflare.DNSRecord) error {
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
if err != nil {
return err
}
ctx := context.Background()
_, err = api.UserDetails(ctx)
if err != nil {
return err
}
err = api.DeleteDNSRecord(ctx, record.ZoneID, record.ID)
if err != nil {
return err
}
return nil
}