-
Notifications
You must be signed in to change notification settings - Fork 1
/
gandiclient.go
184 lines (152 loc) · 4.79 KB
/
gandiclient.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"time"
)
const (
GandiLiveDnsBaseUrl = "https://api.gandi.net/v5/livedns"
)
type GandiClient struct {
apiKey string
dumpRequestResponse bool
}
type GandiRRSet struct {
Type string `json:"rrset_type"`
TTL int `json:"rrset_ttl"`
Name string `json:"rrset_name"`
Values []string `json:"rrset_values"`
}
type GandiRRSetValues struct {
TTL int `json:"rrset_ttl"`
Values []string `json:"rrset_values"`
}
func NewGandiClient(apiKey string) *GandiClient {
return &GandiClient{
apiKey: apiKey,
dumpRequestResponse: false,
}
}
func (c *GandiClient) gandiRecordsUrl(domain string) string {
return fmt.Sprintf("%s/domains/%s/records", GandiLiveDnsBaseUrl, domain)
}
func (c *GandiClient) doRequest(req *http.Request, readResponseBody bool) (int, []byte, error) {
if c.dumpRequestResponse {
dump, _ := httputil.DumpRequest(req, true)
fmt.Printf("Request: %q\n", dump)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
client := http.Client{
Timeout: 30 * time.Second,
}
res, err := client.Do(req)
if err != nil {
return 0, nil, err
}
if c.dumpRequestResponse {
dump, _ := httputil.DumpResponse(res, true)
fmt.Printf("Response: %q\n", dump)
}
if res.StatusCode == http.StatusOK && readResponseBody {
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, nil, err
}
return res.StatusCode, data, nil
}
return res.StatusCode, nil, nil
}
func (c *GandiClient) HasTxtRecord(domain *string, name *string) (bool, error) {
// curl -X GET -H "Content-Type: application/json" \
// -H "Authorization: Apikey $APIKEY" \
// https://api.gandi.net/v5/livedns/domains/<DOMAIN>/records/<NAME>/<TYPE>
url := fmt.Sprintf("%s/%s/TXT", c.gandiRecordsUrl(*domain), *name)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
}
status, _, err := c.doRequest(req, false)
if err != nil {
return false, err
}
if status == http.StatusNotFound {
return false, nil
} else if status == http.StatusOK {
// Maybe parse response body here to really ensure that the record is present
return true, nil
} else {
return false, fmt.Errorf("unexpected HTTP status: %d, err: %w", status, err)
}
}
func (c *GandiClient) CreateTxtRecord(domain *string, name *string, value *string, ttl int) error {
// curl -X POST -H "Content-Type: application/json" \
// -H "Authorization: Apikey $APIKEY" \
// -d '{"rrset_name": "<NAME>", "rrset_type": "<TYPE>", "rrset_ttl": 10800, "rrset_values": ["<VALUE>"]}' \
// https://api.gandi.net/v5/livedns/domains/<DOMAIN>/records
rrs := GandiRRSet{Name: *name, Type: "TXT", TTL: ttl, Values: []string{*value}}
body, err := json.Marshal(rrs)
if err != nil {
return fmt.Errorf("cannot marshall to json: %v", err)
}
url := c.gandiRecordsUrl(*domain)
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
status, body, err := c.doRequest(req, false)
if err != nil {
return err
}
if status != http.StatusCreated && status != http.StatusOK {
return fmt.Errorf("failed creating TXT record: %d, %s", status, string(body))
}
return nil
}
func (c *GandiClient) UpdateTxtRecord(domain *string, name *string, value *string, ttl int) error {
// curl -X PUT -H "Content-Type: application/json" \
// -H "Authorization: Apikey $APIKEY" \
// -d '{"rrset_ttl": 10800, "rrset_values":["<VALUE>"]}' \
// https://api.gandi.net/v5/livedns/domains/<DOMAIN>/records/<NAME>/<TYPE>
rrs := GandiRRSetValues{TTL: ttl, Values: []string{*value}}
body, err := json.Marshal(rrs)
if err != nil {
return fmt.Errorf("cannot marshall to json: %v", err)
}
url := fmt.Sprintf("%s/%s/TXT", c.gandiRecordsUrl(*domain), *name)
req, err := http.NewRequest("PUT", url, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
status, _, err := c.doRequest(req, false)
if err != nil {
return err
}
if status != http.StatusCreated && status != http.StatusOK {
return fmt.Errorf("failed updating TXT record: %v", err)
}
return nil
}
func (c *GandiClient) DeleteTxtRecord(domain *string, name *string) error {
// curl -X DELETE -H "Content-Type: application/json" \
// -H "Authorization: Apikey $APIKEY" \
// https://api.gandi.net/v5/livedns/domains/<DOMAIN>/records/<NAME>/<TYPE>
url := fmt.Sprintf("%s/%s/TXT", c.gandiRecordsUrl(*domain), *name)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
status, _, err := c.doRequest(req, false)
if err != nil {
return err
}
if status != http.StatusOK && status != http.StatusNoContent {
return fmt.Errorf("failed deleting TXT record: %v", err)
}
return nil
}