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
+55
View File
@@ -200,3 +200,58 @@ func (c *client) getRule(ctx context.Context, id string) (*rule, error) {
func (c *client) deleteRule(ctx context.Context, id string) error {
return c.do(ctx, http.MethodDelete, "/rules/"+id, nil, nil)
}
// notificationTarget mirrors alerting/internal/notifystore.Target's
// JSON shape -- deliberately a local type, same "talk HTTP, not Go
// imports" posture as dashboard/rule above. Headers is left as raw
// JSON bytes (not decoded into a Go map) since this client has no
// opinion about its shape -- alerting's own Target type doesn't either
// (json.RawMessage), and the resource layer round-trips it as a plain
// JSON-text string a caller provides via Terraform's jsonencode().
//
// Secret genuinely comes back from GET/List unredacted -- confirmed in
// notifystore/store.go's Get/List queries, which select the secret
// column with no redaction at either the store or handler layer. This
// is alerting's own existing behavior, not something this provider
// introduces or could fix from the client side; notificationTargetResource
// marks the corresponding attribute Sensitive so Terraform at least
// doesn't print it in plan/apply console output (it is still stored in
// Terraform state in plaintext -- a standard, disclosed Terraform
// limitation for any sensitive attribute, not specific to this one).
type notificationTarget struct {
ID string `json:"id,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
Name string `json:"name"`
Kind string `json:"kind"`
WebhookURL string `json:"webhook_url"`
PayloadTemplate *string `json:"payload_template,omitempty"`
Headers json.RawMessage `json:"headers,omitempty"`
Secret *string `json:"secret,omitempty"`
CreatedBy string `json:"created_by,omitempty"`
}
// createNotificationTarget and getNotificationTarget are the only
// mutating/reading calls this client makes against /targets -- same
// "no updateX method, alerting has no PUT /targets/{id} either" shape
// as rules above (rulestore.Store/notifystore.Store both only have
// Create/List/Get/Delete). notificationTargetResource is create/destroy
// only for the same reason alertRuleResource is.
func (c *client) createNotificationTarget(ctx context.Context, t *notificationTarget) (*notificationTarget, error) {
var out notificationTarget
if err := c.do(ctx, http.MethodPost, "/targets", t, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *client) getNotificationTarget(ctx context.Context, id string) (*notificationTarget, error) {
var out notificationTarget
if err := c.do(ctx, http.MethodGet, "/targets/"+id, nil, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *client) deleteNotificationTarget(ctx context.Context, id string) error {
return c.do(ctx, http.MethodDelete, "/targets/"+id, nil, nil)
}