Skip to content

Commit

Permalink
fix context and get zone by id
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Dec 3, 2024
1 parent bbf8bbe commit c126d4a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
1 change: 0 additions & 1 deletion cmd/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func runCloudflare(ctx context.Context, output io.Writer, opts cloudflareOptions
}

client, err := cloudflare.New(
ctx,
cloudflare.WithToken(token),
cloudflare.WithZoneName(opts.domain),
cloudflare.WithSubdomain(opts.subdomain),
Expand Down
9 changes: 7 additions & 2 deletions internal/cloudflare/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cloudflare

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -67,7 +66,7 @@ func WithNuke(nuke bool) Option {
// New creates a new Cloudflare with the given options.
// It returns an error if the token or region is not set, or if it fails to
// create the underlying Cloudflare API client.
func New(ctx context.Context, opts ...Option) (*Cloudflare, error) {
func New(opts ...Option) (*Cloudflare, error) {
c := &Cloudflare{}

for _, opt := range opts {
Expand All @@ -85,6 +84,12 @@ func New(ctx context.Context, opts ...Option) (*Cloudflare, error) {
return nil, fmt.Errorf("unable to authenticate Cloudflare client: %w", err)
}

zoneID, err := cloudflareAPI.ZoneIDByName(c.zoneName)
if err != nil {
return nil, fmt.Errorf("unable to get zone ID by name %q: %w", c.zoneName, err)
}
c.zoneID = zoneID

c.client = cloudflareAPI

if c.logger == nil {
Expand Down
14 changes: 0 additions & 14 deletions internal/cloudflare/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ import (
func (c *Cloudflare) NukeDNSRecords(ctx context.Context) error {
c.logger.Infof("removing dns records for domain %q", c.zoneName)

zones, err := c.client.ListZones(ctx)
if err != nil {
return fmt.Errorf("unable to list zones: %w", err)
}

for _, zone := range zones {
if zone.Name == c.zoneName {
c.zoneID = zone.ID
}
}
if c.zoneID == "" {
return fmt.Errorf("unable to find zone ID for domain: %q", c.zoneName)
}

records, _, err := c.client.ListDNSRecords(ctx, &cloudflarego.ResourceContainer{
Identifier: c.zoneID,
}, cloudflarego.ListDNSRecordsParams{})
Expand Down

0 comments on commit c126d4a

Please sign in to comment.