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

Correctly match RRs in the prefetch cache #84

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master

BUG FIXES:

* Prefetch cache lookups are deterministic (dnsimple/terraform-provider-dnsimple#84)

## 0.16.1

IMPROVEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion dnsimple/resource_dnsimple_zone_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func resourceDNSimpleZoneRecordRead(ctx context.Context, data *schema.ResourceDa
}

for index := range records {
if records[index].Name == data.Get("name").(string) {
if records[index].Name == data.Get("name").(string) && records[index].Type == data.Get("type").(string) && records[index].Content == data.Get("value").(string) {
record = &records[index]
break
}
Expand Down
83 changes: 81 additions & 2 deletions dnsimple/resource_dnsimple_zone_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,74 @@ func TestAccDNSimpleZoneRecord_Prefetch(t *testing.T) {
{
Config: fmt.Sprintf(testAccCheckDnsimpleZoneRecordConfigBasic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDnsimpleZoneRecordPrefetch("dnsimple_zone_record.foobar", &record),
testAccCheckDNSimpleZoneRecordExists("dnsimple_zone_record.foobar", &record),
testAccCheckDnsimpleZoneRecordPrefetch("dnsimple_zone_record.foobar"),
),
},
},
})
}

func testAccCheckDnsimpleZoneRecordPrefetch(n string, record *dnsimple.ZoneRecord) resource.TestCheckFunc {
func TestAccDNSimpleZoneRecord_Prefetch_ForEach(t *testing.T) {
// Issue: https://github.com/dnsimple/terraform-provider-dnsimple/issues/80
// This test is to ensure that the prefetch behaviour is deterministic
var recordSet1 dnsimple.ZoneRecord
var recordSet2 dnsimple.ZoneRecord
domain := os.Getenv("DNSIMPLE_DOMAIN")
resourceName1 := "dnsimple_zone_record.for_each_example_1"
resourceName2 := "dnsimple_zone_record.for_each_example_2"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
os.Setenv("PREFETCH", "1")
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckDNSimpleZoneRecordDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccExampleDnsimpleZoneRecordsForEach, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleZoneRecordExists(resourceName1, &recordSet1),
testAccCheckDNSimpleZoneRecordExists(resourceName2, &recordSet2),
),
},
{
Config: fmt.Sprintf(testAccExampleDnsimpleZoneRecordsForEach, domain),
Check: resource.ComposeTestCheckFunc(
func(s *terraform.State) error {
resourceName := resourceName1
expectedID := fmt.Sprintf("%d", recordSet1.ID)
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID != expectedID {
return fmt.Errorf("Expected the ID of the resource (%s) to be the same as the record ID, but got %s and %s", resourceName, rs.Primary.ID, expectedID)
}
return nil
},
func(s *terraform.State) error {
resourceName := resourceName2
expectedID := fmt.Sprintf("%d", recordSet2.ID)
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID != expectedID {
return fmt.Errorf("Expected the ID of the resource (%s) to be the same as the record ID, but got %s and %s", resourceName, rs.Primary.ID, expectedID)
}
return nil
},
),
},
},
})
}

func testAccCheckDnsimpleZoneRecordPrefetch(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
provider := testAccProvider.Meta().(*Client)

Expand Down Expand Up @@ -318,6 +378,25 @@ resource "dnsimple_zone_record" "foobar" {
ttl = 3600
}`

const testAccExampleDnsimpleZoneRecordsForEach = `
resource "dnsimple_zone_record" "for_each_example_1" {
zone_name = %[1]q

name = ""
value = "1.1.1.1"
type = "A"
ttl = 600
}

resource "dnsimple_zone_record" "for_each_example_2" {
zone_name = %[1]q

name = "1a"
value = "1.1.1.1"
type = "A"
ttl = 600
}`

const testAccCheckDnsimpleZoneRecordConfigNewValue = `
resource "dnsimple_zone_record" "foobar" {
zone_name = "%s"
Expand Down