Skip to content

Commit b40cbdf

Browse files
committed
Remove associations before deleting notification rule
DependencyTrack returns a 500 error when deleting a rule that still has project, team, or tag associations. The Delete method now removes all associations before attempting deletion. https://claude.ai/code/session_019ocavNKpCndi448shsGs4Q
1 parent d6bf0a6 commit b40cbdf

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

internal/provider/notification_rule_resource.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,41 @@ func (r *notificationRuleResource) Delete(ctx context.Context, req resource.Dele
392392
"name": state.Name.ValueString(),
393393
})
394394

395+
// Remove all associations before deleting to avoid server-side errors.
396+
rule, findErr := r.findRule(ctx, ruleID)
397+
if findErr == nil {
398+
for _, project := range rule.Projects {
399+
_, err := r.client.Notification.RemoveProjectFromRule(ctx, ruleID, project.UUID)
400+
if err != nil {
401+
resp.Diagnostics.AddError(
402+
"Unable to remove project from notification rule before deletion",
403+
"Error from: "+err.Error(),
404+
)
405+
return
406+
}
407+
}
408+
for _, team := range rule.Teams {
409+
_, err := r.client.Notification.RemoveTeamFromRule(ctx, ruleID, team.UUID)
410+
if err != nil {
411+
resp.Diagnostics.AddError(
412+
"Unable to remove team from notification rule before deletion",
413+
"Error from: "+err.Error(),
414+
)
415+
return
416+
}
417+
}
418+
for _, tag := range rule.Tags {
419+
err := r.client.Tag.UntagNotificationRules(ctx, tag.Name, []uuid.UUID{ruleID})
420+
if err != nil {
421+
resp.Diagnostics.AddError(
422+
"Unable to remove tag from notification rule before deletion",
423+
"Error from: "+err.Error(),
424+
)
425+
return
426+
}
427+
}
428+
}
429+
395430
err := r.client.Notification.DeleteRule(ctx, dtrack.NotificationRule{UUID: ruleID})
396431
if err != nil {
397432
resp.Diagnostics.AddError(

0 commit comments

Comments
 (0)