libdns-vultr/helpers.go

75 lines
1.4 KiB
Go
Raw Normal View History

2025-06-01 15:14:24 +02:00
package vultr
import (
"fmt"
"time"
"github.com/libdns/libdns"
"github.com/vultr/govultr/v3"
)
type VultrRecord struct {
Record libdns.RR
ID string
}
2025-06-01 15:14:24 +02:00
func (r VultrRecord) RR() libdns.RR {
return r.Record
}
2025-06-01 15:14:24 +02:00
// Converts a govultr.DomainRecord to libdns.Record
// Taken from libdns/digitalocean
func fromAPIRecord(r govultr.DomainRecord, zone string) VultrRecord {
name := libdns.RelativeName(r.Name, zone)
ttl := time.Duration(r.TTL) * time.Second
2025-06-01 15:14:24 +02:00
// Vultr uses a custom priority field for MX records
data := r.Data
if r.Type == "MX" {
data = fmt.Sprintf("%d %s", r.Priority, r.Data)
}
2025-06-01 15:14:24 +02:00
return VultrRecord{
Record: libdns.RR{
2025-06-01 15:14:24 +02:00
Name: name,
TTL: ttl,
Type: r.Type,
Data: data,
},
ID: r.ID,
}
}
// Converts a libdns.Record to VultrRecord with an optional ID
func fromLibdnsRecord(r libdns.Record, id string) VultrRecord {
rr := r.RR()
return VultrRecord{
Record: rr,
ID: id,
2025-06-01 15:14:24 +02:00
}
}
// Converts a libdns.Record to a govultr.DomainRecordReq
func toDomainRecordReq(r libdns.Record) govultr.DomainRecordReq {
rr := r.RR()
2025-06-01 15:14:24 +02:00
return govultr.DomainRecordReq{
Name: rr.Name,
Type: rr.Type,
TTL: int(rr.TTL.Seconds()),
Data: rr.Data,
}
2025-06-01 15:14:24 +02:00
}
func getRecordId(r libdns.Record) (string, error) {
var id string
if vr, err := r.(VultrRecord); err {
id = vr.ID
}
2025-06-01 15:14:24 +02:00
if id == "" {
return "", fmt.Errorf("record has no ID: %v", r)
2025-06-01 15:14:24 +02:00
}
return id, nil
2025-06-01 15:14:24 +02:00
}