From 400163d63cbda7dc277e0fd0cd243da4c81510c3 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Thu, 15 Nov 2018 17:11:56 -0700 Subject: [PATCH] Add removeRecord --- zones.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/zones.go b/zones.go index c19aad0..b9decd6 100644 --- a/zones.go +++ b/zones.go @@ -51,22 +51,18 @@ func (client *EasyDNSClient) GetRecordList(domain string) (*RecordList, error) { 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 } @@ -78,9 +74,7 @@ func (client *EasyDNSClient) CreateRecord(domain, host, recordType, rdata, 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) @@ -89,7 +83,6 @@ func (client *EasyDNSClient) CreateRecord(domain, host, recordType, rdata, ttl, if err != nil { return fmt.Errorf("put request failed: %v", err) } - return nil } @@ -99,18 +92,35 @@ func (client *EasyDNSClient) ModifyRecord(id, host, recordType, rdata, ttl strin 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 }