2017-03-19 09:58:47 +08:00
|
|
|
package dnsimple
|
|
|
|
|
|
|
|
import (
|
2018-10-14 12:30:58 +08:00
|
|
|
"context"
|
2017-03-19 09:58:47 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
dnsimpleapi "github.com/dnsimple/dnsimple-go/dnsimple"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2020-01-28 23:42:31 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v2/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v2/providers"
|
|
|
|
"github.com/StackExchange/dnscontrol/v2/providers/diff"
|
2017-03-19 09:58:47 +08:00
|
|
|
)
|
|
|
|
|
2018-01-05 08:19:35 +08:00
|
|
|
var features = providers.DocumentationNotes{
|
|
|
|
providers.CanUseAlias: providers.Can(),
|
|
|
|
providers.CanUseCAA: providers.Can(),
|
|
|
|
providers.CanUsePTR: providers.Can(),
|
|
|
|
providers.CanUseSRV: providers.Can(),
|
|
|
|
providers.CanUseTLSA: providers.Cannot(),
|
2017-09-15 04:13:17 +08:00
|
|
|
providers.DocCreateDomains: providers.Cannot(),
|
2018-01-05 08:19:35 +08:00
|
|
|
providers.DocDualHost: providers.Cannot("DNSimple does not allow sufficient control over the apex NS records"),
|
2017-09-15 04:13:17 +08:00
|
|
|
providers.DocOfficiallySupported: providers.Cannot(),
|
2020-02-21 03:52:19 +08:00
|
|
|
providers.CanGetZones: providers.Can(),
|
2017-09-15 04:13:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
providers.RegisterRegistrarType("DNSIMPLE", newReg)
|
2018-01-05 08:19:35 +08:00
|
|
|
providers.RegisterDomainServiceProviderType("DNSIMPLE", newDsp, features)
|
2017-09-15 04:13:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-19 09:58:47 +08:00
|
|
|
const stateRegistered = "registered"
|
|
|
|
|
|
|
|
var defaultNameServerNames = []string{
|
|
|
|
"ns1.dnsimple.com",
|
|
|
|
"ns2.dnsimple.com",
|
|
|
|
"ns3.dnsimple.com",
|
|
|
|
"ns4.dnsimple.com",
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// DnsimpleApi is the handle for this provider.
|
2017-03-19 09:58:47 +08:00
|
|
|
type DnsimpleApi struct {
|
|
|
|
AccountToken string // The account access token
|
|
|
|
BaseURL string // An alternate base URI
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID string // Account id cache
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// GetNameservers returns the name servers for a domain.
|
2017-03-19 09:58:47 +08:00
|
|
|
func (c *DnsimpleApi) GetNameservers(domainName string) ([]*models.Nameserver, error) {
|
|
|
|
return models.StringsToNameservers(defaultNameServerNames), nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:59:18 +08:00
|
|
|
// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
|
|
|
|
func (client *DnsimpleApi) GetZoneRecords(domain string) (models.Records, error) {
|
2020-02-21 03:52:19 +08:00
|
|
|
records, err := client.getRecords(domain)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-21 03:52:19 +08:00
|
|
|
var cleanedRecords models.Records
|
2017-03-19 09:58:47 +08:00
|
|
|
for _, r := range records {
|
2020-02-21 03:52:19 +08:00
|
|
|
if r.Type == "SOA" {
|
2017-03-19 09:58:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.Name == "" {
|
|
|
|
r.Name = "@"
|
|
|
|
}
|
2019-11-15 00:25:20 +08:00
|
|
|
if r.Type == "CNAME" || r.Type == "MX" || r.Type == "ALIAS" {
|
2017-03-19 09:58:47 +08:00
|
|
|
r.Content += "."
|
|
|
|
}
|
2020-02-21 03:52:19 +08:00
|
|
|
// DNSimple adds TXT records that mirror the alias records.
|
|
|
|
// They manage them on ALIAS updates, so pretend they don't exist
|
2017-10-19 03:51:44 +08:00
|
|
|
if r.Type == "TXT" && strings.HasPrefix(r.Content, "ALIAS for ") {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-19 09:58:47 +08:00
|
|
|
rec := &models.RecordConfig{
|
2018-02-16 01:02:50 +08:00
|
|
|
TTL: uint32(r.TTL),
|
|
|
|
Original: r,
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2020-02-21 03:52:19 +08:00
|
|
|
rec.SetLabel(r.Name, domain)
|
2018-02-16 01:02:50 +08:00
|
|
|
switch rtype := r.Type; rtype {
|
2018-02-28 05:50:34 +08:00
|
|
|
case "ALIAS", "URL":
|
2018-02-27 22:38:39 +08:00
|
|
|
rec.Type = r.Type
|
|
|
|
rec.SetTarget(r.Content)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "MX":
|
|
|
|
if err := rec.SetTargetMX(uint16(r.Priority), r.Content); err != nil {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
panic(fmt.Errorf("unparsable record received from dnsimple: %w", err))
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
2018-10-14 12:30:13 +08:00
|
|
|
case "SRV":
|
2019-11-15 00:25:20 +08:00
|
|
|
parts := strings.Fields(r.Content)
|
|
|
|
if len(parts) == 3 {
|
|
|
|
r.Content += "."
|
|
|
|
}
|
2018-10-14 12:30:13 +08:00
|
|
|
if err := rec.SetTargetSRVPriorityString(uint16(r.Priority), r.Content); err != nil {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
panic(fmt.Errorf("unparsable record received from dnsimple: %w", err))
|
2018-10-14 12:30:13 +08:00
|
|
|
}
|
2018-02-16 01:02:50 +08:00
|
|
|
default:
|
2020-02-21 03:52:19 +08:00
|
|
|
if err := rec.PopulateFromString(r.Type, r.Content, domain); err != nil {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
panic(fmt.Errorf("unparsable record received from dnsimple: %w", err))
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
2017-10-19 03:51:44 +08:00
|
|
|
}
|
2020-02-21 03:52:19 +08:00
|
|
|
cleanedRecords = append(cleanedRecords, rec)
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2020-02-21 03:52:19 +08:00
|
|
|
|
|
|
|
return cleanedRecords, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDomainCorrections returns corrections that update a domain.
|
|
|
|
func (c *DnsimpleApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
|
|
|
corrections := []*models.Correction{}
|
|
|
|
err := dc.Punycode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err := c.GetZoneRecords(dc.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
actual := removeNS(records)
|
2017-03-19 09:58:47 +08:00
|
|
|
removeOtherNS(dc)
|
2017-11-08 06:12:17 +08:00
|
|
|
|
|
|
|
// Normalize
|
2018-01-05 08:19:35 +08:00
|
|
|
models.PostProcessRecords(actual)
|
2017-11-08 06:12:17 +08:00
|
|
|
|
2017-03-19 09:58:47 +08:00
|
|
|
differ := diff.New(dc)
|
2018-10-14 12:30:13 +08:00
|
|
|
_, create, del, modify := differ.IncrementalDiff(actual)
|
2017-03-19 09:58:47 +08:00
|
|
|
|
2018-10-14 12:30:13 +08:00
|
|
|
for _, del := range del {
|
2017-03-19 09:58:47 +08:00
|
|
|
rec := del.Existing.Original.(dnsimpleapi.ZoneRecord)
|
|
|
|
corrections = append(corrections, &models.Correction{
|
|
|
|
Msg: del.String(),
|
|
|
|
F: c.deleteRecordFunc(rec.ID, dc.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cre := range create {
|
|
|
|
rec := cre.Desired
|
|
|
|
corrections = append(corrections, &models.Correction{
|
|
|
|
Msg: cre.String(),
|
|
|
|
F: c.createRecordFunc(rec, dc.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mod := range modify {
|
|
|
|
old := mod.Existing.Original.(dnsimpleapi.ZoneRecord)
|
2018-10-14 12:30:13 +08:00
|
|
|
rec := mod.Desired
|
2017-03-19 09:58:47 +08:00
|
|
|
corrections = append(corrections, &models.Correction{
|
|
|
|
Msg: mod.String(),
|
2018-10-14 12:30:13 +08:00
|
|
|
F: c.updateRecordFunc(&old, rec, dc.Name),
|
2017-03-19 09:58:47 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return corrections, nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 03:52:19 +08:00
|
|
|
func removeNS(records models.Records) models.Records {
|
|
|
|
var noNameServers models.Records
|
|
|
|
for _, r := range records {
|
|
|
|
if r.Type != "NS" {
|
|
|
|
noNameServers = append(noNameServers, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noNameServers
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// GetRegistrarCorrections returns corrections that update a domain's registrar.
|
2017-03-19 09:58:47 +08:00
|
|
|
func (c *DnsimpleApi) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
|
|
|
corrections := []*models.Correction{}
|
|
|
|
|
|
|
|
nameServers, err := c.getNameservers(dc.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-13 22:41:55 +08:00
|
|
|
sort.Strings(nameServers)
|
2017-03-19 09:58:47 +08:00
|
|
|
|
|
|
|
actual := strings.Join(nameServers, ",")
|
|
|
|
|
|
|
|
expectedSet := []string{}
|
|
|
|
for _, ns := range dc.Nameservers {
|
|
|
|
expectedSet = append(expectedSet, ns.Name)
|
|
|
|
}
|
|
|
|
sort.Strings(expectedSet)
|
|
|
|
expected := strings.Join(expectedSet, ",")
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
return []*models.Correction{
|
|
|
|
{
|
|
|
|
Msg: fmt.Sprintf("Update nameservers %s -> %s", actual, expected),
|
|
|
|
F: c.updateNameserversFunc(expectedSet, dc.Name),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return corrections, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSimple calls
|
|
|
|
|
|
|
|
func (c *DnsimpleApi) getClient() *dnsimpleapi.Client {
|
2018-10-14 12:30:58 +08:00
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.AccountToken})
|
|
|
|
tc := oauth2.NewClient(context.Background(), ts)
|
|
|
|
|
|
|
|
// new client
|
|
|
|
client := dnsimpleapi.NewClient(tc)
|
|
|
|
|
2017-03-19 09:58:47 +08:00
|
|
|
if c.BaseURL != "" {
|
|
|
|
client.BaseURL = c.BaseURL
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
func (c *DnsimpleApi) getAccountID() (string, error) {
|
|
|
|
if c.accountID == "" {
|
2017-03-19 09:58:47 +08:00
|
|
|
client := c.getClient()
|
|
|
|
whoamiResponse, err := client.Identity.Whoami()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if whoamiResponse.Data.User != nil && whoamiResponse.Data.Account == nil {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
return "", fmt.Errorf("DNSimple token appears to be a user token. Please supply an account token")
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2018-10-14 12:30:58 +08:00
|
|
|
c.accountID = strconv.FormatInt(whoamiResponse.Data.Account.ID, 10)
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2018-01-10 01:53:16 +08:00
|
|
|
return c.accountID, nil
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DnsimpleApi) getRecords(domainName string) ([]dnsimpleapi.ZoneRecord, error) {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-19 01:53:44 +08:00
|
|
|
opts := &dnsimpleapi.ZoneRecordListOptions{}
|
|
|
|
recs := []dnsimpleapi.ZoneRecord{}
|
|
|
|
opts.Page = 1
|
|
|
|
for {
|
2018-01-10 01:53:16 +08:00
|
|
|
recordsResponse, err := client.Zones.ListRecords(accountID, domainName, opts)
|
2017-10-19 01:53:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
recs = append(recs, recordsResponse.Data...)
|
|
|
|
pg := recordsResponse.Pagination
|
|
|
|
if pg.CurrentPage == pg.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
opts.Page++
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
2017-10-19 01:53:44 +08:00
|
|
|
return recs, nil
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the name server names that should be used. If the domain is registered
|
|
|
|
// then this method will return the delegation name servers. If this domain
|
|
|
|
// is hosted only, then it will return the default DNSimple name servers.
|
|
|
|
func (c *DnsimpleApi) getNameservers(domainName string) ([]string, error) {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
domainResponse, err := client.Domains.GetDomain(accountID, domainName)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if domainResponse.Data.State == stateRegistered {
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
delegationResponse, err := client.Registrar.GetDomainDelegation(accountID, domainName)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *delegationResponse.Data, nil
|
|
|
|
}
|
2018-01-10 01:53:16 +08:00
|
|
|
return defaultNameServerNames, nil
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function that can be invoked to change the delegation of the domain to the given name server names.
|
|
|
|
func (c *DnsimpleApi) updateNameserversFunc(nameServerNames []string, domainName string) func() error {
|
|
|
|
return func() error {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nameServers := dnsimpleapi.Delegation(nameServerNames)
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
_, err = client.Registrar.ChangeDomainDelegation(accountID, domainName, &nameServers)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function that can be invoked to create a record in a zone.
|
|
|
|
func (c *DnsimpleApi) createRecordFunc(rc *models.RecordConfig, domainName string) func() error {
|
|
|
|
return func() error {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
record := dnsimpleapi.ZoneRecord{
|
2018-03-20 05:18:58 +08:00
|
|
|
Name: rc.GetLabel(),
|
2017-03-19 09:58:47 +08:00
|
|
|
Type: rc.Type,
|
2018-10-14 12:30:13 +08:00
|
|
|
Content: getTargetRecordContent(rc),
|
2017-03-19 09:58:47 +08:00
|
|
|
TTL: int(rc.TTL),
|
2018-10-14 12:30:13 +08:00
|
|
|
Priority: getTargetRecordPriority(rc),
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2018-01-10 01:53:16 +08:00
|
|
|
_, err = client.Zones.CreateRecord(accountID, domainName, record)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function that can be invoked to delete a record in a zone.
|
2018-10-14 12:30:58 +08:00
|
|
|
func (c *DnsimpleApi) deleteRecordFunc(recordID int64, domainName string) func() error {
|
2017-03-19 09:58:47 +08:00
|
|
|
return func() error {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
_, err = client.Zones.DeleteRecord(accountID, domainName, recordID)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a function that can be invoked to update a record in a zone.
|
|
|
|
func (c *DnsimpleApi) updateRecordFunc(old *dnsimpleapi.ZoneRecord, rc *models.RecordConfig, domainName string) func() error {
|
|
|
|
return func() error {
|
|
|
|
client := c.getClient()
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
accountID, err := c.getAccountID()
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := dnsimpleapi.ZoneRecord{
|
2018-03-20 05:18:58 +08:00
|
|
|
Name: rc.GetLabel(),
|
2017-03-19 09:58:47 +08:00
|
|
|
Type: rc.Type,
|
2018-10-14 12:30:13 +08:00
|
|
|
Content: getTargetRecordContent(rc),
|
2017-03-19 09:58:47 +08:00
|
|
|
TTL: int(rc.TTL),
|
2018-10-14 12:30:13 +08:00
|
|
|
Priority: getTargetRecordPriority(rc),
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
_, err = client.Zones.UpdateRecord(accountID, domainName, old.ID, record)
|
2017-03-19 09:58:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 04:07:34 +08:00
|
|
|
// ListZones returns all the zones in an account
|
2020-02-21 03:52:19 +08:00
|
|
|
func (c *DnsimpleApi) ListZones() ([]string, error) {
|
|
|
|
client := c.getClient()
|
|
|
|
accountID, err := c.getAccountID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zones []string
|
|
|
|
opts := &dnsimpleapi.ZoneListOptions{}
|
|
|
|
opts.Page = 1
|
|
|
|
for {
|
|
|
|
zonesResponse, err := client.Zones.ListZones(accountID, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, zone := range zonesResponse.Data {
|
|
|
|
zones = append(zones, zone.Name)
|
|
|
|
}
|
|
|
|
pg := zonesResponse.Pagination
|
|
|
|
if pg.CurrentPage == pg.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
opts.Page++
|
|
|
|
}
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
2017-03-19 09:58:47 +08:00
|
|
|
// constructors
|
|
|
|
|
|
|
|
func newReg(conf map[string]string) (providers.Registrar, error) {
|
|
|
|
return newProvider(conf, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDsp(conf map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
|
|
|
|
return newProvider(conf, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProvider(m map[string]string, metadata json.RawMessage) (*DnsimpleApi, error) {
|
|
|
|
api := &DnsimpleApi{}
|
|
|
|
api.AccountToken = m["token"]
|
|
|
|
if api.AccountToken == "" {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
return nil, fmt.Errorf("missing DNSimple token")
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if m["baseurl"] != "" {
|
|
|
|
api.BaseURL = m["baseurl"]
|
|
|
|
}
|
|
|
|
|
|
|
|
return api, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all non-dnsimple NS records from our desired state.
|
|
|
|
// if any are found, print a warning
|
|
|
|
func removeOtherNS(dc *models.DomainConfig) {
|
|
|
|
newList := make([]*models.RecordConfig, 0, len(dc.Records))
|
|
|
|
for _, rec := range dc.Records {
|
2017-03-21 11:28:43 +08:00
|
|
|
if rec.Type == "NS" {
|
|
|
|
// apex NS inside dnsimple are expected.
|
2018-03-20 05:18:58 +08:00
|
|
|
if rec.GetLabelFQDN() == dc.Name && strings.HasSuffix(rec.GetTargetField(), ".dnsimple.com.") {
|
2017-03-21 11:28:43 +08:00
|
|
|
continue
|
2017-03-19 09:58:47 +08:00
|
|
|
}
|
2018-02-16 01:02:50 +08:00
|
|
|
fmt.Printf("Warning: dnsimple.com does not allow NS records to be modified. %s will not be added.\n", rec.GetTargetField())
|
2017-03-19 09:58:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newList = append(newList, rec)
|
|
|
|
}
|
|
|
|
dc.Records = newList
|
|
|
|
}
|
2018-10-14 12:30:13 +08:00
|
|
|
|
|
|
|
// Return the correct combined content for all special record types, Target for everything else
|
|
|
|
// Using RecordConfig.GetTargetCombined returns priority in the string, which we do not allow
|
|
|
|
func getTargetRecordContent(rc *models.RecordConfig) string {
|
|
|
|
switch rtype := rc.Type; rtype {
|
|
|
|
case "CAA":
|
|
|
|
return rc.GetTargetCombined()
|
|
|
|
case "SRV":
|
|
|
|
return fmt.Sprintf("%d %d %s", rc.SrvWeight, rc.SrvPort, rc.GetTargetField())
|
|
|
|
default:
|
|
|
|
return rc.GetTargetField()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the correct priority for the record type, 0 for records without priority
|
|
|
|
func getTargetRecordPriority(rc *models.RecordConfig) int {
|
|
|
|
switch rtype := rc.Type; rtype {
|
|
|
|
case "MX":
|
|
|
|
return int(rc.MxPreference)
|
|
|
|
case "SRV":
|
|
|
|
return int(rc.SrvPriority)
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|