2018-08-30 20:54:42 +08:00
|
|
|
package hexonet
|
|
|
|
|
2020-10-12 23:45:44 +08:00
|
|
|
import "fmt"
|
|
|
|
|
2023-02-07 20:22:49 +08:00
|
|
|
// EnsureZoneExists returns an error
|
2018-08-30 20:54:42 +08:00
|
|
|
// * if access to dnszone is not allowed (not authorized) or
|
|
|
|
// * if it doesn't exist and creating it fails
|
2023-02-07 20:22:49 +08:00
|
|
|
func (n *HXClient) EnsureZoneExists(domain string) error {
|
2020-10-12 23:45:44 +08:00
|
|
|
r := n.client.Request(map[string]interface{}{
|
2018-08-30 20:54:42 +08:00
|
|
|
"COMMAND": "StatusDNSZone",
|
|
|
|
"DNSZONE": domain + ".",
|
|
|
|
})
|
2020-02-28 02:04:17 +08:00
|
|
|
code := r.GetCode()
|
2018-08-30 20:54:42 +08:00
|
|
|
if code == 545 {
|
2020-10-12 23:45:44 +08:00
|
|
|
r = n.client.Request(map[string]interface{}{
|
2018-08-30 20:54:42 +08:00
|
|
|
"COMMAND": "CreateDNSZone",
|
|
|
|
"DNSZONE": domain + ".",
|
|
|
|
})
|
|
|
|
if !r.IsSuccess() {
|
2023-02-07 20:22:49 +08:00
|
|
|
return n.GetHXApiError("Failed to create not existing zone ", domain, r)
|
2018-08-30 20:54:42 +08:00
|
|
|
}
|
|
|
|
} else if code == 531 {
|
|
|
|
return n.GetHXApiError("Not authorized to manage dnszone", domain, r)
|
2022-01-28 04:58:56 +08:00
|
|
|
} else if r.IsError() || r.IsTmpError() {
|
2018-08-30 20:54:42 +08:00
|
|
|
return n.GetHXApiError("Error while checking status of dnszone", domain, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-12 23:45:44 +08:00
|
|
|
|
|
|
|
// ListZones lists all the
|
|
|
|
func (n *HXClient) ListZones() ([]string, error) {
|
|
|
|
var zones []string
|
|
|
|
|
|
|
|
// Basic
|
|
|
|
|
|
|
|
rs := n.client.RequestAllResponsePages(map[string]string{
|
|
|
|
"COMMAND": "QueryDNSZoneList",
|
|
|
|
})
|
|
|
|
for _, r := range rs {
|
|
|
|
if r.IsError() {
|
|
|
|
return nil, n.GetHXApiError("Error while QueryDNSZoneList", "Basic", &r)
|
|
|
|
}
|
|
|
|
zoneColumn := r.GetColumn("DNSZONE")
|
|
|
|
if zoneColumn == nil {
|
|
|
|
return nil, fmt.Errorf("failed getting DNSZONE BASIC column")
|
|
|
|
}
|
|
|
|
zones = append(zones, zoneColumn.GetData()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Premium
|
|
|
|
|
|
|
|
rs = n.client.RequestAllResponsePages(map[string]string{
|
|
|
|
"COMMAND": "QueryDNSZoneList",
|
|
|
|
"PROPERTIES": "PREMIUMDNS",
|
|
|
|
"PREMIUMDNSCLASS": "*",
|
|
|
|
})
|
|
|
|
for _, r := range rs {
|
|
|
|
if r.IsError() {
|
|
|
|
return nil, n.GetHXApiError("Error while QueryDNSZoneList", "Basic", &r)
|
|
|
|
}
|
|
|
|
zoneColumn := r.GetColumn("DNSZONE")
|
|
|
|
if zoneColumn == nil {
|
|
|
|
return nil, fmt.Errorf("failed getting DNSZONE PREMIUM column")
|
|
|
|
}
|
|
|
|
zones = append(zones, zoneColumn.GetData()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zones, nil
|
|
|
|
}
|