diff --git a/pagerdutyplugin/resource_pagerduty_business_service.go b/pagerdutyplugin/resource_pagerduty_business_service.go index 184467094..7e6bbd3af 100644 --- a/pagerdutyplugin/resource_pagerduty_business_service.go +++ b/pagerdutyplugin/resource_pagerduty_business_service.go @@ -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)...) diff --git a/pagerdutyplugin/resource_pagerduty_business_service_test.go b/pagerdutyplugin/resource_pagerduty_business_service_test.go index 6af157831..a57e681c0 100644 --- a/pagerdutyplugin/resource_pagerduty_business_service_test.go +++ b/pagerdutyplugin/resource_pagerduty_business_service_test.go @@ -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, + }, }, }) } @@ -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" {