|
|
@ -1,29 +1,44 @@
|
|
|
|
package dns
|
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/dnsimple/dnsimple-go/dnsimple"
|
|
|
|
"github.com/dnsimple/dnsimple-go/dnsimple"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Client holds metadata for the dns api client
|
|
|
|
// Client holds metadata for the dns api client
|
|
|
|
type Client struct {
|
|
|
|
type Client struct {
|
|
|
|
client *dnsimple.Client
|
|
|
|
client *dnsimple.Client
|
|
|
|
accountID string
|
|
|
|
accountID int64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Record is the type used for a Zone record
|
|
|
|
// Record is the type used for a Zone record
|
|
|
|
type Record dnsimple.ZoneRecord
|
|
|
|
type Record dnsimple.ZoneRecord
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Option type
|
|
|
|
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithOAuth returns Option for oauth
|
|
|
|
|
|
|
|
func WithOAuth(oauthToken string) Option {
|
|
|
|
|
|
|
|
return func(c *Client) {
|
|
|
|
|
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: oauthToken})
|
|
|
|
|
|
|
|
oauthClient := oauth2.NewClient(context.Background(), ts)
|
|
|
|
|
|
|
|
client := dnsimple.NewClient(oauthClient)
|
|
|
|
|
|
|
|
c.client = client
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// New returns an implementation of a DNS interface
|
|
|
|
// New returns an implementation of a DNS interface
|
|
|
|
func New(oauthToken string) *Client {
|
|
|
|
func New(options ...Option) *Client {
|
|
|
|
credentials := dnsimple.NewOauthTokenCredentials(oauthToken)
|
|
|
|
c := &Client{}
|
|
|
|
client := dnsimple.NewClient(credentials)
|
|
|
|
for _, option := range options {
|
|
|
|
return &Client{
|
|
|
|
option(c)
|
|
|
|
client: client,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetRecords returns all record for a host
|
|
|
|
// GetRecords returns all record for a host
|
|
|
@ -38,7 +53,11 @@ func (d *Client) GetRecords(host, kind string) ([]Record, error) {
|
|
|
|
return nil, errors.Wrap(err, "unable to parse host")
|
|
|
|
return nil, errors.Wrap(err, "unable to parse host")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
records, err := d.client.Zones.ListRecords(accountID, domain, &dnsimple.ZoneRecordListOptions{})
|
|
|
|
records, err := d.client.Zones.ListRecords(
|
|
|
|
|
|
|
|
fmt.Sprintf("%d", accountID),
|
|
|
|
|
|
|
|
domain,
|
|
|
|
|
|
|
|
&dnsimple.ZoneRecordListOptions{},
|
|
|
|
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get list of records")
|
|
|
|
return nil, errors.Wrap(err, "could not get list of records")
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,7 +85,8 @@ func (d *Client) CreateRecord(host string, record Record) error {
|
|
|
|
|
|
|
|
|
|
|
|
newRecord := dnsimple.ZoneRecord(record)
|
|
|
|
newRecord := dnsimple.ZoneRecord(record)
|
|
|
|
newRecord.Name = subdomain
|
|
|
|
newRecord.Name = subdomain
|
|
|
|
if _, err := d.client.Zones.CreateRecord(accountID, domain, newRecord); err != nil {
|
|
|
|
aID := fmt.Sprintf("%d", accountID)
|
|
|
|
|
|
|
|
if _, err := d.client.Zones.CreateRecord(aID, domain, newRecord); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create record")
|
|
|
|
return errors.Wrap(err, "could not create record")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
@ -79,7 +99,8 @@ func (d *Client) UpdateRecord(record Record) error {
|
|
|
|
return errors.Wrap(err, "unable to get account id")
|
|
|
|
return errors.Wrap(err, "unable to get account id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := d.client.Zones.UpdateRecord(accountID, record.ZoneID, record.ID, dnsimple.ZoneRecord(record)); err != nil {
|
|
|
|
aID := fmt.Sprintf("%d", accountID)
|
|
|
|
|
|
|
|
if _, err := d.client.Zones.UpdateRecord(aID, record.ZoneID, record.ID, dnsimple.ZoneRecord(record)); err != nil {
|
|
|
|
return errors.Wrap(err, "could not update record")
|
|
|
|
return errors.Wrap(err, "could not update record")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
@ -92,25 +113,26 @@ func (d *Client) DeleteRecord(record Record) error {
|
|
|
|
return errors.Wrap(err, "unable to get account id")
|
|
|
|
return errors.Wrap(err, "unable to get account id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := d.client.Zones.DeleteRecord(accountID, record.ZoneID, record.ID); err != nil {
|
|
|
|
aID := fmt.Sprintf("%d", accountID)
|
|
|
|
|
|
|
|
if _, err := d.client.Zones.DeleteRecord(aID, record.ZoneID, record.ID); err != nil {
|
|
|
|
return errors.Wrap(err, "could not delete record")
|
|
|
|
return errors.Wrap(err, "could not delete record")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *Client) getAccountID() (string, error) {
|
|
|
|
func (d *Client) getAccountID() (int64, error) {
|
|
|
|
if d.accountID != "" {
|
|
|
|
if d.accountID != 0 {
|
|
|
|
return d.accountID, nil
|
|
|
|
return d.accountID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
whoamiResponse, err := d.client.Identity.Whoami()
|
|
|
|
whoamiResponse, err := d.client.Identity.Whoami()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not get account information from dnsimple")
|
|
|
|
return 0, errors.Wrap(err, "could not get account information from dnsimple")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if whoamiResponse.Data.Account == nil {
|
|
|
|
if whoamiResponse.Data.Account == nil {
|
|
|
|
return "", errors.New("could not get account information")
|
|
|
|
return 0, errors.New("could not get account information")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID)
|
|
|
|
d.accountID = whoamiResponse.Data.Account.ID
|
|
|
|
return d.accountID, nil
|
|
|
|
return d.accountID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|