Skip to content

Commit

Permalink
Check for empty change request
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-soltesz committed Mar 12, 2024
1 parent bca43d8 commit e2c7317
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/dnsx/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewManager(s dnsiface.Service, project, zone string) *Manager {

// Register creates a new resource record for hostname with the given ipv4 and ipv6 adresses.
func (d *Manager) Register(ctx context.Context, hostname, ipv4, ipv6 string) (*dns.Change, error) {
var err error
chg := &dns.Change{}
records := []struct {
ip string
Expand All @@ -49,7 +50,8 @@ func (d *Manager) Register(ctx context.Context, hostname, ipv4, ipv6 string) (*d
if record.ip == "" {
continue
}
rr, err := d.get(ctx, hostname, record.rtype)
var rr *dns.ResourceRecordSet
rr, err = d.get(ctx, hostname, record.rtype)
if rr != nil {
// Found a registration.
if len(rr.Rrdatas) == 1 && rr.Rrdatas[0] == record.ip {
Expand Down Expand Up @@ -82,6 +84,10 @@ func (d *Manager) Register(ctx context.Context, hostname, ipv4, ipv6 string) (*d
)
}
}
if chg.Additions == nil && chg.Deletions == nil {
// Without any actions, the ChangeCreate will fail with an error.
return nil, err
}
// Apply changes.
result, err := d.Service.ChangeCreate(ctx, d.Project, d.Zone, chg)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/dnsx/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (f *fakeDNS) ResourceRecordSetsGet(ctx context.Context, project string, zon
}

func (f *fakeDNS) ChangeCreate(ctx context.Context, project string, zone string, change *dns.Change) (*dns.Change, error) {
if change.Additions == nil && change.Deletions == nil {
return nil, errors.New("fake change create error")
}
return change, f.chgErr
}

Expand Down

0 comments on commit e2c7317

Please sign in to comment.