dnscontrol/providers/namecheap/namecheap.go

72 lines
1.8 KiB
Go
Raw Normal View History

2016-08-23 08:31:50 +08:00
package namecheap
import (
"fmt"
"sort"
2016-08-23 08:31:50 +08:00
"strings"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/providers"
2017-01-12 22:31:32 +08:00
nc "github.com/billputer/go-namecheap"
2016-08-23 08:31:50 +08:00
)
type Namecheap struct {
ApiKey string
ApiUser string
client *nc.Client
}
func init() {
providers.RegisterRegistrarType("NAMECHEAP", newReg)
// NOTE(tlim): If in the future the DNS Service Provider is implemented,
// most likely it will require providers.CantUseNOPURGE.
2016-08-23 08:31:50 +08:00
}
func newReg(m map[string]string) (providers.Registrar, error) {
api := &Namecheap{}
api.ApiUser, api.ApiKey = m["apiuser"], m["apikey"]
if api.ApiKey == "" || api.ApiUser == "" {
return nil, fmt.Errorf("Namecheap apikey and apiuser must be provided.")
}
api.client = nc.NewClient(api.ApiUser, api.ApiKey, api.ApiUser)
// if BaseURL is specified in creds, use that url
BaseURL, ok := m["BaseURL"]
if ok {
api.client.BaseURL = BaseURL
}
2016-08-23 08:31:50 +08:00
return api, nil
}
func (n *Namecheap) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
info, err := n.client.DomainGetInfo(dc.Name)
if err != nil {
return nil, err
}
sort.Strings(info.DNSDetails.Nameservers)
2016-08-23 08:31:50 +08:00
found := strings.Join(info.DNSDetails.Nameservers, ",")
desiredNs := []string{}
2016-08-23 08:31:50 +08:00
for _, d := range dc.Nameservers {
desiredNs = append(desiredNs, d.Name)
2016-08-23 08:31:50 +08:00
}
sort.Strings(desiredNs)
desired := strings.Join(desiredNs, ",")
2016-08-23 08:31:50 +08:00
if found != desired {
parts := strings.SplitN(dc.Name, ".", 2)
sld, tld := parts[0], parts[1]
return []*models.Correction{
{
Msg: fmt.Sprintf("Change Nameservers from '%s' to '%s'", found, desired),
2016-08-23 08:31:50 +08:00
F: func() error {
_, err := n.client.DomainDNSSetCustom(sld, tld, desired)
if err != nil {
return err
}
return nil
}},
}, nil
}
return nil, nil
}