Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow retries on DNS records if the record already exists #773

Merged

Conversation

jacobbednarz
Copy link
Member

Despite best efforts, occasionally Terraform's API calls will be faster
than the remote API's consistency. The scenario is easiest to find when
you are constantly updating similar records back and forth or changing
between A/CNAME and there is a Delete operation involved.

This results in a scenario where an end user will get failures from
terraform apply runs that indicate the record alredy exists but
moments later, is available for use.

To handle this scenario a bit nicer for the user, we can attempt to
automatically retry the operation for a short period of time. I've opted
for 30s as that is high enough that the remote API state will be in sync
but also low enough that you won't be waiting too long to find out there
is an issue there if the state is as intended and you're introducing an
unintended duplicate.

Fixes #750

@jacobbednarz
Copy link
Member Author

I couldn't come up with a unit test for this one as it relies on creating records, waiting, deleting a record and then asserting that state. I've manually completed it but can't find a sensible way of automating that.

@@ -270,6 +271,10 @@ func resourceCloudflareRecord() *schema.Resource {
Computed: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Second),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically, 10-15s is probably stil more than enough 🤷‍♂️

Despite best efforts, occasionally Terraform's API calls will be faster
than the remote API's consistency. The scenario is easiest to find when
you are constantly updating similar records back and forth or changing
between A/CNAME and there is a `Delete` operation involved.

This results in a scenario where an end user will get failures from
`terraform apply` runs that indicate the record alredy exists but
moments later, is available for use.

To handle this scenario a bit nicer for the user, we can attempt to
automatically retry the operation for a short period of time. I've opted
for 30s as that is high enough that the remote API state will be in sync
but also low enough that you won't be waiting too long to find out there
is an issue there if the state is as intended and you're introducing an
unintended duplicate.

Fixes cloudflare#750
@jacobbednarz jacobbednarz force-pushed the retry-a-and-cnames-on-duplicates branch from 86b84ff to 7f04599 Compare August 17, 2020 22:03
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
r, err := client.CreateDNSRecord(newRecord.ZoneID, newRecord)
if err != nil {
if strings.Contains(err.Error(), "already exist") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty naive matching but it covers the A/CNAME cases nicely.

@patryk patryk merged commit 2d43c46 into cloudflare:master Aug 18, 2020
boekkooi-lengoo pushed a commit to boekkooi-lengoo/terraform-provider-cloudflare that referenced this pull request Feb 28, 2022
Replace hard coded per_page parameter on DNS API with default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants