Skip to content

Commit

Permalink
Merge pull request #925 from imjaroiswebdev/issue-895-err-w-bs
Browse files Browse the repository at this point in the history
Address: Errors when trying to apply a business service that was manually deleted from PD
  • Loading branch information
imjaroiswebdev authored Aug 15, 2024
2 parents b43be0c + d053625 commit 33bc8d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pagerdutyplugin/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ func (r *resourceBusinessService) Read(ctx context.Context, req resource.ReadReq
log.Printf("[INFO] Reading PagerDuty business service %s", state.ID)

state, found := requestGetBusinessService(ctx, r.client, state.ID.ValueString(), false, &resp.Diagnostics)
if !found {
resp.State.RemoveResource(ctx)
return
}
if resp.Diagnostics.HasError() {
if !found {
resp.State.RemoveResource(ctx)
}
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
Expand Down
30 changes: 30 additions & 0 deletions pagerdutyplugin/resource_pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func TestAccPagerDutyBusinessService_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("pagerduty_business_service.foo", "self"),
),
},
// Validating that externally removed business services are detected and
// planed for re-creation
{
Config: testAccCheckPagerDutyBusinessServiceConfig(nameUpdated, descriptionUpdated, pointOfContactUpdated),
Check: resource.ComposeTestCheckFunc(
testAccExternallyDestroyBusinessService("pagerduty_business_service.foo"),
),
ExpectNonEmptyPlan: true,
},
},
})
}
Expand Down Expand Up @@ -145,6 +154,27 @@ func testAccCheckPagerDutyBusinessServiceDestroy(s *terraform.State) error {
return nil
}

func testAccExternallyDestroyBusinessService(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.client
ctx := context.Background()

rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Business Service ID is set")
}

if err := client.DeleteBusinessServiceWithContext(ctx, rs.Primary.ID); err != nil {
return err
}

return nil
}
}

func testAccCheckPagerDutyBusinessServiceConfig(name, description, poc string) string {
return fmt.Sprintf(`
resource "pagerduty_business_service" "foo" {
Expand Down

0 comments on commit 33bc8d4

Please sign in to comment.