Refactor dns part of app to it's own package
dns package changes dns settings through a client, a client is able to: • GetRecords to get a list of records of a type and host. For example, GetRecords can get all "AAAA" records for host "domain.example.com" • CreateRecord will add a dns entry • DeleteRecord will delete a record on dnsimple Update main package to use dns package • main package will now create a record and remove all existing records when updating • Add special case to do nothing if a dns name has only one value Create utils package • Move splitHost into a utils package
This commit is contained in:
parent
59b259e55f
commit
f05b4b4278
115
dns/dns.go
Normal file
115
dns/dns.go
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.xbudex.com/buddy/update-dns/utils"
|
||||||
|
"github.com/dnsimple/dnsimple-go/dnsimple"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client holds metadata for the dns api client
|
||||||
|
type Client struct {
|
||||||
|
client *dnsimple.Client
|
||||||
|
accountID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record is the type used for a Zone record
|
||||||
|
type Record dnsimple.ZoneRecord
|
||||||
|
|
||||||
|
// New returns an implementation of a DNS interface
|
||||||
|
func New(oauthToken string) *Client {
|
||||||
|
credentials := dnsimple.NewOauthTokenCredentials(oauthToken)
|
||||||
|
client := dnsimple.NewClient(credentials)
|
||||||
|
return &Client{
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRecords returns all record for a host
|
||||||
|
func (d *Client) GetRecords(host, kind string) ([]Record, error) {
|
||||||
|
accountID, err := d.getAccountID()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "unable to get account id")
|
||||||
|
}
|
||||||
|
|
||||||
|
subdomain, domain, err := utils.SplitHost(host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "unable to parse host")
|
||||||
|
}
|
||||||
|
|
||||||
|
records, err := d.client.Zones.ListRecords(accountID, domain, &dnsimple.ZoneRecordListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "could not get list of records")
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := []Record{}
|
||||||
|
for _, record := range records.Data {
|
||||||
|
if record.ZoneID == domain && record.Name == subdomain && record.Type == kind {
|
||||||
|
ret = append(ret, Record(record))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateRecord adds a record to DNS
|
||||||
|
func (d *Client) CreateRecord(host string, record Record) error {
|
||||||
|
accountID, err := d.getAccountID()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "unable to get account id")
|
||||||
|
}
|
||||||
|
|
||||||
|
subdomain, domain, err := utils.SplitHost(host)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "unable to parse host")
|
||||||
|
}
|
||||||
|
|
||||||
|
newRecord := dnsimple.ZoneRecord(record)
|
||||||
|
newRecord.Name = subdomain
|
||||||
|
if _, err := d.client.Zones.CreateRecord(accountID, domain, newRecord); err != nil {
|
||||||
|
return errors.Wrap(err, "could not create record")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateRecord returns all record for a host
|
||||||
|
func (d *Client) UpdateRecord(record Record) error {
|
||||||
|
accountID, err := d.getAccountID()
|
||||||
|
if err != nil {
|
||||||
|
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 {
|
||||||
|
return errors.Wrap(err, "could not update record")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRecord returns all record for a host
|
||||||
|
func (d *Client) DeleteRecord(record Record) error {
|
||||||
|
accountID, err := d.getAccountID()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "unable to get account id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := d.client.Zones.DeleteRecord(accountID, record.ZoneID, record.ID); err != nil {
|
||||||
|
return errors.Wrap(err, "could not delete record")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Client) getAccountID() (string, error) {
|
||||||
|
if d.accountID != "" {
|
||||||
|
return d.accountID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
whoamiResponse, err := d.client.Identity.Whoami()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrap(err, "could not get account information from dnsimple")
|
||||||
|
}
|
||||||
|
if whoamiResponse.Data.Account == nil {
|
||||||
|
return "", errors.New("could not get account information")
|
||||||
|
}
|
||||||
|
d.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID)
|
||||||
|
return d.accountID, nil
|
||||||
|
}
|
||||||
123
main.go
123
main.go
@ -5,11 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/dnsimple/dnsimple-go/dnsimple"
|
"git.xbudex.com/buddy/update-dns/dns"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@ -18,6 +17,7 @@ func main() {
|
|||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "update-dns"
|
app.Name = "update-dns"
|
||||||
app.Usage = "update dnsimple with public ip address"
|
app.Usage = "update dnsimple with public ip address"
|
||||||
|
app.Version = "0.2.0-dev"
|
||||||
app.Action = action
|
app.Action = action
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
@ -64,22 +64,19 @@ func action(context *cli.Context) error {
|
|||||||
ip = result.IP
|
ip = result.IP
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var dnSimpleAccountID string
|
client := dns.New(context.String("dnsimple-token"))
|
||||||
var getRecordError error
|
records := []dns.Record{}
|
||||||
var record *dnsimple.ZoneRecord
|
var getRecordsError error
|
||||||
credentials := dnsimple.NewOauthTokenCredentials(context.String("dnsimple-token"))
|
|
||||||
dnSimpleClient := dnsimple.NewClient(credentials)
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
fmt.Println("Getting records")
|
||||||
accountID, result, err := getRecord(dnSimpleClient, host, "AAAA")
|
if results, err := client.GetRecords(host, "AAAA"); err != nil {
|
||||||
dnSimpleAccountID = accountID
|
getRecordsError = errors.Wrap(err, "could not get records for host")
|
||||||
if err != nil {
|
} else {
|
||||||
getRecordError = errors.Wrap(err, "could not get ip for host")
|
records = results
|
||||||
return
|
|
||||||
}
|
}
|
||||||
record = result
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@ -87,36 +84,33 @@ func action(context *cli.Context) error {
|
|||||||
if getIPError != nil {
|
if getIPError != nil {
|
||||||
return errors.Wrap(getIPError, "could no get ip address")
|
return errors.Wrap(getIPError, "could no get ip address")
|
||||||
}
|
}
|
||||||
if getRecordError != nil {
|
|
||||||
switch errors.Cause(getRecordError).(type) {
|
if getRecordsError != nil {
|
||||||
case notFoundError:
|
return errors.Wrap(getRecordsError, "could no get record")
|
||||||
break
|
|
||||||
default:
|
|
||||||
return errors.Wrap(getRecordError, "could no get record")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subdomain, domain, _ := splitHost(host)
|
if len(records) == 1 && records[0].Content == ip {
|
||||||
if record == nil {
|
fmt.Printf("%s record for %s already set as %s\n", "AAAA", host, ip)
|
||||||
_, err := dnSimpleClient.Zones.CreateRecord(dnSimpleAccountID, domain, dnsimple.ZoneRecord{
|
return nil
|
||||||
Name: subdomain,
|
}
|
||||||
Type: "AAAA",
|
|
||||||
Content: ip,
|
if err := client.CreateRecord(host, dns.Record{Type: "AAAA", Content: ip}); err != nil {
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "could not create record")
|
return errors.Wrap(err, "could not create record")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s record created for %s as %s\n", "AAAA", host, ip)
|
fmt.Printf("%s record created for %s as %s\n", "AAAA", host, ip)
|
||||||
} else if ip != record.Content {
|
|
||||||
record.Content = ip
|
var deleteWG sync.WaitGroup
|
||||||
_, err := dnSimpleClient.Zones.UpdateRecord(dnSimpleAccountID, domain, record.ID, *record)
|
for _, record := range records {
|
||||||
if err != nil {
|
deleteWG.Add(1)
|
||||||
return errors.Wrap(err, "could not update record")
|
go func(r dns.Record) {
|
||||||
}
|
defer deleteWG.Done()
|
||||||
fmt.Printf("%s record for %s updated to %s\n", "AAAA", host, ip)
|
fmt.Printf("%s record for %s being deleting %s\n", r.Type, host, r.Content)
|
||||||
} else {
|
client.DeleteRecord(r)
|
||||||
fmt.Printf("%s record for %s already set as %s\n", "AAAA", host, ip)
|
}(record)
|
||||||
}
|
}
|
||||||
|
deleteWG.Wait()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,57 +119,6 @@ type whatIsMyIPResult struct {
|
|||||||
Version string `json:version`
|
Version string `json:version`
|
||||||
}
|
}
|
||||||
|
|
||||||
type notFoundError error
|
|
||||||
|
|
||||||
func getRecord(client *dnsimple.Client, host string, kind string) (string, *dnsimple.ZoneRecord, error) {
|
|
||||||
subdomain, domain, err := splitHost(host)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, errors.Wrap(err, "unable to parse host")
|
|
||||||
}
|
|
||||||
accountID, err := getAccountID(client)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, errors.Wrap(err, "could not get account id")
|
|
||||||
}
|
|
||||||
|
|
||||||
records, err := client.Zones.ListRecords(accountID, domain, &dnsimple.ZoneRecordListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, errors.Wrap(err, "could not get list of records")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, record := range records.Data {
|
|
||||||
if record.ZoneID == domain && record.Name == subdomain && record.Type == kind {
|
|
||||||
return accountID, &record, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return accountID, nil, notFoundError(errors.New("could not find record"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAccountID(client *dnsimple.Client) (string, error) {
|
|
||||||
whoamiResponse, err := client.Identity.Whoami()
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.Wrap(err, "could not get account information from dnsimple")
|
|
||||||
}
|
|
||||||
if whoamiResponse.Data.Account == nil {
|
|
||||||
return "", errors.New("could not get account information")
|
|
||||||
}
|
|
||||||
return strconv.Itoa(whoamiResponse.Data.Account.ID), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func splitHost(host string) (subdomain string, domain string, err error) {
|
|
||||||
subdomains := strings.Split(host, ".")
|
|
||||||
|
|
||||||
if len(subdomains) >= 3 {
|
|
||||||
domain = strings.Join(subdomains[len(subdomains)-2:], ".")
|
|
||||||
subdomain = strings.Join(subdomains[:len(subdomains)-2], ".")
|
|
||||||
} else if len(subdomains) == 2 {
|
|
||||||
domain = strings.Join(subdomains, ".")
|
|
||||||
} else {
|
|
||||||
err = errors.New("invalid domain")
|
|
||||||
}
|
|
||||||
return subdomain, domain, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func getIP(url string) (*whatIsMyIPResult, error) {
|
func getIP(url string) (*whatIsMyIPResult, error) {
|
||||||
result, err := http.Get(url)
|
result, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
25
utils/utils.go
Normal file
25
utils/utils.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SplitHost returns hostname into domain and subdomain strings.
|
||||||
|
// For example:
|
||||||
|
// "some.subdomain.example.com" → ("some.subdomain", "example.com", nil)
|
||||||
|
// "example.com" → ("", "example.com", nil)
|
||||||
|
func SplitHost(host string) (subdomain string, domain string, err error) {
|
||||||
|
subdomains := strings.Split(host, ".")
|
||||||
|
|
||||||
|
if len(subdomains) >= 3 {
|
||||||
|
domain = strings.Join(subdomains[len(subdomains)-2:], ".")
|
||||||
|
subdomain = strings.Join(subdomains[:len(subdomains)-2], ".")
|
||||||
|
} else if len(subdomains) == 2 {
|
||||||
|
domain = strings.Join(subdomains, ".")
|
||||||
|
} else {
|
||||||
|
err = errors.New("invalid domain")
|
||||||
|
}
|
||||||
|
return subdomain, domain, err
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user