Skip to content

Commit

Permalink
Merge pull request #676 from imjaroiswebdev/sf-ticket-00556518-eo-ser…
Browse files Browse the repository at this point in the history
…vice-not-deleted

Support for deleting remote configuration of Event Orchestration Paths
  • Loading branch information
imjaroiswebdev authored Apr 14, 2023
2 parents da8299a + 70e9c32 commit 635a596
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 8 deletions.
37 changes: 37 additions & 0 deletions pagerduty/event_orchestration_path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
25 changes: 23 additions & 2 deletions pagerduty/resource_pagerduty_event_orchestration_path_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions pagerduty/resource_pagerduty_event_orchestration_path_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 23 additions & 2 deletions pagerduty/resource_pagerduty_event_orchestration_path_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions pagerduty/resource_pagerduty_event_orchestration_path_unrouted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 635a596

Please sign in to comment.