Skip to content

Commit

Permalink
update tag not found error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
imjaroiswebdev committed Apr 5, 2023
1 parent 544d49e commit 9ab0173
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
10 changes: 9 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"fmt"
"log"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -135,12 +136,19 @@ func isErrCode(err error, code int) bool {
return false
}

func isMalformedNotFoundError(err error) bool {
// There are some errors that doesn't stick to expected error interface and
// fallback to a simple text error message that can be capture by this regexp.
re := regexp.MustCompile(".*: 404 Not Found$")
return re.Match([]byte(err.Error()))
}

func genError(err error, d *schema.ResourceData) error {
return fmt.Errorf("Error reading: %s: %s", d.Id(), err)
}

func handleNotFoundError(err error, d *schema.ResourceData) error {
if isErrCode(err, 404) {
if isErrCode(err, 404) || isMalformedNotFoundError(err) {
log.Printf("[WARN] Removing %s because it's gone", d.Id())
d.SetId("")
return nil
Expand Down
15 changes: 11 additions & 4 deletions pagerduty/resource_pagerduty_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ func resourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Reading PagerDuty tag %s", d.Id())

return resource.Retry(30*time.Second, func() *resource.RetryError {
if tag, _, err := client.Tags.Get(d.Id()); err != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
} else if tag != nil {
tag, _, err := client.Tags.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}
if tag != nil {
log.Printf("Tag Type: %v", tag.Type)
d.Set("label", tag.Label)
d.Set("summary", tag.Summary)
Expand Down
29 changes: 29 additions & 0 deletions pagerduty/resource_pagerduty_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func TestAccPagerDutyTag_Basic(t *testing.T) {
"pagerduty_tag.foo", "label", tagLabel),
),
},
// Validating that externally removed tags are detected and planed for
// re-creation
{
Config: testAccCheckPagerDutyTagConfig(tagLabel),
Check: resource.ComposeTestCheckFunc(
testAccExternallyDestroyTag("pagerduty_tag.foo"),
),
ExpectNonEmptyPlan: true,
},
},
})
}
Expand Down Expand Up @@ -103,6 +112,26 @@ func testAccCheckPagerDutyTagExists(n string) resource.TestCheckFunc {
}
}

func testAccExternallyDestroyTag(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Tag ID is set")
}

client, _ := testAccProvider.Meta().(*Config).Client()
_, err := client.Tags.Delete(rs.Primary.ID)
if err != nil {
return err
}

return nil
}
}

func testAccCheckPagerDutyTagConfig(tagLabel string) string {
return fmt.Sprintf(`
resource "pagerduty_tag" "foo" {
Expand Down

0 comments on commit 9ab0173

Please sign in to comment.