2024-04-03 09:05:12 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-04-09 13:16:19 +00:00
|
|
|
"encoding/json"
|
2024-04-09 13:53:12 +00:00
|
|
|
"errors"
|
2024-04-09 02:16:06 +00:00
|
|
|
"strings"
|
2024-04-03 09:05:12 +00:00
|
|
|
|
|
|
|
dns "github.com/cloud66-oss/coredns_mysql"
|
|
|
|
)
|
|
|
|
|
2024-04-09 13:53:12 +00:00
|
|
|
var ErrorZoneNotEndWithDot = errors.New("zone should end with '.'")
|
|
|
|
|
2024-04-03 09:05:12 +00:00
|
|
|
const (
|
|
|
|
RecordTypeA = "A"
|
|
|
|
RecordTypeAAAA = "AAAA"
|
|
|
|
RecordTypeCNAME = "CNAME"
|
|
|
|
RecordTypeSOA = "SOA"
|
|
|
|
RecordTypeTXT = "TXT"
|
|
|
|
RecordTypeNS = "NS"
|
|
|
|
RecordTypeMX = "MX"
|
|
|
|
RecordTypeCAA = "CAA"
|
|
|
|
RecordTypeSRV = "SRV"
|
|
|
|
)
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
type RecordContentDefault any
|
|
|
|
|
|
|
|
type recordContentTypes interface {
|
|
|
|
dns.ARecord | dns.AAAARecord | dns.CNAMERecord | dns.CAARecord | dns.NSRecord | dns.MXRecord | dns.SOARecord | dns.SRVRecord | dns.TXTRecord | RecordContentDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
type Record[T recordContentTypes] struct {
|
2024-04-03 09:05:12 +00:00
|
|
|
ID int `gorm:"primaryKey" json:"id"`
|
|
|
|
Zone string `gorm:"not null,size:255" json:"zone"`
|
|
|
|
Name string `gorm:"not null,size:255" json:"name"`
|
|
|
|
Ttl int `json:"ttl"`
|
2024-04-09 13:16:19 +00:00
|
|
|
Content T `gorm:"serializer:json,type:\"text\"" json:"content"`
|
2024-04-03 09:05:12 +00:00
|
|
|
RecordType string `gorm:"not null,size:255" json:"record_type"`
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (Record[T]) TableName() string {
|
2024-04-03 09:05:12 +00:00
|
|
|
return "coredns_record"
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (r Record[T]) CheckZone() error {
|
2024-04-09 02:16:06 +00:00
|
|
|
if strings.HasSuffix(r.Zone, ".") {
|
2024-04-09 13:53:12 +00:00
|
|
|
return ErrorZoneNotEndWithDot
|
2024-04-09 02:16:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (r Record[T]) WithOutDotTail() string {
|
2024-04-09 08:28:18 +00:00
|
|
|
return strings.TrimRight(r.Zone, ".")
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (r Record[T]) ToEntity() IRecord {
|
|
|
|
return &r
|
2024-04-03 09:05:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (r *Record[T]) FromEntity(entity any) error {
|
|
|
|
b, err := json.Marshal(entity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-03 09:05:12 +00:00
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
return json.Unmarshal(b, r)
|
2024-04-03 09:05:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func (r Record[T]) GetType() string {
|
|
|
|
return r.RecordType
|
|
|
|
}
|
2024-04-03 09:05:12 +00:00
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
type IRecord interface {
|
|
|
|
TableName() string
|
|
|
|
CheckZone() error
|
|
|
|
WithOutDotTail() string
|
|
|
|
ToEntity() IRecord
|
|
|
|
FromEntity(any) error
|
|
|
|
GetType() string
|
2024-04-03 09:05:12 +00:00
|
|
|
}
|