go-easydns/zones.go
2018-11-15 17:11:56 -07:00

140 lines
3.9 KiB
Go

package easydns
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
type Record struct {
ID string `json:"id,omitempty"`
Domain string `json:"domain,omitempty"`
Host string `json:"host"`
TTL string `json:"ttl"`
Prio string `json:"prio,omitempty"`
Type string `json:"type"`
Rdata string `json:"rdata"`
GeozoneID string `json:"geozone_id,omitempty"`
LastMod string `json:"last_mod,omitempty"`
}
type RecordList struct {
Tm int `json:"tm"`
Data []Record `json:"data"`
Count int `json:"count"`
Total int `json:"total"`
Start int `json:"start"`
Max int `json:"max"`
Status int `json:"status"`
}
type EasyDNSClient struct {
ApiToken string
ApiKey string
BaseURL string
}
func NewClient(baseURL, apiToken, apiKey string) *EasyDNSClient {
client := &EasyDNSClient{}
client.ApiToken = apiToken
client.ApiKey = apiKey
client.BaseURL = baseURL
return client
}
func (client *EasyDNSClient) GetRecordList(domain string) (*RecordList, error) {
endpoint := fmt.Sprintf("%s/zones/records/all/%s?_key=%s&_user=%s&format=json", client.BaseURL, domain, client.ApiKey, client.ApiToken)
resp, err := http.Get(endpoint)
if err != nil {
return nil, fmt.Errorf("http request failed to '%s': %v", endpoint, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("expected HTTP status of '200', got '%s'", resp.Status)
}
recordList := &RecordList{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body to byte slice: %v", err)
}
err = json.Unmarshal(body, recordList)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response to RecordList: %v", err)
}
return recordList, nil
}
func (client *EasyDNSClient) CreateRecord(domain, host, recordType, rdata, ttl, prio string) error {
record := &Record{
Domain: domain,
Host: host,
TTL: ttl,
Prio: prio,
Type: recordType,
Rdata: rdata}
endpoint := fmt.Sprintf("%s/zones/records/add/%s/%s?_key=%s&_user=%s&format=json", client.BaseURL, domain, recordType, client.ApiKey, client.ApiToken)
body, err := json.Marshal(record)
if err != nil {
return fmt.Errorf("failed to marshal record to struct: %v", err)
}
err = putRequest(endpoint, bytes.NewBuffer(body))
if err != nil {
return fmt.Errorf("put request failed: %v", err)
}
return nil
}
func (client *EasyDNSClient) ModifyRecord(id, host, recordType, rdata, ttl string) error {
record := &Record{
Host: host,
TTL: ttl,
Type: recordType,
Rdata: rdata}
endpoint := fmt.Sprintf("%s/zones/records/%s?_key=%s&_user=%s&format=json", client.BaseURL, id, client.ApiKey, client.ApiToken)
body, err := json.Marshal(record)
if err != nil {
return fmt.Errorf("failed to marshal record to struct: %v", err)
}
_, err = http.Post(endpoint, "POST", bytes.NewBuffer(body))
if err != nil {
return fmt.Errorf("post request failed: %v", err)
}
return nil
}
func (client *EasyDNSClient) DeleteRecord(domain, id string) error {
endpoint := fmt.Sprintf("%s/zones/records/%s/%s?_key=%s&_user=%s&format=json", client.BaseURL, domain, id, client.ApiKey, client.ApiToken)
httpClient := &http.Client{}
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
if err != nil {
return fmt.Errorf("failed to create http delete request: %v", err)
}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("http delete request failed: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body to byte slice: %v", err)
}
fmt.Println(string(body))
return nil
}
func putRequest(url string, data io.Reader) error {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPut, url, data)
if err != nil {
fmt.Errorf("failed to create http put request: %v", err)
}
_, err = client.Do(req)
if err != nil {
fmt.Errorf("http put request failed: %v", err)
}
return nil
}