lego/providers/dns/netcup/internal/client.go

328 lines
9.5 KiB
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package internal
2018-09-08 12:08:07 +00:00
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
2018-09-08 12:08:07 +00:00
)
// defaultBaseURL for reaching the jSON-based API-Endpoint of netcup
const defaultBaseURL = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
2018-09-08 12:08:07 +00:00
// success response status
const success = "success"
// Request wrapper as specified in netcup wiki
// needed for every request to netcup API around *Msg
// https://www.netcup-wiki.de/wiki/CCP_API#Anmerkungen_zu_JSON-Requests
type Request struct {
Action string `json:"action"`
Param interface{} `json:"param"`
}
// LoginRequest as specified in netcup WSDL
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#login
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"`
}
// LogoutRequest as specified in netcup WSDL
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#logout
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"`
}
// UpdateDNSRecordsRequest as specified in netcup WSDL
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#updateDnsRecords
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"`
}
// DNSRecordSet as specified in netcup WSDL
// 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"`
}
// InfoDNSRecordsRequest as specified in netcup WSDL
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#infoDnsRecords
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"`
}
// DNSRecord as specified in netcup WSDL
// 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"`
TTL int `json:"ttl,omitempty"`
2018-09-08 12:08:07 +00:00
}
// ResponseMsg as specified in netcup WSDL
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Responsemessage
type ResponseMsg struct {
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
}
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
}
// InfoDNSRecordsResponse response to infoDnsRecords action.
type InfoDNSRecordsResponse struct {
2018-09-08 12:08:07 +00:00
APISessionID string `json:"apisessionid"`
DNSRecords []DNSRecord `json:"dnsrecords,omitempty"`
2018-09-08 12:08:07 +00:00
}
// Client netcup DNS client
type Client struct {
customerNumber string
apiKey string
apiPassword string
HTTPClient *http.Client
BaseURL string
2018-09-08 12:08:07 +00:00
}
// NewClient creates a netcup DNS client
func NewClient(customerNumber string, apiKey string, apiPassword string) (*Client, error) {
if customerNumber == "" || apiKey == "" || apiPassword == "" {
return nil, fmt.Errorf("credentials missing")
}
2018-09-08 12:08:07 +00:00
return &Client{
customerNumber: customerNumber,
apiKey: apiKey,
apiPassword: apiPassword,
BaseURL: defaultBaseURL,
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
}, nil
2018-09-08 12:08:07 +00:00
}
// Login performs the login as specified by the netcup WSDL
// returns sessionID needed to perform remaining actions
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
func (c *Client) Login() (string, error) {
payload := &Request{
Action: "login",
Param: &LoginRequest{
2018-09-08 12:08:07 +00:00
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
APIPassword: c.apiPassword,
ClientRequestID: "",
},
}
var responseData LoginResponse
err := c.doRequest(payload, &responseData)
2018-09-08 12:08:07 +00:00
if err != nil {
return "", fmt.Errorf("loging error: %v", err)
2018-09-08 12:08:07 +00:00
}
return responseData.APISessionID, nil
2018-09-08 12:08:07 +00:00
}
// Logout performs the logout with the supplied sessionID as specified by the netcup WSDL
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
func (c *Client) Logout(sessionID string) error {
payload := &Request{
Action: "logout",
Param: &LogoutRequest{
2018-09-08 12:08:07 +00:00
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
APISessionID: sessionID,
ClientRequestID: "",
},
}
err := c.doRequest(payload, nil)
2018-09-08 12:08:07 +00:00
if err != nil {
return fmt.Errorf("logout error: %v", err)
2018-09-08 12:08:07 +00:00
}
return nil
}
// UpdateDNSRecord performs an update of the DNSRecords as specified by the netcup WSDL
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
func (c *Client) UpdateDNSRecord(sessionID, domainName string, records []DNSRecord) error {
2018-09-08 12:08:07 +00:00
payload := &Request{
Action: "updateDnsRecords",
Param: UpdateDNSRecordsRequest{
2018-09-08 12:08:07 +00:00
DomainName: domainName,
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
APISessionID: sessionID,
ClientRequestID: "",
DNSRecordSet: DNSRecordSet{DNSRecords: records},
2018-09-08 12:08:07 +00:00
},
}
err := c.doRequest(payload, nil)
2018-09-08 12:08:07 +00:00
if err != nil {
return fmt.Errorf("error when sending the request: %v", 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
// returns an array of DNSRecords
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
func (c *Client) GetDNSRecords(hostname, apiSessionID string) ([]DNSRecord, error) {
payload := &Request{
Action: "infoDnsRecords",
Param: InfoDNSRecordsRequest{
2018-09-08 12:08:07 +00:00
DomainName: hostname,
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
APISessionID: apiSessionID,
ClientRequestID: "",
},
}
var responseData InfoDNSRecordsResponse
err := c.doRequest(payload, &responseData)
2018-09-08 12:08:07 +00:00
if err != nil {
return nil, fmt.Errorf("error when sending the request: %v", err)
2018-09-08 12:08:07 +00:00
}
return responseData.DNSRecords, nil
2018-09-08 12:08:07 +00:00
}
// doRequest marshals given body to JSON, send the request to netcup API
2018-09-08 12:08:07 +00:00
// and returns body of response
func (c *Client) doRequest(payload interface{}, responseData interface{}) error {
2018-09-08 12:08:07 +00:00
body, err := json.Marshal(payload)
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
req, err := http.NewRequest(http.MethodPost, c.BaseURL, bytes.NewReader(body))
2018-09-08 12:08:07 +00:00
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
req.Close = true
2018-09-08 12:08:07 +00:00
req.Header.Set("content-type", "application/json")
resp, err := c.HTTPClient.Do(req)
2018-09-08 12:08:07 +00:00
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
if err = checkResponse(resp); err != nil {
return err
2018-09-08 12:08:07 +00:00
}
respMsg, err := decodeResponseMsg(resp)
2018-09-08 12:08:07 +00:00
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
if respMsg.Status != success {
return respMsg
}
if responseData != nil {
err = json.Unmarshal(respMsg.ResponseData, responseData)
if err != nil {
return fmt.Errorf("%v: unmarshaling %T error: %v: %s",
respMsg, responseData, err, string(respMsg.ResponseData))
}
}
return nil
2018-09-08 12:08:07 +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
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unable to read body: status code=%d, error=%v", resp.StatusCode, err)
}
return fmt.Errorf("status code=%d: %s", resp.StatusCode, string(raw))
2018-09-08 12:08:07 +00:00
}
return nil
2018-09-08 12:08:07 +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 {
return nil, fmt.Errorf("unable to read body: status code=%d, error=%v", resp.StatusCode, err)
2018-09-08 12:08:07 +00:00
}
var respMsg ResponseMsg
err = json.Unmarshal(raw, &respMsg)
if err != nil {
return nil, fmt.Errorf("unmarshaling %T error [status code=%d]: %v: %s", respMsg, resp.StatusCode, err, string(raw))
}
return &respMsg, nil
2018-09-08 12:08:07 +00:00
}
// GetDNSRecordIdx searches a given array of DNSRecords for a given DNSRecord
// equivalence is determined by Destination and RecortType attributes
// returns index of given DNSRecord in given array of DNSRecords
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
}
}
return -1, fmt.Errorf("no DNS Record found")
}