Add sentry_notification_target, closing the alert-rule-as-code loop

sentry_alert_rule.notification_target_id could previously only point at
a target created outside Terraform (sentryctl/curl/the web UI) --
without this resource, "manage alert rules as code" was only half true.
Same create/destroy-only shape as sentry_alert_rule and for the same
reason: alerting has no PUT /targets/{id} either, confirmed down to
notifystore.Store (Create/List/Get/Delete, no Update).

client.go's notificationTarget type mirrors notifystore.Target's JSON
shape. headers stays raw JSON bytes end to end -- the client has no
opinion about its shape (neither does alerting's own Target type,
json.RawMessage), and the resource layer round-trips it as a plain
JSON-text string a caller provides via Terraform's jsonencode().

secret is marked Sensitive in the schema, but alerting's own
GET /targets/{id} returns it unredacted (confirmed in
notifystore/store.go -- no redaction at the store or handler layer, an
existing property of alerting's API, not something this provider
introduces). A new client test
(TestGetNotificationTargetReturnsSecretUnredacted) documents that real
behavior so a future change to it would be caught here, not discovered
by surprise. Sensitive keeps the value out of plan/apply console output;
it does not keep it out of Terraform state, the standard caveat for any
sensitive attribute, named explicitly in the schema description and
README rather than left implicit.

Examples updated end to end: sentry_alert_rule's example now creates a
real sentry_notification_target and references its .id, instead of a
placeholder string.

Verified: client tests are real httptest.Server round trips. Schema
validation needs no Terraform binary.
TestAccNotificationTargetResource_basic is a real acceptance test,
skip-gated by TF_ACC same as the other two, including a
plancheck.ExpectResourceAction assertion that a config change actually
plans destroy-then-create, and (since secret really does round-trip
unredacted) a real ImportStateVerify on the secret attribute rather than
one papered over with ImportStateVerifyIgnore. Not run against a live
stack in this environment, same disclosed gap as everything else
Docker-gated in this repo.
This commit is contained in:
2026-08-15 10:20:52 -07:00
parent b98f397221
commit 30ae84cd04
11 changed files with 607 additions and 67 deletions
@@ -1,16 +1,18 @@
# notification_target_id has to name an already-existing target --
# no sentry_notification_target resource exists yet (see the provider
# README), so create one via sentryctl/curl/the web UI first and pass
# its id in here.
resource "sentry_notification_target" "ops_webhook" {
name = "Ops Webhook"
kind = "webhook"
webhook_url = "https://ops.example.com/hooks/sentry-alerts"
}
resource "sentry_alert_rule" "checkout_5xx" {
name = "Checkout 5xx spike"
query = "service=checkout status>=500 | stats count"
condition_type = "threshold"
comparator = "gt"
threshold_value = 50
eval_interval_seconds = 60
for_minutes = 5
notification_target_id = "target-abc123"
name = "Checkout 5xx spike"
query = "service=checkout status>=500 | stats count"
condition_type = "threshold"
comparator = "gt"
threshold_value = 50
eval_interval_seconds = 60
for_minutes = 5
notification_target_id = sentry_notification_target.ops_webhook.id
}
# Create/destroy only -- alerting has no PUT /rules/{id} today, so
@@ -0,0 +1 @@
terraform import sentry_notification_target.ops_webhook <target-id>
@@ -0,0 +1,24 @@
resource "sentry_notification_target" "ops_webhook" {
name = "Ops Webhook"
kind = "webhook"
webhook_url = "https://ops.example.com/hooks/sentry-alerts"
# Optional. Sensitive -- not printed in plan/apply output, but note
# it's still stored in Terraform state in plaintext (alerting's own
# GET /targets/{id} returns it unredacted; see the provider README).
secret = var.ops_webhook_secret
# Optional -- must be a JSON object string.
headers = jsonencode({
"X-Team" = "platform"
})
}
variable "ops_webhook_secret" {
type = string
sensitive = true
}
# Create/destroy only -- alerting has no PUT /targets/{id} today, so
# changing any attribute above destroys and recreates the target rather
# than updating it in place. See the provider README for why.