Initial commit, w/ example client. Testing and working
This commit is contained in:
parent
dc9dc9d23f
commit
6e04f91b7f
26
example-client/main.go
Normal file
26
example-client/main.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
easydns "deadbeef.codes/steven/goeasydns"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
client := easydns.NewClient("https://sandbox.rest.easydns.net", "APITOKEN", "APIKEY")
|
||||||
|
recordList, err := client.GetRecordList("nextdigital.ca")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to get records for nextdigital.ca: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, record := range recordList.Data {
|
||||||
|
if record.Type == "TXT" {
|
||||||
|
fmt.Printf("ID: %s\n", record.ID)
|
||||||
|
fmt.Printf("Host: %s\n", record.Host)
|
||||||
|
fmt.Printf("Type: %s\n", record.Type)
|
||||||
|
fmt.Printf("rdata: %s\n", record.Rdata)
|
||||||
|
fmt.Printf("\n\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
129
zones.go
Normal file
129
zones.go
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
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 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user