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

provider/aws: Refactor Route53 record to fix regression in deleting #4892

Merged
merged 3 commits into from
Jan 29, 2016

Conversation

catsby
Copy link
Contributor

@catsby catsby commented Jan 28, 2016

Route 53 records created in Terraform versions pre-0.6.9 can not be deleted by versions post-0.6.9. This is because of the addition of the weight attribute and how we calculate our records for changes after introducing the sentinel value of -1 for weights:

    w := d.Get("weight").(int)
    if w > -1 {
        rec.Weight = aws.Int64(int64(w))
    }

Records created pre-0.6.9 do not have the weight sentinel value of -1, which was added so that a value of zero could be used for the weight (prior to this it could not be). Because they do not have the -1 value, a d.Get("weight") call returns 0, which is greater than -1. As a result, delete commands includes a 0 weight on records that don't have a weight, so the delete record set doesn't match.

Since the actual record is not a thing we can query directly (it’s a property of the zone), we can’t just issue a DELETE to a specific record, we have to isolate it from the zone’s records and send an UPDATE / DELETE ChangeBatch call to the zone. Currently, we do this by rebuilding the record set from the state.

Additions to the state (via new attributes, et. al) in new versions of Terraform can cause this rebuild to be different than a previous run, if the state file was created in a previous version of Terraform.

In this PR we refactor out the code used in the READ method to range over a record set and find the matching record. We can then issue a delete call on that record, or update the local state file if we’re in the READ method.

Fixes #4641

refactored to add a `findRecord` method to find the matching record set,
and use that for the `DELETE` method call.
if err != nil {
log.Printf("[DEBUG] No matching record found for: %s, removing from state file", d.Id())
d.SetId("")
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like any kind of error here is treated the same. Up in findRecord there's code to recognize NoSuchHostedZone specifically, but then it's wrapped back up in a regular error return value.

I believe this would mean that, for example, if ran terraform plan w/o network connectivity, I could lose all my R53 records from the state.

Maybe we can change the "NoSuchHostedZone" case to just return nil, nil and switch this up to something like:

if err != nil {
  return err
}
if rec == nil {
  log.Printf("[WARN] No matching record found...")
  d.SetId("")
}

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree; this was refactored in 3bbb21d

@phinze
Copy link
Contributor

phinze commented Jan 29, 2016

Switching to findRecord makes a lot of sense to me! One Q about error handling, but looks good generally.

@jen20
Copy link
Contributor

jen20 commented Jan 29, 2016

Changes to error handling LGTM. Build is because of broken upstream dep rather than anything on this PR.

catsby added a commit that referenced this pull request Jan 29, 2016
provider/aws: Refactor Route53 record to fix regression in deleting
@catsby catsby merged commit 41de3ee into master Jan 29, 2016
catsby added a commit that referenced this pull request Feb 3, 2016
This is a follow up on #4892 with tests that demonstrate creating a record and a zone, then destroying said record, and confirming that a new plan is generated, using the ExpectNonEmptyPlan flag

This simulates the bug reported in #4641 by mimicking the state file that one would have if they created a record with Terraform v0.6.6, which is to say a weight = 0 for a default value.

When upgrading, there would be an expected plan change to get that to -1. To mimic the statefile we apply the record and then in a follow up step change the attributes directly. We then try to delete the record.

I tested this by grabbing the source of aws_resource_route53.go from Terraform v0.6.9 and running the included test, which fails. The test will pass with #4892 , because we no longer reconstruct what the record should be based on the state (instead finding via the API and elimination/matching)
@mrwacky42
Copy link
Contributor

Please get this in 0.6.12. This is a PITA.

joshmyers pushed a commit to joshmyers/terraform that referenced this pull request Feb 18, 2016
This is a follow up on hashicorp#4892 with tests that demonstrate creating a record and a zone, then destroying said record, and confirming that a new plan is generated, using the ExpectNonEmptyPlan flag

This simulates the bug reported in hashicorp#4641 by mimicking the state file that one would have if they created a record with Terraform v0.6.6, which is to say a weight = 0 for a default value.

When upgrading, there would be an expected plan change to get that to -1. To mimic the statefile we apply the record and then in a follow up step change the attributes directly. We then try to delete the record.

I tested this by grabbing the source of aws_resource_route53.go from Terraform v0.6.9 and running the included test, which fails. The test will pass with hashicorp#4892 , because we no longer reconstruct what the record should be based on the state (instead finding via the API and elimination/matching)
bigkraig pushed a commit to bigkraig/terraform that referenced this pull request Mar 1, 2016
This is a follow up on hashicorp#4892 with tests that demonstrate creating a record and a zone, then destroying said record, and confirming that a new plan is generated, using the ExpectNonEmptyPlan flag

This simulates the bug reported in hashicorp#4641 by mimicking the state file that one would have if they created a record with Terraform v0.6.6, which is to say a weight = 0 for a default value.

When upgrading, there would be an expected plan change to get that to -1. To mimic the statefile we apply the record and then in a follow up step change the attributes directly. We then try to delete the record.

I tested this by grabbing the source of aws_resource_route53.go from Terraform v0.6.9 and running the included test, which fails. The test will pass with hashicorp#4892 , because we no longer reconstruct what the record should be based on the state (instead finding via the API and elimination/matching)
@jen20 jen20 deleted the b-aws-r53-weight-update branch April 24, 2016 23:57
@ghost
Copy link

ghost commented Apr 26, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws_route53 cannot destroy records
4 participants