-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.go
205 lines (165 loc) · 4.89 KB
/
record.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"k8s.io/klog/v2"
)
func (c *DNSProviderSolver) Txt(recordName, serviceName, zoneName, content string) error {
var record = &Request{
RrList: &RrList{
Rr: []*Rr{},
},
}
record.RrList.Rr = append(record.RrList.Rr, &Rr{
Name: recordName,
TTL: 60,
Type: "TXT",
Txt: &TxtRecord{
String: content}})
err := c.createRecord(record, serviceName, zoneName)
if err != nil {
return fmt.Errorf("failed to create new record: %w", err)
}
return nil
}
func (c *DNSProviderSolver) createRecord(request *Request, serviceName, zoneName string) error {
var zone Zone
accessToken := c.getAccessToken()
url := fmt.Sprintf(urlCreateRecord, serviceName, zoneName)
payload, err := xml.MarshalIndent(request, "", "")
if err != nil {
return fmt.Errorf("failed to marshal indent XML: %w", err)
}
bodyPayload := bytes.NewBuffer(payload)
req, err := http.NewRequest(http.MethodPut, url, bodyPayload)
if err != nil {
return fmt.Errorf("failed to generate request: %w", err)
}
header := fmt.Sprintf("Bearer %s", accessToken)
req.Header.Add("Authorization", header)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed to read body: %w", err)
}
err = xml.Unmarshal(body, &zone)
if err != nil {
return fmt.Errorf("failed to unmarshal body: %w", err)
}
rrID := zone.Data.Zone[0].RR[0].ID
if zone.Status != "success" {
return fmt.Errorf("failed to create record: status does not indicate success")
}
err = c.Commit(serviceName, zoneName)
if err != nil {
return fmt.Errorf("failed to commit the zone: %w", err)
}
klog.Infof("Record successfully added. rrID=%s", rrID)
return nil
}
func (c *DNSProviderSolver) getRecord(serviceName, zoneName, recordName string) (string, error) {
var zone Zone
accessToken := c.getAccessToken()
urlRecord := fmt.Sprintf(urlGetRecord, serviceName, zoneName)
req, err := http.NewRequest(http.MethodGet, urlRecord, nil)
if err != nil {
return "", fmt.Errorf("failed to generate request: %w", err)
}
header := fmt.Sprintf("Bearer %s", accessToken)
req.Header.Add("Authorization", header)
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("failed to read body: %w", err)
}
err = xml.Unmarshal(body, &zone)
if err != nil {
return "", fmt.Errorf("failed to unmarshal: %w", err)
}
var rrID string
for i := 0; i < len(zone.Data.Zone); i++ {
for j := 0; j < len(zone.Data.Zone[i].RR); j++ {
if zone.Data.Zone[i].RR[j].Name == recordName {
rrID = zone.Data.Zone[i].RR[j].ID
}
}
}
if rrID == "" {
return "", fmt.Errorf("record not found")
}
return rrID, nil
}
func (c *DNSProviderSolver) deleteRecord(serviceName, zoneName, rrID string) error {
var response Response
accessToken := c.getAccessToken()
url := fmt.Sprintf(urlDeleteRecord, serviceName, zoneName, rrID)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("failed to generate request: %w", err)
}
header := fmt.Sprintf("Bearer %s", accessToken)
req.Header.Add("Authorization", header)
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed to read body: %w", err)
}
err = xml.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("failed to unmarhal body: %w", err)
}
if response.Status != "success" {
return fmt.Errorf("return fmt.Errorf")
}
err = c.Commit(serviceName, zoneName)
if err != nil {
return fmt.Errorf("failed to commit the zone: %w", err)
}
klog.Infof("Record successfully deleted")
return nil
}
func (c *DNSProviderSolver) Commit(serviceName, zoneName string) error {
var response Response
accessToken := c.getAccessToken()
url := fmt.Sprintf(urlCommit, serviceName, zoneName)
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("failed to generate request: %w", err)
}
header := fmt.Sprintf("Bearer %s", accessToken)
req.Header.Add("Authorization", header)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed to read body: %w", err)
}
err = xml.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("failed to unmarhal body: %w", err)
}
if response.Status != "success" {
return fmt.Errorf("commit zone failed: %w", err)
}
return nil
}