ncddns/providers/gandi/gandi.go

66 lines
1.4 KiB
Go
Raw Normal View History

package gandi
2022-08-01 00:19:30 +01:00
import (
"fmt"
"ncddns/mod"
2022-08-01 00:19:30 +01:00
"github.com/go-gandi/go-gandi"
"github.com/go-gandi/go-gandi/config"
"github.com/go-gandi/go-gandi/livedns"
)
var (
gandiEmailRecords = []livedns.DomainRecord{
{
RrsetType: "MX",
RrsetTTL: 10800,
RrsetName: "@",
RrsetValues: []string{"10 spool.mail.gandi.net.", "50 fb.mail.gandi.net."},
},
{
RrsetType: "TXT",
RrsetTTL: 10800,
RrsetName: "@",
RrsetValues: []string{`"v=spf1 include:_mailcust.gandi.net ?all"`},
},
}
)
type Client struct{}
2022-08-01 00:19:30 +01:00
func (g Client) IPSet(reqs []mod.IPSetRequest) error {
2022-08-01 00:19:30 +01:00
if len(reqs) == 0 {
return nil
}
if err := g.EnsureAllDomainsAreSame(reqs); err != nil {
return err
}
pwd := reqs[0].Password
domain := reqs[0].Domain
records := make([]livedns.DomainRecord, len(reqs))
for index, req := range reqs {
records[index] = livedns.DomainRecord{
RrsetType: "A",
RrsetTTL: 300,
RrsetName: req.Host,
RrsetValues: []string{req.IP},
}
}
records = append(records, gandiEmailRecords...)
_, err := gandi.NewLiveDNSClient(config.Config{
APIKey: pwd,
}).UpdateDomainRecords(domain, records)
return err
}
func (Client) EnsureAllDomainsAreSame(reqs []mod.IPSetRequest) error {
2022-08-01 00:19:30 +01:00
first := reqs[0].Domain
for _, dom := range reqs {
if first != dom.Domain {
return fmt.Errorf("domain mismatch: %s != %s", first, dom.Domain)
}
}
return nil
}