Skip to content

Commit d093b7b

Browse files
Adding SOA Deletion short-circuit (#10559) (#7305)
[upstream:9efe66970062aec5a16e3aef55cc523bd699ae56] Signed-off-by: Modular Magician <[email protected]>
1 parent bb2833a commit d093b7b

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

google-beta/services/dns/resource_dns_record_set.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,22 +459,25 @@ func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error
459459

460460
zone := d.Get("managed_zone").(string)
461461

462-
// NS records must always have a value, so we short-circuit delete
463-
// this allows terraform delete to work, but may have unexpected
464-
// side-effects when deleting just that record set.
462+
// NS and SOA records on the root zone must always have a value,
463+
// so we short-circuit delete this allows terraform delete to work,
464+
// but may have unexpected side-effects when deleting just that
465+
// record set.
465466
// Unfortunately, you can set NS records on subdomains, and those
466467
// CAN and MUST be deleted, so we need to retrieve the managed zone,
467468
// check if what we're looking at is a subdomain, and only not delete
468469
// if it's not actually a subdomain
469-
if d.Get("type").(string) == "NS" {
470+
// This does not apply to SOA, as they can only be set on the root
471+
// zone.
472+
if d.Get("type").(string) == "NS" || d.Get("type").(string) == "SOA" {
470473
mz, err := config.NewDnsClient(userAgent).ManagedZones.Get(project, zone).Do()
471474
if err != nil {
472475
return fmt.Errorf("Error retrieving managed zone %q from %q: %s", zone, project, err)
473476
}
474477
domain := mz.DnsName
475478

476479
if domain == d.Get("name").(string) {
477-
log.Println("[DEBUG] NS records can't be deleted due to API restrictions, so they're being left in place. See https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set for more information.")
480+
log.Printf("[DEBUG] root-level %s records can't be deleted due to API restrictions, so they're being left in place. See https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set for more information.\n", d.Get("type").(string))
478481
return nil
479482
}
480483
}

google-beta/services/dns/resource_dns_record_set_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ func TestAccDNSRecordSet_secondaryNS(t *testing.T) {
209209
})
210210
}
211211

212+
// tracks fix for https://github.com/hashicorp/terraform-provider-google/issues/12827
213+
func TestAccDNSRecordSet_deletionSOA(t *testing.T) {
214+
t.Parallel()
215+
216+
zoneName := fmt.Sprintf("dnszone-test-soa-%s", acctest.RandString(t, 10))
217+
recordSetName := "google_dns_managed_zone.parent-zone.dns_name"
218+
acctest.VcrTest(t, resource.TestCase{
219+
PreCheck: func() { acctest.AccTestPreCheck(t) },
220+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
221+
CheckDestroy: testAccCheckDnsRecordSetDestroyProducer(t),
222+
Steps: []resource.TestStep{
223+
{
224+
Config: testAccDnsRecordSet_SOA(zoneName, recordSetName, 300),
225+
},
226+
{
227+
ResourceName: "google_dns_record_set.foobar",
228+
ImportStateId: fmt.Sprintf("projects/%s/managedZones/%s/rrsets/%s.hashicorptest.com./SOA", envvar.GetTestProjectFromEnv(), zoneName, zoneName),
229+
ImportState: true,
230+
ImportStateVerify: true,
231+
},
232+
},
233+
})
234+
}
235+
212236
func TestAccDNSRecordSet_quotedTXT(t *testing.T) {
213237
t.Parallel()
214238

@@ -680,6 +704,24 @@ resource "google_dns_record_set" "foobar" {
680704
`, zoneName, zoneName, zoneName, ttl)
681705
}
682706

707+
func testAccDnsRecordSet_SOA(name string, recordSetName string, ttl int) string {
708+
return fmt.Sprintf(`
709+
resource "google_dns_managed_zone" "parent-zone" {
710+
name = "%s"
711+
dns_name = "%s.hashicorptest.com."
712+
description = "Test Description"
713+
}
714+
715+
resource "google_dns_record_set" "foobar" {
716+
managed_zone = google_dns_managed_zone.parent-zone.name
717+
name = %s
718+
type = "SOA"
719+
rrdatas = ["ns-cloud-a1.googledomains.com. cloud-dns-hostmaster.google.com. 629010464 900 900 1800 60"]
720+
ttl = %d
721+
}
722+
`, name, name, recordSetName, ttl)
723+
}
724+
683725
func testAccDnsRecordSet_quotedTXT(name string, ttl int) string {
684726
return fmt.Sprintf(`
685727
resource "google_dns_managed_zone" "parent-zone" {

website/docs/r/dns_record_set.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: |-
99
Manages a set of DNS records within Google Cloud DNS. For more information see [the official documentation](https://cloud.google.com/dns/records/) and
1010
[API](https://cloud.google.com/dns/api/v1/resourceRecordSets).
1111

12-
~> **Note:** The provider treats this resource as an authoritative record set. This means existing records (including the default records) for the given type will be overwritten when you create this resource in Terraform. In addition, the Google Cloud DNS API requires NS records to be present at all times, so Terraform will not actually remove NS records during destroy but will report that it did.
12+
~> **Note:** The provider treats this resource as an authoritative record set. This means existing records (including the default records) for the given type will be overwritten when you create this resource in Terraform. In addition, the Google Cloud DNS API requires NS and SOA records to be present at all times, so Terraform will not actually remove NS or SOA records on the root of the zone during destroy but will report that it did.
1313

1414
## Example Usage
1515

0 commit comments

Comments
 (0)