2018-09-15 21:25:14 +00:00
|
|
|
package hostingde
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-26 14:25:34 +00:00
|
|
|
"context"
|
2018-12-06 21:50:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-02-26 14:25:34 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-12 16:16:46 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://secure.hosting.de/api/dns/v1/json"
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
// https://www.hosting.de/api/?json#list-zoneconfigs
|
|
|
|
func (d *DNSProvider) listZoneConfigs(findRequest ZoneConfigsFindRequest) (*ZoneConfigsFindResponse, error) {
|
|
|
|
uri := defaultBaseURL + "/zoneConfigsFind"
|
2018-09-15 21:25:14 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
findResponse := &ZoneConfigsFindResponse{}
|
2018-09-15 21:25:14 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
rawResp, err := d.post(uri, findRequest, findResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-15 21:25:14 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
if len(findResponse.Response.Data) == 0 {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", err, toUnreadableBodyMessage(uri, rawResp))
|
2019-02-26 14:25:34 +00:00
|
|
|
}
|
2018-09-15 21:25:14 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
if findResponse.Status != "success" && findResponse.Status != "pending" {
|
|
|
|
return findResponse, errors.New(toUnreadableBodyMessage(uri, rawResp))
|
|
|
|
}
|
2018-09-15 21:25:14 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
return findResponse, nil
|
2018-09-15 21:25:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
// https://www.hosting.de/api/?json#updating-zones
|
|
|
|
func (d *DNSProvider) updateZone(updateRequest ZoneUpdateRequest) (*ZoneUpdateResponse, error) {
|
|
|
|
uri := defaultBaseURL + "/zoneUpdate"
|
|
|
|
|
|
|
|
// but we'll need the ID later to delete the record
|
|
|
|
updateResponse := &ZoneUpdateResponse{}
|
|
|
|
|
|
|
|
rawResp, err := d.post(uri, updateRequest, updateResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateResponse.Status != "success" && updateResponse.Status != "pending" {
|
|
|
|
return nil, errors.New(toUnreadableBodyMessage(uri, rawResp))
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateResponse, nil
|
2018-09-15 21:25:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
var zoneConfig *ZoneConfig
|
|
|
|
|
|
|
|
operation := func() error {
|
|
|
|
findResponse, err := d.listZoneConfigs(findRequest)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if findResponse.Response.Data[0].Status != "active" {
|
|
|
|
return fmt.Errorf("unexpected status: %q", findResponse.Response.Data[0].Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
zoneConfig = &findResponse.Response.Data[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.InitialInterval = 3 * time.Second
|
|
|
|
bo.MaxInterval = 10 * bo.InitialInterval
|
|
|
|
bo.MaxElapsedTime = 100 * bo.InitialInterval
|
|
|
|
|
|
|
|
// retry in case the zone was edited recently and is not yet active
|
|
|
|
err := backoff.Retry(operation, backoff.WithContext(bo, ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return zoneConfig, nil
|
2018-09-15 21:25:14 +00:00
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
func (d *DNSProvider) post(uri string, request interface{}, response interface{}) ([]byte, error) {
|
|
|
|
body, err := json.Marshal(request)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(body))
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("error querying API: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-02-26 14:25:34 +00:00
|
|
|
return nil, errors.New(toUnreadableBodyMessage(uri, content))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
err = json.Unmarshal(content, response)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", err, toUnreadableBodyMessage(uri, content))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
return content, nil
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:25:34 +00:00
|
|
|
func toUnreadableBodyMessage(uri string, rawBody []byte) string {
|
|
|
|
return fmt.Sprintf("the request %s sent a response with a body which is an invalid format: %q", uri, string(rawBody))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|