Add removeRecord

This commit is contained in:
Steven Polley 2018-11-15 17:11:56 -07:00
parent 6e04f91b7f
commit 400163d63c

View File

@ -51,22 +51,18 @@ func (client *EasyDNSClient) GetRecordList(domain string) (*RecordList, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("http request failed to '%s': %v", endpoint, err) return nil, fmt.Errorf("http request failed to '%s': %v", endpoint, err)
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("expected HTTP status of '200', got '%s'", resp.Status) return nil, fmt.Errorf("expected HTTP status of '200', got '%s'", resp.Status)
} }
recordList := &RecordList{} recordList := &RecordList{}
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read response body to byte slice: %v", err) return nil, fmt.Errorf("failed to read response body to byte slice: %v", err)
} }
err = json.Unmarshal(body, recordList) err = json.Unmarshal(body, recordList)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to unmarshal response to RecordList: %v", err) return nil, fmt.Errorf("failed to unmarshal response to RecordList: %v", err)
} }
return recordList, nil return recordList, nil
} }
@ -78,9 +74,7 @@ func (client *EasyDNSClient) CreateRecord(domain, host, recordType, rdata, ttl,
Prio: prio, Prio: prio,
Type: recordType, Type: recordType,
Rdata: rdata} 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) 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) body, err := json.Marshal(record)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal record to struct: %v", err) 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 { if err != nil {
return fmt.Errorf("put request failed: %v", err) return fmt.Errorf("put request failed: %v", err)
} }
return nil return nil
} }
@ -99,18 +92,35 @@ func (client *EasyDNSClient) ModifyRecord(id, host, recordType, rdata, ttl strin
TTL: ttl, TTL: ttl,
Type: recordType, Type: recordType,
Rdata: rdata} Rdata: rdata}
endpoint := fmt.Sprintf("%s/zones/records/%s?_key=%s&_user=%s&format=json", client.BaseURL, id, client.ApiKey, client.ApiToken) 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) body, err := json.Marshal(record)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal record to struct: %v", err) return fmt.Errorf("failed to marshal record to struct: %v", err)
} }
_, err = http.Post(endpoint, "POST", bytes.NewBuffer(body)) _, err = http.Post(endpoint, "POST", bytes.NewBuffer(body))
if err != nil { if err != nil {
return fmt.Errorf("post request failed: %v", err) 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 return nil
} }