Skip to content

Commit

Permalink
Use factory to create new dnsapi object
Browse files Browse the repository at this point in the history
  • Loading branch information
DaruZero committed Sep 26, 2024
1 parent c0f2636 commit dcf2fb7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
46 changes: 3 additions & 43 deletions cmd/dnsapi/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,15 @@ import (
"go.uber.org/zap"
)

type CFDNS struct {
type CloudflareAPI struct {
Api *cloudflare.API
Cfg *config.Config
Records map[string][]cloudflare.DNSRecord
Zones []cloudflare.Zone
}

// New creates a new Dns struct instance
func New(cfg *config.Config) (dns *CFDNS, err error) {
zap.S().Debug("Creating new Dns struct")
dns = &CFDNS{
Cfg: cfg,
}

// Authenticate the APIs
dns.Api, err = cloudflare.New(dns.Cfg.AuthKey, dns.Cfg.Email)
if err != nil {
return nil, err
}

// Fetch all zones
allZones, err := dns.Api.ListZones(context.Background())
if err != nil {
return nil, err
}

// Filter only the selected zones
for _, zone := range allZones {
if utils.StringInSlice(zone.ID, dns.Cfg.ZoneIDs) || utils.StringInSlice(zone.Name, dns.Cfg.ZoneNames) {
dns.Zones = append(dns.Zones, zone)
}
}

if len(dns.Zones) == 0 {
return nil, errors.New("no zones found")
}

// Get the zones records
dns.Records = make(map[string][]cloudflare.DNSRecord)
err = dns.getRecords()
if err != nil {
return dns, err
}

return dns, nil
}

// getRecords gets all the records for the zone
func (dns *CFDNS) getRecords() (err error) {
func (dns *CloudflareAPI) getRecords() (err error) {
zap.S().Info("Getting records")
for _, zone := range dns.Zones {
zap.S().Debugf("%+v", zone)
Expand Down Expand Up @@ -86,7 +46,7 @@ func (dns *CFDNS) getRecords() (err error) {
}

// UpdateRecords updates the records with the current ip
func (dns *CFDNS) UpdateRecords(currentIP string) (updatedRecords map[string][]string, err error) {
func (dns *CloudflareAPI) UpdateRecords(currentIP string) (updatedRecords map[string][]string, err error) {
if err := dns.getRecords(); err != nil {
return nil, err
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/dnsapi/dnsapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dnsapi

import (
"context"
"errors"

"github.com/cloudflare/cloudflare-go"
"github.com/daruzero/cloudflare-dns-auto-updater-go/internal/config"
"github.com/daruzero/cloudflare-dns-auto-updater-go/pkg/utils"
"go.uber.org/zap"
)

type DNSAPI interface {
UpdateRecords(currentIP string) (updatedRecords map[string][]string, err error)
getRecords() (err error)
}

// New creates a new Dns struct instance
func New(cfg *config.Config) (dns DNSAPI, err error) {
zap.S().Debug("Creating new Dns struct")
dns = &CloudflareAPI{
Cfg: cfg,
}

// Authenticate the APIs
dns.(*CloudflareAPI).Api, err = cloudflare.New(dns.(*CloudflareAPI).Cfg.AuthKey, dns.(*CloudflareAPI).Cfg.Email)
if err != nil {
return nil, err
}

// Fetch all zones
allZones, err := dns.(*CloudflareAPI).Api.ListZones(context.Background())
if err != nil {
return nil, err
}

// Filter only the selected zones
for _, zone := range allZones {
if utils.StringInSlice(zone.ID, dns.(*CloudflareAPI).Cfg.ZoneIDs) || utils.StringInSlice(zone.Name, dns.(*CloudflareAPI).Cfg.ZoneNames) {
dns.(*CloudflareAPI).Zones = append(dns.(*CloudflareAPI).Zones, zone)
}
}

if len(dns.(*CloudflareAPI).Zones) == 0 {
return nil, errors.New("no zones found")
}

// Get the zones records
dns.(*CloudflareAPI).Records = make(map[string][]cloudflare.DNSRecord)
err = dns.getRecords()
if err != nil {
return dns, err
}

return dns, nil
}

0 comments on commit dcf2fb7

Please sign in to comment.