Skip to content

Commit

Permalink
fix: always refresh records on identifier lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed May 24, 2024
1 parent 85b07ac commit e41eac4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions internal/unifi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Client struct {
BaseURL string
HTTPClient *http.Client
csrf string
records []DNSRecord
}

// DNSRecord represents a DNS record in the API.
Expand Down Expand Up @@ -205,7 +204,6 @@ func (c *Client) ListRecords() ([]DNSRecord, error) {
return nil, err
}

c.records = records // store records for later use
return records, nil
}

Expand Down Expand Up @@ -288,11 +286,12 @@ func (c *Client) DeleteEndpoint(endpoint *endpoint.Endpoint) error {

// LookupIdentifier finds the ID of a DNS record.
func (c *Client) LookupIdentifier(Key string, RecordType string) (string, error) {
if c.records == nil {
c.ListRecords()
records, err := c.ListRecords()
if err != nil {
return "", err
}

for _, r := range c.records {
for _, r := range records {
if r.Key == Key && r.RecordType == RecordType {
return r.ID, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/unifi/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (p *Provider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {

// ApplyChanges applies a given set of changes in the DNS provider.
func (p *Provider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
for _, ep := range changes.Create {
if _, err := p.client.CreateEndpoint(ep); err != nil {
for _, ep := range changes.Delete {
if err := p.client.DeleteEndpoint(ep); err != nil {
return err
}
}
Expand All @@ -73,8 +73,8 @@ func (p *Provider) ApplyChanges(ctx context.Context, changes *plan.Changes) erro
}
}

for _, ep := range changes.Delete {
if err := p.client.DeleteEndpoint(ep); err != nil {
for _, ep := range changes.Create {
if _, err := p.client.CreateEndpoint(ep); err != nil {
return err
}
}
Expand Down

0 comments on commit e41eac4

Please sign in to comment.