diff --git a/pagerduty/event_orchestration_path_util.go b/pagerduty/event_orchestration_path_util.go index 2cd608590..de93114f1 100644 --- a/pagerduty/event_orchestration_path_util.go +++ b/pagerduty/event_orchestration_path_util.go @@ -350,3 +350,40 @@ func convertEventOrchestrationPathWarningsToDiagnostics(warnings []*pagerduty.Ev return diags } + +func emptyOrchestrationPathStructBuilder(pathType string) *pagerduty.EventOrchestrationPath { + commonEmptyOrchestrationPath := func() *pagerduty.EventOrchestrationPath { + return &pagerduty.EventOrchestrationPath{ + CatchAll: &pagerduty.EventOrchestrationPathCatchAll{ + Actions: nil, + }, + Sets: []*pagerduty.EventOrchestrationPathSet{ + { + ID: "start", + Rules: []*pagerduty.EventOrchestrationPathRule{}, + }, + }, + } + } + routerEmptyOrchestrationPath := func() *pagerduty.EventOrchestrationPath { + return &pagerduty.EventOrchestrationPath{ + CatchAll: &pagerduty.EventOrchestrationPathCatchAll{ + Actions: &pagerduty.EventOrchestrationPathRuleActions{ + RouteTo: "unrouted", + }, + }, + Sets: []*pagerduty.EventOrchestrationPathSet{ + { + ID: "start", + Rules: []*pagerduty.EventOrchestrationPathRule{}, + }, + }, + } + } + + if pathType == "router" { + return routerEmptyOrchestrationPath() + } + + return commonEmptyOrchestrationPath() +} diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_global.go b/pagerduty/resource_pagerduty_event_orchestration_path_global.go index b9f595c77..81260fc08 100644 --- a/pagerduty/resource_pagerduty_event_orchestration_path_global.go +++ b/pagerduty/resource_pagerduty_event_orchestration_path_global.go @@ -230,10 +230,31 @@ func resourcePagerDutyEventOrchestrationPathGlobalUpdate(ctx context.Context, d } func resourcePagerDutyEventOrchestrationPathGlobalDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics + client, err := meta.(*Config).Client() + if err != nil { + return diag.FromErr(err) + } + + // In order to delete a Global Orchestration an empty orchestration path + // config should be sent as an update. + emptyPath := emptyOrchestrationPathStructBuilder("global_orchestration") + orchestrationID := d.Get("event_orchestration").(string) + + log.Printf("[INFO] Deleting PagerDuty Global Event Orchestration Path: %s", orchestrationID) + + retryErr := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, orchestrationID, "global", emptyPath); err != nil { + return resource.RetryableError(err) + } + return nil + }) + + if retryErr != nil { + return diag.FromErr(retryErr) + } d.SetId("") - return diags + return nil } func resourcePagerDutyEventOrchestrationPathGlobalImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_router.go b/pagerduty/resource_pagerduty_event_orchestration_path_router.go index ee1a2fec4..40544f3f9 100644 --- a/pagerduty/resource_pagerduty_event_orchestration_path_router.go +++ b/pagerduty/resource_pagerduty_event_orchestration_path_router.go @@ -153,10 +153,31 @@ func resourcePagerDutyEventOrchestrationPathRouterCreate(ctx context.Context, d } func resourcePagerDutyEventOrchestrationPathRouterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics + client, err := meta.(*Config).Client() + if err != nil { + return diag.FromErr(err) + } + + // In order to delete an Orchestration Router an empty orchestration path + // config should be sent as an update. + emptyPath := emptyOrchestrationPathStructBuilder("router") + routerID := d.Get("event_orchestration").(string) + + log.Printf("[INFO] Deleting PagerDuty Event Orchestration Router Path: %s", routerID) + + retryErr := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, routerID, "router", emptyPath); err != nil { + return resource.RetryableError(err) + } + return nil + }) + + if retryErr != nil { + return diag.FromErr(retryErr) + } d.SetId("") - return diags + return nil } func resourcePagerDutyEventOrchestrationPathRouterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_service.go b/pagerduty/resource_pagerduty_event_orchestration_path_service.go index 5def2735c..be4bfc9f5 100644 --- a/pagerduty/resource_pagerduty_event_orchestration_path_service.go +++ b/pagerduty/resource_pagerduty_event_orchestration_path_service.go @@ -315,10 +315,31 @@ func needToUpdateServiceActiveStatus(d *schema.ResourceData) bool { } func resourcePagerDutyEventOrchestrationPathServiceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics + client, err := meta.(*Config).Client() + if err != nil { + return diag.FromErr(err) + } + + // In order to delete a Service Orchestration an empty orchestration path + // config should be sent as an update. + emptyPath := emptyOrchestrationPathStructBuilder("service") + serviceID := d.Get("service").(string) + + log.Printf("[INFO] Deleting PagerDuty Event Orchestration Service Path: %s", serviceID) + + retryErr := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, serviceID, "service", emptyPath); err != nil { + return resource.RetryableError(err) + } + return nil + }) + + if retryErr != nil { + return diag.FromErr(retryErr) + } d.SetId("") - return diags + return nil } func resourcePagerDutyEventOrchestrationPathServiceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go b/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go index 351c41769..0c7c1065d 100644 --- a/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go +++ b/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go @@ -203,10 +203,31 @@ func resourcePagerDutyEventOrchestrationPathUnroutedCreate(ctx context.Context, // EventOrchestrationPath cannot be deleted, use update to add / edit / remove rules and sets func resourcePagerDutyEventOrchestrationPathUnroutedDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics + client, err := meta.(*Config).Client() + if err != nil { + return diag.FromErr(err) + } + + // In order to delete an Unrouted Orchestration an empty orchestration path + // config should be sent as an update. + emptyPath := emptyOrchestrationPathStructBuilder("unrouted") + orchestrationID := d.Get("event_orchestration").(string) + + log.Printf("[INFO] Deleting PagerDuty Unrouted Event Orchestration Path: %s", orchestrationID) + + retryErr := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, orchestrationID, "unrouted", emptyPath); err != nil { + return resource.RetryableError(err) + } + return nil + }) + + if retryErr != nil { + return diag.FromErr(retryErr) + } d.SetId("") - return diags + return nil } func resourcePagerDutyEventOrchestrationPathUnroutedUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {