-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
222 lines (187 loc) · 5.61 KB
/
client.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// libdns client for IONOS DNS API
package ionos
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const (
APIEndpoint = "https://api.hosting.ionos.com/dns/v1"
)
type getAllZonesResponse struct {
Zones []zoneDescriptor
}
type zoneDescriptor struct {
Name string `json:"name"`
ID string `json:"id"`
Type string `json:"type"`
}
type getZoneResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Records []zoneRecord `json:"records"`
}
type zoneRecord struct {
ID string `json:"id"`
Name string `json:"name"`
RootName string `json:"rootName"`
Type string `json:"type"`
Content string `json:"content"`
ChangeDate string `json:"changeDate"`
TTL int `json:"ttl"`
Prio int `json:"prio"`
Disabled bool `json:"disabled"`
}
type record struct {
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL *int `json:"ttl,omitempty"`
Prio int `json:"prio"`
Disabled bool `json:"disabled,omitempty"` // TODO default=true
}
// IONOS does not accept TTL values < 60, and returns status 400. If the
// TTL is 0, we leave the field empty, by setting the struct value to nil.
func ionosTTL(ttl float64) *int {
var intTTL *int
if ttl > 0 {
tmp := int(ttl)
intTTL = &tmp
}
return intTTL
}
func doRequest(token string, request *http.Request) ([]byte, error) {
request.Header.Add("X-API-Key", token)
request.Header.Add("Content-Type", "application/json")
client := &http.Client{} // no timeout set because request is w/ context
// fmt.Printf(">>> HTTP req: %+v\n\n", request)
response, err := client.Do(request)
// fmt.Printf("<<< HTTP res: %+v\n\n", response)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode >= 300 {
return nil, fmt.Errorf("%s (%d)", http.StatusText(response.StatusCode), response.StatusCode)
}
return io.ReadAll(response.Body)
}
// GET /v1/zones
func ionosGetAllZones(ctx context.Context, token string) (getAllZonesResponse, error) {
uri := fmt.Sprintf("%s/zones", APIEndpoint)
req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
data, err := doRequest(token, req)
if err != nil {
return getAllZonesResponse{}, err
}
// parse top-level JSON array
zones := make([]zoneDescriptor, 0)
err = json.Unmarshal(data, &zones)
return getAllZonesResponse{zones}, err
}
// ionosGetZone reads the contents of zone by it's IONOS zoneID, optionally filtering for
// a specific recordType and recordName.
// /v1/zones/{zoneId}
func ionosGetZone(ctx context.Context, token string, zoneID string, recordType, recordName string) (getZoneResponse, error) {
u, err := url.Parse(APIEndpoint)
if err != nil {
return getZoneResponse{}, err
}
u = u.JoinPath("zones", zoneID)
queryString := u.Query()
if recordType != "" {
queryString.Set("recordType", recordType)
}
if recordName != "" {
queryString.Set("recordName", recordName)
}
u.RawQuery = queryString.Encode()
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
data, err := doRequest(token, req)
var result getZoneResponse
if err != nil {
return result, err
}
err = json.Unmarshal(data, &result)
return result, err
}
// ionosFindRecordsInZone is a convenience function to search all records in the
// given zone for a record with the given name and type and returns this record
// on success
func ionosFindRecordsInZone(ctx context.Context, token string, zoneID, typ, name string) ([]zoneRecord, error) {
resp, err := ionosGetZone(ctx, token, zoneID, typ, name)
if err != nil {
return nil, err
}
if len(resp.Records) < 1 {
return nil, fmt.Errorf("record not found in zone")
}
return resp.Records, nil
}
// ionosDeleteRecord deletes the given record
// DELETE /v1/zones/{zoneId}/records/{recordId}
func ionosDeleteRecord(ctx context.Context, token string, zoneID, id string) error {
if id == "" {
return fmt.Errorf("no record id provided")
}
req, err := http.NewRequestWithContext(ctx, "DELETE",
fmt.Sprintf("%s/zones/%s/records/%s", APIEndpoint, zoneID, id), nil)
if err != nil {
return err
}
_, err = doRequest(token, req)
return err
}
// ionosCreateRecord creates a batch of DNS record in the given zone
// POST /v1/zones/{zoneId}/records
func ionosCreateRecords(
ctx context.Context,
token string,
zoneID string,
records []record) ([]zoneRecord, error) {
reqBuffer, err := json.Marshal(records)
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s/zones/%s/records", APIEndpoint, zoneID)
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(reqBuffer))
if err != nil {
return nil, err
}
// as result of the POST, a zoneRecord array is returned
res, err := doRequest(token, req)
if err != nil {
return nil, err
}
var zoneRecords []zoneRecord
if err = json.Unmarshal(res, &zoneRecords); err != nil {
return nil, err
}
return zoneRecords, nil
}
// ionosUpdateRecord updates the record with id `id` in the given zone
// TODO check TTL
// PUT /v1/zones/{zoneId}/records/{recordId}
func ionosUpdateRecord(ctx context.Context, token string, zoneID, id string, r record) error {
if id == "" {
return fmt.Errorf("no record id provided")
}
reqBuffer, err := json.Marshal(r)
if err != nil {
return fmt.Errorf("marshal record for update: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "PUT",
fmt.Sprintf("%s/zones/%s/records/%s", APIEndpoint, zoneID, id),
bytes.NewBuffer(reqBuffer))
if err != nil {
return err
}
// according to API doc, no response returned here
_, err = doRequest(token, req)
return err
}