62 lines
1,020 B
Go
62 lines
1,020 B
Go
package hetzner
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
|
|
"git.readonly.ch/bouzoure/gestion-dns/helpers"
|
|
)
|
|
|
|
type Zone struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
TTL int `json:"ttl"`
|
|
NS []string `json:"ns"`
|
|
Records []Record `json:"-"`
|
|
}
|
|
|
|
type Zones struct {
|
|
Zones []Zone `json:"zones"`
|
|
}
|
|
|
|
func GetZones() ([]Zone, error) {
|
|
var zones Zones
|
|
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return zones.Zones, err
|
|
}
|
|
|
|
// Create client
|
|
client := &http.Client{}
|
|
|
|
// Create request
|
|
req, err := http.NewRequest(
|
|
"GET",
|
|
"https://dns.hetzner.com/api/v1/zones",
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return zones.Zones, err
|
|
}
|
|
|
|
// Headers
|
|
req.Header.Add("Auth-API-Token", config.Hetzner.ApiToken)
|
|
|
|
// Fetch Request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return zones.Zones, err
|
|
}
|
|
|
|
// Read Response Body
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return zones.Zones, err
|
|
}
|
|
|
|
err = json.Unmarshal(respBody, &zones)
|
|
|
|
return zones.Zones, err
|
|
}
|