From cc65753aed8adfc3fa4802b610b2c835423804de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 13 Apr 2023 21:17:02 -0400 Subject: [PATCH 1/5] add API call to delete `pagerduty_event_orchestration_service` --- pagerduty/event_orchestration_path_util.go | 37 +++++++++++++++++++ ...erduty_event_orchestration_path_service.go | 25 ++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) 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_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) { From d3e32cfbd4c1b8bbc93fb89699bd1d0da99a7cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 13 Apr 2023 21:17:50 -0400 Subject: [PATCH 2/5] add API call to delete `pagerduty_event_orchestration_router` --- ...gerduty_event_orchestration_path_router.go | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) 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 { From dd859702d0ab95e6bf31f99adfbdeab7baf7239e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 13 Apr 2023 21:21:08 -0400 Subject: [PATCH 3/5] add API call to delete `pagerduty_event_orchestration_unrouted` --- ...rduty_event_orchestration_path_unrouted.go | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go b/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go index 351c41769..c8d1d24e6 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") + routerID := d.Get("event_orchestration").(string) + + log.Printf("[INFO] Deleting PagerDuty Unrouted Event Orchestration Path: %s", routerID) + + retryErr := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, routerID, "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 { From 3d33c176003a9770b07a3c6bfc104f459d01ef30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Thu, 13 Apr 2023 21:21:30 -0400 Subject: [PATCH 4/5] add API call to delete `pagerduty_event_orchestration_global` --- ...gerduty_event_orchestration_path_global.go | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) 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) { From 70e9c32706bc0035dcb76889c6ad50a0174b7296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 14 Apr 2023 11:38:47 -0400 Subject: [PATCH 5/5] rename var to be more meaninful --- .../resource_pagerduty_event_orchestration_path_unrouted.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go b/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go index c8d1d24e6..0c7c1065d 100644 --- a/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go +++ b/pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go @@ -211,12 +211,12 @@ func resourcePagerDutyEventOrchestrationPathUnroutedDelete(ctx context.Context, // In order to delete an Unrouted Orchestration an empty orchestration path // config should be sent as an update. emptyPath := emptyOrchestrationPathStructBuilder("unrouted") - routerID := d.Get("event_orchestration").(string) + orchestrationID := d.Get("event_orchestration").(string) - log.Printf("[INFO] Deleting PagerDuty Unrouted Event Orchestration Path: %s", routerID) + 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, routerID, "unrouted", emptyPath); err != nil { + if _, _, err := client.EventOrchestrationPaths.UpdateContext(ctx, orchestrationID, "unrouted", emptyPath); err != nil { return resource.RetryableError(err) } return nil