forked from cert-manager/webhook-example
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinfomaniak_api.go
267 lines (222 loc) · 8.77 KB
/
infomaniak_api.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"k8s.io/klog/v2"
)
const (
infomaniakBaseURL = "https://api.infomaniak.com"
)
// InfomaniakAPI is a basic implementation of an API client to api.infomaniak.com
// It implements only the methods required for the ACME Challenge
type InfomaniakAPI struct {
apiToken string
}
// ErrorResponse defines the error response format, as described here https://api.infomaniak.com/doc#home
type ErrorResponse struct {
Code string `json:"code"`
Description string `json:"description,omitempty"`
Context map[string]string `json:"context,omitempty"`
Errors []ErrorResponse `json:"errors,omitempty"`
}
// InfomaniakAPIResponse defines the generic response format, as described here https://api.infomaniak.com/doc#home
type InfomaniakAPIResponse struct {
Result string `json:"result"`
Data *json.RawMessage `json:"data,omitempty"`
ErrResponse ErrorResponse `json:"error,omitempty"`
}
// NewInfomaniakAPI creates a new infomaniak API client
func NewInfomaniakAPI(apiToken string) *InfomaniakAPI {
return &InfomaniakAPI{
apiToken: apiToken,
}
}
// request builds the raw request
func (ik *InfomaniakAPI) request(method, path string, body io.Reader) (*InfomaniakAPIResponse, error) {
if path[0] != '/' {
path = "/" + path
}
url := infomaniakBaseURL + path
client := &http.Client{}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+ik.apiToken)
req.Header.Set("Content-Type", "application/json")
klog.V(6).Infof("%s %s", method, url)
rawResp, err := client.Do(req)
if err != nil {
return nil, err
}
var resp InfomaniakAPIResponse
if err := json.NewDecoder(rawResp.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("%s %s response parsing error: %v", method, path, err)
}
rawJSON, _ := json.Marshal(resp)
klog.V(8).Infof("Response status: `%s` json response: `%v`", rawResp.Status, string(rawJSON))
if resp.Result != "success" {
return nil, fmt.Errorf("%s %s failed: %v", method, path, resp.ErrResponse)
}
return &resp, nil
}
// get is a helper to build a bare GET request
func (ik *InfomaniakAPI) get(path string, params url.Values) (*InfomaniakAPIResponse, error) {
base, err := url.Parse(path)
if err != nil {
return nil, err
}
if params != nil {
base.RawQuery = params.Encode()
}
return ik.request("GET", base.String(), nil)
}
// get is a helper to build a bare POST request
func (ik *InfomaniakAPI) post(path string, body io.Reader) (*InfomaniakAPIResponse, error) {
return ik.request("POST", path, body)
}
// get is a helper to build a bare PUT request
func (ik *InfomaniakAPI) put(path string, body io.Reader) (*InfomaniakAPIResponse, error) {
return ik.request("PUT", path, body)
}
// get is a helper to build a bare DELETE request
func (ik *InfomaniakAPI) delete(path string) (*InfomaniakAPIResponse, error) {
return ik.request("DELETE", path, nil)
}
// InfomaniakDNSDomain defines the format of a Domain object
type InfomaniakDNSDomain struct {
ID uint64 `json:"id,omitempty"`
AccountID uint64 `json:"account_id,omitempty"`
ServiceID uint64 `json:"service_id,omitempty"`
ServiceName string `json:"service_name,omitempty"`
CustomerName string `json:"customer_name,omitempty"`
InternalName string `json:"internal_name,omitempty,omitempty"`
CreatedAt uint64 `json:"created_at,omitempty"`
ExpiredAt uint64 `json:"expired_at,omitempty"`
Version uint64 `json:"version,omitempty"`
Maintenance bool `json:"maintenance,omitempty"`
Locked bool `json:"locked,omitempty"`
OperationInProgress bool `json:"operation_in_progress,omitempty"`
Tags *json.RawMessage `json:"tags,omitempty"`
UniqueID uint64 `json:"unique_id,omitempty"`
Description string `json:"description,omitempty"`
Isfree bool `json:"is_free,omitempty"`
Rights *json.RawMessage `json:"rights,omitempty"`
Special bool `json:"special,omitempty"`
}
// InfomaniakDNSRecord defines the format of a DNSRecord object
type InfomaniakDNSRecord struct {
ID string `json:"id,omitempty"`
Source string `json:"source,omitempty"`
SourceIdn string `json:"source_idn,omitempty"`
Type string `json:"type,omitempty"`
TTL uint64 `json:"ttl,omitempty"`
TTLIdn string `json:"ttl_idn,omitempty"`
Target string `json:"target,omitempty"`
TargetIdn string `json:"target_idn,omitempty"`
UpdatedAt uint64 `json:"updated_at,omitempty"`
DyndnsID string `json:"dyndns_id,omitempty,omitempty"`
Priority uint64 `json:"priority,omitempty"`
IsEditable bool `json:"is_editable,omitempty"`
}
// ErrDomainNotFound
var ErrDomainNotFound = errors.New("domain not found")
// GetDomainByName gather a Domain object from its name
func (ik *InfomaniakAPI) GetDomainByName(name string) (*InfomaniakDNSDomain, error) {
klog.V(4).Infof("Getting domain matching `%s`", name)
// remove trailing . if present
if strings.HasSuffix(name, ".") {
name = name[:len(name)-1]
}
// Try to find the most specific domain
// starts with the FQDN, then remove each left label until we have a match
for {
i := strings.Index(name, ".")
if i == -1 {
break
}
params := url.Values{}
params.Add("service_name", "domain")
params.Add("customer_name", name)
resp, err := ik.get("/1/product", params)
if err != nil {
return nil, err
}
var domains []InfomaniakDNSDomain
if err = json.Unmarshal(*resp.Data, &domains); err != nil {
return nil, fmt.Errorf("expected array of Domain, got: %v", string(*resp.Data))
}
for _, domain := range domains {
if domain.CustomerName == name {
klog.V(4).Infof("Domain `%s` found, id=`%d`", name, domain.ID)
return &domain, nil
}
}
klog.V(4).Infof("Domain `%s` not found, trying with `%s`", name, name[i+1:])
name = name[i+1:]
}
return nil, ErrDomainNotFound
}
// getRecordID gather a record id from its specs (domain, source, target, rtype)
func (ik *InfomaniakAPI) getRecordID(domain *InfomaniakDNSDomain, source, target, rtype string) (*string, error) {
klog.V(4).Infof("Getting all record for domain=%d, then match source=%s target=%s rtype=%s", domain.ID, source, target, rtype)
resp, err := ik.get(fmt.Sprintf("/1/domain/%d/dns/record", domain.ID), nil)
if err != nil {
return nil, err
}
var records []InfomaniakDNSRecord
if err = json.Unmarshal(*resp.Data, &records); err != nil {
return nil, fmt.Errorf("expected array of Record, got: %v", string(*resp.Data))
}
if len(records) < 1 {
return nil, fmt.Errorf("no records in zone")
}
for _, record := range records {
if record.Source == source && record.Target == target && record.Type == rtype {
return &record.ID, nil
}
}
return nil, nil
}
// EnsureDNSRecord ensures a record is present with the correct key
func (ik *InfomaniakAPI) EnsureDNSRecord(domain *InfomaniakDNSDomain, source, target, rtype string, ttl uint64) error {
klog.V(4).Infof("Ensure record domain=%d source=%s target=%s rtype=%s TTL=%d", domain.ID, source, target, rtype, ttl)
recordID, err := ik.getRecordID(domain, source, target, rtype)
if err != nil {
return err
}
if recordID != nil {
klog.V(4).Infof("Record already exists (domain=%d record=%s source=%s rtype=%s target=%s), skipping addition", domain.ID, *recordID, source, rtype, target)
return nil
}
record := InfomaniakDNSRecord{Source: source, Target: target, Type: rtype, TTL: ttl}
rawJSON, err := json.Marshal(record)
if err != nil {
return err
}
klog.V(4).Infof("Adding record domain=%d (source=%s rtype=%s target=%s ttl=%d)", domain.ID, source, rtype, target, ttl)
_, err = ik.post(fmt.Sprintf("/1/domain/%d/dns/record", domain.ID), bytes.NewBuffer(rawJSON))
return err
}
// RemoveDNSRecord ensures a record is absent
func (ik *InfomaniakAPI) RemoveDNSRecord(domain *InfomaniakDNSDomain, source, target, rtype string) error {
klog.V(4).Infof("Remove record domain=%d source=%s rtype=%s target=%s", domain.ID, source, rtype, target)
recordID, err := ik.getRecordID(domain, source, target, rtype)
if err != nil {
return err
}
// the record is already absent doing nothing
if recordID == nil || len(*recordID) < 1 {
klog.V(4).Infof("No record found (domain=%d source=%s rtype=%s target=%s), skipping deletion", domain.ID, source, rtype, target)
return nil
}
klog.V(4).Infof("Deleting record domain=%d record=%s (source=%s rtype=%s target=%s)", domain.ID, *recordID, source, rtype, target)
_, err = ik.delete(fmt.Sprintf("/1/domain/%d/dns/record/%s", domain.ID, *recordID))
return err
}