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

Address #655 Tags are not cleaned up from State after removed externally #670

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
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