From 6e04f91b7fde3e9f8d23bf8648b73e3355ee7cf7 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Tue, 13 Nov 2018 16:48:48 -0700 Subject: [PATCH] Initial commit, w/ example client. Testing and working --- example-client/main.go | 26 +++++++++ zones.go | 129 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 example-client/main.go create mode 100644 zones.go diff --git a/example-client/main.go b/example-client/main.go new file mode 100644 index 0000000..cc16f38 --- /dev/null +++ b/example-client/main.go @@ -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") + } + } +} diff --git a/zones.go b/zones.go new file mode 100644 index 0000000..c19aad0 --- /dev/null +++ b/zones.go @@ -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 +}