-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudflare.go
141 lines (122 loc) · 2.86 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type dnsRecord struct {
id string
content string
}
type DNSRecords struct {
name string
a dnsRecord
aaaa dnsRecord
}
func setAuthHeader(req *http.Request, apiKey string) {
authHeader := fmt.Sprint("bearer ", apiKey)
req.Header.Add("Authorization", authHeader)
}
func GetDNSRecord(zoneID string, domainName string, apiKey string) DNSRecords {
dnsRecords := DNSRecords{
name: domainName,
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records?name=%s", zoneID, domainName)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Panic("Error creating the request: ", err)
}
setAuthHeader(req, apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Panic("Error loading the response: ", err)
}
defer resp.Body.Close()
var data struct {
Success bool
Errors []struct {
Message string
}
Result []struct {
ID string
Content string
Type string
}
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
log.Panic("Error parsing JSON: ", err)
}
if !data.Success {
msg := ""
for i, err := range data.Errors {
if i != 0 {
msg += ", "
}
msg += err.Message
}
log.Panic("Server responded with error: ", msg)
}
for _, record := range data.Result {
switch record.Type {
case "A":
dnsRecords.a = dnsRecord{id: record.ID, content: record.Content}
case "AAAA":
dnsRecords.aaaa = dnsRecord{id: record.ID, content: record.Content}
}
}
return dnsRecords
}
type DNSRecordBody struct {
Content string
Name string
Type string
}
func UpdateDNSRecord(zoneID string, dnsRecordID string, apiKey string, body DNSRecordBody) {
var method string
var url string
if dnsRecordID == "" {
method = http.MethodPost
url = fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%v/dns_records", zoneID)
} else {
method = http.MethodPatch
url = fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%v/dns_records/%v", zoneID, dnsRecordID)
}
encodedBody, err := json.Marshal(&body)
if err != nil {
log.Panic("Error parsing the json body: ", err)
}
req, err := http.NewRequest(method, url, bytes.NewReader(encodedBody))
if err != nil {
log.Panic("Error creating the request: ", err)
}
setAuthHeader(req, apiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Panic("Error loading the response: ", err)
}
defer resp.Body.Close()
var data struct {
Success bool
Errors []struct {
Message string
}
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
log.Fatal("Error parsing JSON: ", err)
}
if !data.Success {
msg := ""
for i, err := range data.Errors {
if i != 0 {
msg += ", "
}
msg += err.Message
}
log.Panic("Server responded with error: ", msg)
}
}