2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2018-09-08 12:08:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-02-27 18:14:46 +00:00
|
|
|
"errors"
|
2018-09-08 12:08:07 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2018-09-08 12:08:07 +00:00
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// defaultBaseURL for reaching the jSON-based API-Endpoint of netcup.
|
2018-09-15 17:07:24 +00:00
|
|
|
const defaultBaseURL = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
|
2018-09-08 12:08:07 +00:00
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// success response status.
|
2018-09-08 12:08:07 +00:00
|
|
|
const success = "success"
|
|
|
|
|
|
|
|
// Request wrapper as specified in netcup wiki
|
2020-05-08 17:35:25 +00:00
|
|
|
// needed for every request to netcup API around *Msg.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://www.netcup-wiki.de/wiki/CCP_API#Anmerkungen_zu_JSON-Requests
|
|
|
|
type Request struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Param interface{} `json:"param"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// LoginRequest as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#login
|
2018-10-23 00:01:13 +00:00
|
|
|
type LoginRequest struct {
|
2018-09-08 12:08:07 +00:00
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APIPassword string `json:"apipassword"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// LogoutRequest as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#logout
|
2018-10-23 00:01:13 +00:00
|
|
|
type LogoutRequest struct {
|
2018-09-08 12:08:07 +00:00
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// UpdateDNSRecordsRequest as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#updateDnsRecords
|
2018-10-23 00:01:13 +00:00
|
|
|
type UpdateDNSRecordsRequest struct {
|
2018-09-08 12:08:07 +00:00
|
|
|
DomainName string `json:"domainname"`
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
DNSRecordSet DNSRecordSet `json:"dnsrecordset"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSRecordSet as specified in netcup WSDL.
|
2020-11-24 08:38:11 +00:00
|
|
|
// needed in UpdateDNSRecordsRequest.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Dnsrecordset
|
|
|
|
type DNSRecordSet struct {
|
|
|
|
DNSRecords []DNSRecord `json:"dnsrecords"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// InfoDNSRecordsRequest as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#infoDnsRecords
|
2018-10-23 00:01:13 +00:00
|
|
|
type InfoDNSRecordsRequest struct {
|
2018-09-08 12:08:07 +00:00
|
|
|
DomainName string `json:"domainname"`
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSRecord as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Dnsrecord
|
|
|
|
type DNSRecord struct {
|
|
|
|
ID int `json:"id,string,omitempty"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
RecordType string `json:"type"`
|
|
|
|
Priority string `json:"priority,omitempty"`
|
|
|
|
Destination string `json:"destination"`
|
|
|
|
DeleteRecord bool `json:"deleterecord,omitempty"`
|
|
|
|
State string `json:"state,omitempty"`
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL int `json:"ttl,omitempty"`
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// ResponseMsg as specified in netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Responsemessage
|
|
|
|
type ResponseMsg struct {
|
2018-10-23 00:01:13 +00:00
|
|
|
ServerRequestID string `json:"serverrequestid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
StatusCode int `json:"statuscode"`
|
|
|
|
ShortMessage string `json:"shortmessage"`
|
|
|
|
LongMessage string `json:"longmessage"`
|
|
|
|
ResponseData json.RawMessage `json:"responsedata,omitempty"`
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
func (r *ResponseMsg) Error() string {
|
|
|
|
return fmt.Sprintf("an error occurred during the action %s: [Status=%s, StatusCode=%d, ShortMessage=%s, LongMessage=%s]",
|
|
|
|
r.Action, r.Status, r.StatusCode, r.ShortMessage, r.LongMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginResponse response to login action.
|
|
|
|
type LoginResponse struct {
|
|
|
|
APISessionID string `json:"apisessionid"`
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
// InfoDNSRecordsResponse response to infoDnsRecords action.
|
|
|
|
type InfoDNSRecordsResponse struct {
|
2018-09-08 12:08:07 +00:00
|
|
|
APISessionID string `json:"apisessionid"`
|
2018-10-23 00:01:13 +00:00
|
|
|
DNSRecords []DNSRecord `json:"dnsrecords,omitempty"`
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Client netcup DNS client.
|
2018-09-08 12:08:07 +00:00
|
|
|
type Client struct {
|
|
|
|
customerNumber string
|
|
|
|
apiKey string
|
|
|
|
apiPassword string
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient *http.Client
|
|
|
|
BaseURL string
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewClient creates a netcup DNS client.
|
2020-07-09 23:48:18 +00:00
|
|
|
func NewClient(customerNumber, apiKey, apiPassword string) (*Client, error) {
|
2018-10-23 00:01:13 +00:00
|
|
|
if customerNumber == "" || apiKey == "" || apiPassword == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("credentials missing")
|
2018-10-23 00:01:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 12:08:07 +00:00
|
|
|
return &Client{
|
|
|
|
customerNumber: customerNumber,
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiPassword: apiPassword,
|
2018-09-15 17:07:24 +00:00
|
|
|
BaseURL: defaultBaseURL,
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
},
|
2018-10-23 00:01:13 +00:00
|
|
|
}, nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login performs the login as specified by the netcup WSDL
|
2020-05-08 17:35:25 +00:00
|
|
|
// returns sessionID needed to perform remaining actions.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) Login() (string, error) {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "login",
|
2018-10-23 00:01:13 +00:00
|
|
|
Param: &LoginRequest{
|
2018-09-08 12:08:07 +00:00
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APIPassword: c.apiPassword,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
var responseData LoginResponse
|
|
|
|
err := c.doRequest(payload, &responseData)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return "", fmt.Errorf("loging error: %w", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
return responseData.APISessionID, nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Logout performs the logout with the supplied sessionID as specified by the netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) Logout(sessionID string) error {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "logout",
|
2018-10-23 00:01:13 +00:00
|
|
|
Param: &LogoutRequest{
|
2018-09-08 12:08:07 +00:00
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: sessionID,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
err := c.doRequest(payload, nil)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("logout error: %w", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// UpdateDNSRecord performs an update of the DNSRecords as specified by the netcup WSDL.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
2018-10-23 00:01:13 +00:00
|
|
|
func (c *Client) UpdateDNSRecord(sessionID, domainName string, records []DNSRecord) error {
|
2018-09-08 12:08:07 +00:00
|
|
|
payload := &Request{
|
|
|
|
Action: "updateDnsRecords",
|
2018-10-23 00:01:13 +00:00
|
|
|
Param: UpdateDNSRecordsRequest{
|
2018-09-08 12:08:07 +00:00
|
|
|
DomainName: domainName,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: sessionID,
|
|
|
|
ClientRequestID: "",
|
2018-10-23 00:01:13 +00:00
|
|
|
DNSRecordSet: DNSRecordSet{DNSRecords: records},
|
2018-09-08 12:08:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
err := c.doRequest(payload, nil)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("error when sending the request: %w", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSRecords retrieves all dns records of an DNS-Zone as specified by the netcup WSDL
|
2020-05-08 17:35:25 +00:00
|
|
|
// returns an array of DNSRecords.
|
2018-09-08 12:08:07 +00:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) GetDNSRecords(hostname, apiSessionID string) ([]DNSRecord, error) {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "infoDnsRecords",
|
2018-10-23 00:01:13 +00:00
|
|
|
Param: InfoDNSRecordsRequest{
|
2018-09-08 12:08:07 +00:00
|
|
|
DomainName: hostname,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: apiSessionID,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
var responseData InfoDNSRecordsResponse
|
|
|
|
err := c.doRequest(payload, &responseData)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("error when sending the request: %w", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
return responseData.DNSRecords, nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
// doRequest marshals given body to JSON, send the request to netcup API
|
2020-05-08 17:35:25 +00:00
|
|
|
// and returns body of response.
|
2020-07-09 23:48:18 +00:00
|
|
|
func (c *Client) doRequest(payload, responseData interface{}) error {
|
2018-09-08 12:08:07 +00:00
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
2018-10-23 00:01:13 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, c.BaseURL, bytes.NewReader(body))
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2018-10-23 00:01:13 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
req.Close = true
|
2018-09-08 12:08:07 +00:00
|
|
|
req.Header.Set("content-type", "application/json")
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2018-10-23 00:01:13 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
if err = checkResponse(resp); err != nil {
|
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
respMsg, err := decodeResponseMsg(resp)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2018-10-23 00:01:13 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
if respMsg.Status != success {
|
|
|
|
return respMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseData != nil {
|
|
|
|
err = json.Unmarshal(respMsg.ResponseData, responseData)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("%v: unmarshaling %T error: %w: %s",
|
2018-10-23 00:01:13 +00:00
|
|
|
respMsg, responseData, err, string(respMsg.ResponseData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
func checkResponse(resp *http.Response) error {
|
|
|
|
if resp.StatusCode > 299 {
|
|
|
|
if resp.Body == nil {
|
|
|
|
return fmt.Errorf("response body is nil, status code=%d", resp.StatusCode)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
2018-10-23 00:01:13 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("unable to read body: status code=%d, error=%w", resp.StatusCode, err)
|
2018-10-23 00:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("status code=%d: %s", resp.StatusCode, string(raw))
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
2018-10-23 00:01:13 +00:00
|
|
|
|
|
|
|
return nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:01:13 +00:00
|
|
|
func decodeResponseMsg(resp *http.Response) (*ResponseMsg, error) {
|
|
|
|
if resp.Body == nil {
|
|
|
|
return nil, fmt.Errorf("response body is nil, status code=%d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("unable to read body: status code=%d, error=%w", resp.StatusCode, err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
2018-10-23 00:01:13 +00:00
|
|
|
|
|
|
|
var respMsg ResponseMsg
|
|
|
|
err = json.Unmarshal(raw, &respMsg)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("unmarshaling %T error [status code=%d]: %w: %s", respMsg, resp.StatusCode, err, string(raw))
|
2018-10-23 00:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &respMsg, nil
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
// GetDNSRecordIdx searches a given array of DNSRecords for a given DNSRecord
|
|
|
|
// equivalence is determined by Destination and RecortType attributes
|
2020-05-08 17:35:25 +00:00
|
|
|
// returns index of given DNSRecord in given array of DNSRecords.
|
2018-12-06 21:50:17 +00:00
|
|
|
func GetDNSRecordIdx(records []DNSRecord, record DNSRecord) (int, error) {
|
|
|
|
for index, element := range records {
|
|
|
|
if record.Destination == element.Destination && record.RecordType == element.RecordType {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 18:14:46 +00:00
|
|
|
return -1, errors.New("no DNS Record found")
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|