-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
139 lines (109 loc) · 3.82 KB
/
dns.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
package directadmin
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/spf13/cast"
)
type DNSRecord struct {
Name string `json:"name"`
Ttl int `json:"ttl"`
Type string `json:"type"`
Value string `json:"value"`
}
// CheckDNSRecordExists (user) checks if the given dns record exists on the server
//
// checkField can be either "name" or "value"
func (c *UserContext) CheckDNSRecordExists(checkField string, domain string, dnsRecord DNSRecord) error {
body := url.Values{
"check": {checkField},
"domain": {domain},
"name": {dnsRecord.Name},
"record": {dnsRecord.Type},
"type": {"dns"},
"value": {dnsRecord.Value},
}
if dnsRecord.Type == "MX" {
body.Set("mx_value", dnsRecord.Value)
}
return c.checkObjectExists(body)
}
// CreateDNSRecord (user) creates the provided dns record for the given domain
func (c *UserContext) CreateDNSRecord(domain string, dnsRecord DNSRecord) error {
var response apiGenericResponse
rawDNSRecordData := dnsRecord.translate()
body := url.Values{
"domain": {domain},
"name": {rawDNSRecordData.Name},
"ttl": {cast.ToString(rawDNSRecordData.Ttl)},
"type": {rawDNSRecordData.Type},
"value": {rawDNSRecordData.Value},
}
if _, err := c.makeRequestOld(http.MethodPost, "API_DNS_CONTROL?action=add&action_pointers=yes", body, &response); err != nil {
return err
}
if response.Success != "Record Added" {
return fmt.Errorf("failed to create dns record: %v", response.Result)
}
return nil
}
// DeleteDNSRecords (user) deletes all the specified dnss for the session user
func (c *UserContext) DeleteDNSRecords(dnsRecords ...DNSRecord) error {
var response apiGenericResponse
body := url.Values{}
dnsRecordMap := make(map[string][]string)
for _, dnsRecord := range dnsRecords {
dnsRecordMap[dnsRecord.Type] = append(dnsRecordMap[strings.ToLower(dnsRecord.Type)], fmt.Sprintf("name=%v&value=%v", dnsRecord.Name, dnsRecord.Value))
}
for dnsRecordType, dnsRecordData := range dnsRecordMap {
for index, data := range dnsRecordData {
body.Set(dnsRecordType+"recs"+cast.ToString(index), data)
}
}
if _, err := c.makeRequestOld(http.MethodPost, "API_DNS_CONTROL?action=select&delete=yes", body, &response); err != nil {
return err
}
if response.Success != "Records Deleted" {
return fmt.Errorf("failed to delete dns records: %v", response.Result)
}
return nil
}
// GetDNSRecords (user) returns the given domain's dns records
func (c *UserContext) GetDNSRecords(domain string) ([]DNSRecord, error) {
var dnsRecords []DNSRecord
rawDNSRecords := struct {
DNSRecords []rawDNSRecord `json:"records"`
}{}
if _, err := c.makeRequestOld(http.MethodGet, "API_DNS_CONTROL?domain="+domain, nil, &rawDNSRecords); err != nil {
return nil, err
}
for _, dnsRecord := range rawDNSRecords.DNSRecords {
dnsRecords = append(dnsRecords, dnsRecord.translate())
}
if len(dnsRecords) == 0 {
return nil, errors.New("no dns records were found")
}
return dnsRecords, nil
}
// UpdateDNSRecord (user) updates the given dns record for the given domain
func (c *UserContext) UpdateDNSRecord(domain string, originalDNSRecord DNSRecord, updatedDNSRecord DNSRecord) error {
var response apiGenericResponse
rawDNSRecordData := updatedDNSRecord.translate()
body := url.Values{
"domain": {domain},
"name": {rawDNSRecordData.Name},
"ttl": {cast.ToString(rawDNSRecordData.Ttl)},
"type": {rawDNSRecordData.Type},
"value": {rawDNSRecordData.Value},
strings.ToLower(originalDNSRecord.Type) + "recs0": {fmt.Sprintf("name=%v&value=%v", originalDNSRecord.Name, originalDNSRecord.Value)},
}
if _, err := c.makeRequestOld(http.MethodPost, "API_DNS_CONTROL?action=edit&action_pointers=yes", body, &response); err != nil {
return err
}
if response.Success != "Record Edited" {
return fmt.Errorf("failed to update dns record: %v", response.Result)
}
return nil
}