Skip to content

Commit

Permalink
feat(cde-2722): allow to not wait for propagation when deleting DNS r…
Browse files Browse the repository at this point in the history
…ecords (#1)

allow to not wait for propagation when deleting DNS records
  • Loading branch information
NivKeidan authored Jul 7, 2024
1 parent 5afde8b commit 690d760
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 16 deletions.
54 changes: 38 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,18 @@ func (p *Provider) createRecord(ctx context.Context, zoneID string, record libdn
HostedZoneId: aws.String(zoneID),
}

err := p.applyChange(ctx, createInput)
changeId, err := p.applyChange(ctx, createInput)
if err != nil {
return record, err
}

if p.WaitForPropagation {
err = p.waitForChangeToPropagate(ctx, changeId)
if err != nil {
return record, err
}
}

return record, nil
}

Expand Down Expand Up @@ -315,11 +322,18 @@ func (p *Provider) updateRecord(ctx context.Context, zoneID string, record libdn
HostedZoneId: aws.String(zoneID),
}

err := p.applyChange(ctx, updateInput)
changeId, err := p.applyChange(ctx, updateInput)
if err != nil {
return record, err
}

if p.WaitForPropagation {
err = p.waitForChangeToPropagate(ctx, changeId)
if err != nil {
return record, err
}
}

return record, nil
}

Expand Down Expand Up @@ -366,7 +380,7 @@ func (p *Provider) deleteRecord(ctx context.Context, zoneID string, record libdn
HostedZoneId: aws.String(zoneID),
}

err := p.applyChange(ctx, deleteInput)
changeId, err := p.applyChange(ctx, deleteInput)
if err != nil {
var nfe *types.InvalidChangeBatch
if record.Type == "TXT" && errors.As(err, &nfe) {
Expand All @@ -375,27 +389,35 @@ func (p *Provider) deleteRecord(ctx context.Context, zoneID string, record libdn
return record, err
}

if p.shouldWaitForDeletePropagation() {
err = p.waitForChangeToPropagate(ctx, changeId)
if err != nil {
return record, err
}
}

return record, nil
}

func (p *Provider) applyChange(ctx context.Context, input *r53.ChangeResourceRecordSetsInput) error {
func (p *Provider) applyChange(ctx context.Context, input *r53.ChangeResourceRecordSetsInput) (*string, error) {
changeResult, err := p.client.ChangeResourceRecordSets(ctx, input)
if err != nil {
return err
return nil, err
}

// Waiting for propagation if it's set in the provider config.
if p.WaitForPropagation {
changeInput := &r53.GetChangeInput{
Id: changeResult.ChangeInfo.Id,
}
return changeResult.ChangeInfo.Id, nil
}

// Wait for the RecordSetChange status to be "INSYNC"
waiter := r53.NewResourceRecordSetsChangedWaiter(p.client)
err = waiter.Wait(ctx, changeInput, p.MaxWaitDur)
if err != nil {
return err
}
func (p *Provider) waitForChangeToPropagate(ctx context.Context, changeID *string) error {
changeInput := &r53.GetChangeInput{
Id: changeID,
}

// Wait for the RecordSetChange status to be "INSYNC"
waiter := r53.NewResourceRecordSetsChangedWaiter(p.client)
err := waiter.Wait(ctx, changeInput, p.MaxWaitDur)
if err != nil {
return err
}

return nil
Expand Down
23 changes: 23 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ type Provider struct {
// WaitForPropagation if set to true, it will wait for the record to be
// propagated before returning.
WaitForPropagation bool `json:"wait_for_propagation,omitempty"`

// WaitForDeletePropagation enables not to wait for propogation when cleaning challenge records
// 0 (default) will use WaitForPropagation's value (for backwards compatibility)
// 1 will always wait, 2 will never wait
WaitForDeletePropagation waitForDeletePropagationOption `json:"wait_for_delete_propogation,omitempty"`
}

type waitForDeletePropagationOption int

const (
AlwaysWaitForDeletePropagation waitForDeletePropagationOption = 1
NeverWaitForDeletePropagation waitForDeletePropagationOption = 2
)

func (p *Provider) shouldWaitForDeletePropagation() bool {
switch p.WaitForDeletePropagation {
case AlwaysWaitForDeletePropagation:
return true
case NeverWaitForDeletePropagation:
return false
default:
return p.WaitForPropagation
}
}

// GetRecords lists all the records in the zone.
Expand Down
59 changes: 59 additions & 0 deletions provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package route53

import (
"testing"
)

func TestProvider_shouldWaitForDeletePropagation(t *testing.T) {
type fields struct {
WaitForPropagation bool
WaitForDeletePropagation waitForDeletePropagationOption
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "backwards compatability - should wait",
fields: fields{
WaitForPropagation: true,
},
want: true,
},
{
name: "backwards compatability - should not wait",
fields: fields{
WaitForPropagation: false,
},
want: false,
},
{
name: "opposite from WaitForPropagation - should wait",
fields: fields{
WaitForPropagation: false,
WaitForDeletePropagation: AlwaysWaitForDeletePropagation,
},
want: true,
},
{
name: "opposite from WaitForPropagation - should wait",
fields: fields{
WaitForPropagation: true,
WaitForDeletePropagation: NeverWaitForDeletePropagation,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Provider{
WaitForPropagation: tt.fields.WaitForPropagation,
WaitForDeletePropagation: tt.fields.WaitForDeletePropagation,
}
if got := p.shouldWaitForDeletePropagation(); got != tt.want {
t.Errorf("Provider.shouldWaitForDeletePropagation() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 690d760

Please sign in to comment.