Stop custom Client struct from being exported in Provider

This commit is contained in:
Alexandre Oliveira 2020-11-24 00:38:07 +01:00
parent 120fee4266
commit 77ddd0389e
2 changed files with 27 additions and 27 deletions

View File

@ -6,42 +6,42 @@ import (
"sync" "sync"
"time" "time"
"github.com/vultr/govultr"
"github.com/libdns/libdns" "github.com/libdns/libdns"
"github.com/vultr/govultr"
) )
type Client struct { type Client struct {
client *govultr.Client vultr *govultr.Client
mutex sync.Mutex mutex sync.Mutex
} }
func (p *Provider) getClient() error { func (p *Provider) getClient() error {
if p.client == nil { if p.client.vultr == nil {
p.client = govultr.NewClient(nil, p.APIToken) p.client.vultr = govultr.NewClient(nil, p.APIToken)
} }
return nil return nil
} }
func (p *Provider) getDNSEntries(ctx context.Context, domain string) ([]libdns.Record, error) { func (p *Provider) getDNSEntries(ctx context.Context, domain string) ([]libdns.Record, error) {
p.mutex.Lock() p.client.mutex.Lock()
defer p.mutex.Unlock() defer p.client.mutex.Unlock()
p.getClient() p.getClient()
var records []libdns.Record var records []libdns.Record
dns_entries, err := p.client.DNSRecord.List(ctx, domain) dns_entries, err := p.client.vultr.DNSRecord.List(ctx, domain)
if err != nil { if err != nil {
return records, err return records, err
} }
for _, entry := range dns_entries { for _, entry := range dns_entries {
record := libdns.Record{ record := libdns.Record{
Name: entry.Name, Name: entry.Name,
Value: entry.Data, Value: entry.Data,
Type: entry.Type, Type: entry.Type,
TTL: time.Duration(entry.TTL) * time.Second, TTL: time.Duration(entry.TTL) * time.Second,
ID: strconv.Itoa(entry.RecordID), ID: strconv.Itoa(entry.RecordID),
} }
records = append(records, record) records = append(records, record)
} }
@ -50,12 +50,12 @@ func (p *Provider) getDNSEntries(ctx context.Context, domain string) ([]libdns.R
} }
func (p *Provider) addDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) { func (p *Provider) addDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock() p.client.mutex.Lock()
defer p.mutex.Unlock() defer p.client.mutex.Unlock()
p.getClient() p.getClient()
err := p.client.DNSRecord.Create(ctx, domain, record.Type, record.Name, record.Value, int(record.TTL.Seconds()), 0) err := p.client.vultr.DNSRecord.Create(ctx, domain, record.Type, record.Name, record.Value, int(record.TTL.Seconds()), 0)
if err != nil { if err != nil {
return record, err return record, err
} }
@ -64,12 +64,12 @@ func (p *Provider) addDNSRecord(ctx context.Context, domain string, record libdn
} }
func (p *Provider) removeDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) { func (p *Provider) removeDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock() p.client.mutex.Lock()
defer p.mutex.Unlock() defer p.client.mutex.Unlock()
p.getClient() p.getClient()
err := p.client.DNSRecord.Delete(ctx, domain, record.ID) err := p.client.vultr.DNSRecord.Delete(ctx, domain, record.ID)
if err != nil { if err != nil {
return record, err return record, err
} }
@ -78,8 +78,8 @@ func (p *Provider) removeDNSRecord(ctx context.Context, domain string, record li
} }
func (p *Provider) updateDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) { func (p *Provider) updateDNSRecord(ctx context.Context, domain string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock() p.client.mutex.Lock()
defer p.mutex.Unlock() defer p.client.mutex.Unlock()
p.getClient() p.getClient()
@ -89,14 +89,14 @@ func (p *Provider) updateDNSRecord(ctx context.Context, domain string, record li
} }
entry := govultr.DNSRecord{ entry := govultr.DNSRecord{
Name: record.Name, Name: record.Name,
Data: record.Value, Data: record.Value,
Type: record.Type, Type: record.Type,
TTL: int(record.TTL.Seconds()), TTL: int(record.TTL.Seconds()),
RecordID: id, RecordID: id,
} }
err = p.client.DNSRecord.Update(ctx, domain, &entry) err = p.client.vultr.DNSRecord.Update(ctx, domain, &entry)
if err != nil { if err != nil {
return record, err return record, err
} }

View File

@ -11,7 +11,7 @@ import (
// Provider implements the libdns interfaces for Vultr // Provider implements the libdns interfaces for Vultr
// Adapted from libdns/digitalocean to work with the Vultr API // Adapted from libdns/digitalocean to work with the Vultr API
type Provider struct { type Provider struct {
Client client Client
// APIToken is the Vultr API token // APIToken is the Vultr API token
// see https://my.vultr.com/settings/#settingsapi // see https://my.vultr.com/settings/#settingsapi
APIToken string `json:"auth_token"` APIToken string `json:"auth_token"`