Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Unable to remove contact point if used in notification policy #1806

Open
benwh opened this issue Sep 17, 2024 · 2 comments
Open

[Bug]: Unable to remove contact point if used in notification policy #1806

benwh opened this issue Sep 17, 2024 · 2 comments

Comments

@benwh
Copy link

benwh commented Sep 17, 2024

Terraform Version

1.5.7

Terraform Grafana Provider Version

2.19.4

Grafana Version

11.3.0-75420.patch2-75797 (cloud)

Affected Resource(s)

  • grafana_contact_point
  • grafana_notification_policy

Terraform Configuration Files

locals {
  teams = {
    "team1" = {
      id            = "team1"
      alert_channel = "#team1-alerts"
    },
    "team2" = {
      id            = "team2"
      alert_channel = "#team2-alerts"
    },
  }
}

# Create contact points for each team.
resource "grafana_contact_point" "team" {
  for_each = {
    for team in local.teams :
    team.id => team
  }

  name = "team-${each.key}"

  slack {
    token     = var.grafana_slack_token
    recipient = trimprefix(each.value.alert_channel, "#")
  }
}

resource "grafana_notification_policy" "default" {
  group_by = ["grafana_folder", "alertname"]

  contact_point = grafana_contact_point.team1.name

  dynamic "policy" {
    for_each = {
      for team in local.teams :
      team.id => team
    }
    content {
      matcher {
        label = "team"
        match = "="
        value = policy.value.id
      }

      contact_point = grafana_contact_point.team[policy.value.id].name
    }
  }
}

Expected Behavior

If I remove a team from the local.teams map, I should be able to apply a plan that successfully removes:

a) The contact point from the notification policy
b) The contact point entirely

Currently the dependency tree, and resource creation, works well for creating the resources, but not so much for destroying them.

Actual Behavior

An error like this:

grafana_contact_point.team["team1"]: Destroying... [id=0:team1]

│ Error: [DELETE /v1/provisioning/contact-points/{UID}] DeleteContactpoints (status 409): {}

If I try to manually remove that Contact Point via the API first, then I get:

{"message":"Contact point is currently referenced by a notification policy.","messageId":"alerting.notifications.contact-points.referenced","statusCode":409,"traceID":"fdd47c6ffcd5bc66f47a2453367e96c9"}

Which is understandable...

Because we've got the provisioned = true attribute set on the notification policy, we can't manually delete the reference to the contact point.

Steps to Reproduce

  1. terraform apply the config as supplied
  2. Remove team2 from the map.
  3. terraform plan
    • get a legitimate-looking plan (delete 1 contact point, update notification policy).
  4. terraform apply - fails to delete contact point
    • doesn't attempt to update notification policy.

Important Factoids

It's perhaps debatable whether this is a Terraform issue or a provider issue? I suppose if the provider called the DeleteContactPoint API, with a flag that represented "cascade to all references", then this could be fixed.

I could break the dependency on notification_policy -> contact_point, by not using Terraform implict dependencies, but I expect that if I did that then I'd get an error on creation, rather than destruction, due to trying to add a contact point which doesn't yet exist into a notification policy?

References

No response

@benwh benwh added the bug label Sep 17, 2024
@benwh
Copy link
Author

benwh commented Sep 17, 2024

The workaround that I have right now is to:

  1. terraform state rm 'grafana_contact_point.team["team2"]'
  2. Run terraform apply - which removes the contact point from the notification policy.
  3. Manually remove the contact points via the API:
    CP_ID=abc1234
    curl --fail-with-body -SsL \
      -X DELETE \
      -H "Authorization: Bearer $API_KEY" \
      "https://my-instance.grafana.net/api/v1/provisioning/contact-points/$CP_ID"
    

Which works, but isn't very self-serve for our users, so it'd be great to figure out how to achieve this with declarative config!

@ismailyenigul
Copy link

having the same issue.

Error: [DELETE /v1/provisioning/contact-points/{UID}] DeleteContactpoints (status 409)

@Duologic Duologic removed the bug label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

3 participants