219 lines
4.2 KiB
Go
219 lines
4.2 KiB
Go
package hetzner
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.readonly.ch/bouzoure/gestion-dns/helpers"
|
|
)
|
|
|
|
type Record struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
Value string `json:"value"`
|
|
TTL int `json:"ttl"`
|
|
ZoneID string `json:"zone_id"`
|
|
Created string `json:"created"`
|
|
Modified string `json:"modified"`
|
|
}
|
|
|
|
type Records struct {
|
|
Records []Record `json:"records"`
|
|
}
|
|
|
|
type CreateRecordResponse struct {
|
|
Record Record `json:"record"`
|
|
}
|
|
|
|
type UpdateRecordResponse struct {
|
|
Record Record `json:"record"`
|
|
}
|
|
|
|
func GetRecords(zone *Zone) error {
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create client
|
|
client := &http.Client{}
|
|
|
|
// Create request
|
|
url := fmt.Sprintf("https://dns.hetzner.com/api/v1/records?zone_id=%s", zone.ID)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Headers
|
|
req.Header.Add("Auth-API-Token", config.Hetzner.ApiToken)
|
|
|
|
// Fetch Request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Read Response Body
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var records Records
|
|
err = json.Unmarshal(respBody, &records)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var recordsToKeep []Record
|
|
for _, record := range records.Records {
|
|
if strings.EqualFold(record.Type, "NS") {
|
|
continue
|
|
}
|
|
|
|
if strings.EqualFold(record.Type, "SOA") {
|
|
continue
|
|
}
|
|
|
|
if !strings.EqualFold(record.Type, "TXT") || len(record.Value) <= 255 {
|
|
record.Value = strings.TrimPrefix(record.Value, "\"")
|
|
record.Value = strings.TrimSuffix(record.Value, "\"")
|
|
}
|
|
|
|
recordsToKeep = append(recordsToKeep, record)
|
|
}
|
|
|
|
zone.Records = recordsToKeep
|
|
return nil
|
|
}
|
|
|
|
func CreateRecord(record *Record) (Record, error) {
|
|
var newRecord Record
|
|
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
payload, err := json.Marshal(record)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
body := bytes.NewBuffer(payload)
|
|
|
|
// Create client
|
|
client := &http.Client{}
|
|
|
|
// Create request
|
|
req, err := http.NewRequest("POST", "https://dns.hetzner.com/api/v1/records", body)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
// Headers
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Auth-API-Token", config.Hetzner.ApiToken)
|
|
|
|
// Fetch Request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
// Read Response Body
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
var createRecordResponse CreateRecordResponse
|
|
err = json.Unmarshal(respBody, &createRecordResponse)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
return createRecordResponse.Record, nil
|
|
}
|
|
|
|
func UpdateRecord(record *Record) (Record, error) {
|
|
var newRecord Record
|
|
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
payload, err := json.Marshal(record)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
body := bytes.NewBuffer(payload)
|
|
|
|
// Create client
|
|
client := &http.Client{}
|
|
|
|
// Create request
|
|
url := fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%s", record.ID)
|
|
req, err := http.NewRequest("PUT", url, body)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
// Headers
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Auth-API-Token", config.Hetzner.ApiToken)
|
|
|
|
// Fetch Request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
// Read Response Body
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
var updateRecordResponse UpdateRecordResponse
|
|
err = json.Unmarshal(respBody, &updateRecordResponse)
|
|
if err != nil {
|
|
return newRecord, err
|
|
}
|
|
|
|
return updateRecordResponse.Record, nil
|
|
}
|
|
|
|
func DeleteRecord(record *Record) error {
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create client
|
|
client := &http.Client{}
|
|
|
|
// Create request
|
|
url := fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%s", record.ID)
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Headers
|
|
req.Header.Add("Auth-API-Token", config.Hetzner.ApiToken)
|
|
|
|
// Fetch Request
|
|
_, err = client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|