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/dnsimple: Handle 404 on DNSimple records #13131

Merged
merged 1 commit into from
Apr 12, 2017
Merged
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
6 changes: 6 additions & 0 deletions builtin/providers/dnsimple/resource_dnsimple_record.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strconv"
"strings"

"github.com/dnsimple/dnsimple-go/dnsimple"
"github.com/hashicorp/terraform/helper/schema"
@@ -104,6 +105,11 @@ func resourceDNSimpleRecordRead(d *schema.ResourceData, meta interface{}) error

resp, err := provider.client.Zones.GetRecord(provider.config.Account, d.Get("domain").(string), recordID)
if err != nil {
if err != nil && strings.Contains(err.Error(), "404") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason for repeating if err != nil?

Also, this line reminds me we probably have to improve the error reporting in case of 404. Checking against the 404 string is quite error-prone. I can't recall on the top of my head if there is a better way to handle it, I'll check it by tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@weppos any luck with the SDK?

log.Printf("DNSimple Record Not Found - Refreshing from State")
d.SetId("")
return nil
}
return fmt.Errorf("Couldn't find DNSimple Record: %s", err)
}

48 changes: 42 additions & 6 deletions builtin/providers/dnsimple/resource_dnsimple_record_test.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ func TestAccDNSimpleRecord_Basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -46,7 +46,7 @@ func TestAccDNSimpleRecord_CreateMxWithPriority(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_mx, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -73,7 +73,7 @@ func TestAccDNSimpleRecord_Updated(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -86,7 +86,7 @@ func TestAccDNSimpleRecord_Updated(t *testing.T) {
"dnsimple_record.foobar", "value", "192.168.0.10"),
),
},
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_new_value, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -103,6 +103,27 @@ func TestAccDNSimpleRecord_Updated(t *testing.T) {
})
}

func TestAccDNSimpleRecord_disappears(t *testing.T) {
var record dnsimple.ZoneRecord
domain := os.Getenv("DNSIMPLE_DOMAIN")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
testAccCheckDNSimpleRecordDisappears(&record, domain),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccDNSimpleRecord_UpdatedMx(t *testing.T) {
var record dnsimple.ZoneRecord
domain := os.Getenv("DNSIMPLE_DOMAIN")
@@ -112,7 +133,7 @@ func TestAccDNSimpleRecord_UpdatedMx(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_mx, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -126,7 +147,7 @@ func TestAccDNSimpleRecord_UpdatedMx(t *testing.T) {
"dnsimple_record.foobar", "priority", "5"),
),
},
resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_mx_new_value, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
@@ -144,6 +165,21 @@ func TestAccDNSimpleRecord_UpdatedMx(t *testing.T) {
})
}

func testAccCheckDNSimpleRecordDisappears(record *dnsimple.ZoneRecord, domain string) resource.TestCheckFunc {
return func(s *terraform.State) error {

provider := testAccProvider.Meta().(*Client)

_, err := provider.client.Zones.DeleteRecord(provider.config.Account, domain, record.ID)
if err != nil {
return err
}

return nil
}

}

func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
provider := testAccProvider.Meta().(*Client)