65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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 Gandi struct{}
|
|
|
|
func (g Gandi) IPSet(reqs []IPSetRequest) error {
|
|
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 (Gandi) EnsureAllDomainsAreSame(reqs []IPSetRequest) error {
|
|
first := reqs[0].Domain
|
|
for _, dom := range reqs {
|
|
if first != dom.Domain {
|
|
return fmt.Errorf("domain mismatch: %s != %s", first, dom.Domain)
|
|
}
|
|
}
|
|
return nil
|
|
}
|