Skip to content

feat(monitoring_alert_policy): support notification prompts #8457

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

Conversation

modular-magician
Copy link
Collaborator

Fixes: hashicorp/terraform-provider-google#8470

This PR adds support for setting notification prompts in the alert strategy for google_monitoring_alert_policy resource. This allows users to specify when notifications should be sent for an alert policy. For example, users can choose to send notifications only when an alert is fired, or when it is both fired and recovered. This feature was recently introduced in the Google Cloud Monitoring API and is not yet supported by the Terraform provider. See REST API doc.

  • If no value is set, OPENED and CLOSED are set by default.
  • You cannot specify only CLOSED.
  • Specifying NOTIFICATION_PROMPT_UNSPECIFIED will result in an error.

When only CLOSED is specified:

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "My Alert Policy"
  combiner     = "OR"
  conditions {
    display_name = "test condition"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }
  alert_strategy {
    notification_prompts = ["CLOSED"]
  }
}

The following error is returned, so the user can identify the issue from the error message provided by the API. IMO, validation on the Terraform side isn't necessary.

google_monitoring_alert_policy.alert_policy: Modifying... [id=projects/nagasawa-test/alertPolicies/3128142619627286892]
╷
│ Error: Error updating AlertPolicy "projects/nagasawa-test/alertPolicies/3128142619627286892": googleapi: Error 400: Field alertStrategy.notificationPrompts had an invalid value of "CLOSED": invalid notification prompts: metric-based alert policies notification prompts must be [OPENED] or [OPENED, CLOSED]
│
│   with google_monitoring_alert_policy.alert_policy,
│   on main.tf line 1, in resource "google_monitoring_alert_policy" "alert_policy":1: resource "google_monitoring_alert_policy" "alert_policy" {
│

When NOTIFICATION_PROMPT_UNSPECIFIED is specified:

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "My Alert Policy"
  combiner     = "OR"
  conditions {
    display_name = "test condition"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }
  alert_strategy {
    notification_prompts = ["NOTIFICATION_PROMPT_UNSPECIFIED"]
  }
}

The following error is returned. The API documentation also states "Treated as error", so I'm not sure if it makes sense to allow this to be specified in Terraform.

google_monitoring_alert_policy.alert_policy: Modifying... [id=projects/nagasawa-test/alertPolicies/3128142619627286892]
╷
│ Error: Error updating AlertPolicy "projects/nagasawa-test/alertPolicies/3128142619627286892": googleapi: Error 400: Field alertStrategy.notificationPrompts had an invalid value of "NOTIFICATION_PROMPT_UNSPECIFIED": unspecified notification prompt
│
│   with google_monitoring_alert_policy.alert_policy,
│   on main.tf line 1, in resource "google_monitoring_alert_policy" "alert_policy":1: resource "google_monitoring_alert_policy" "alert_policy" {
│
╵

Release Note Template for Downstream PRs (will be copied)

monitoring: added `alert_strategy.notification_prompts` field to `google_monitoring_alert_policy`

Derived from GoogleCloudPlatform/magic-modules#12052

Signed-off-by: Tsubasa Nagasawa <[email protected]>

[upstream:091b67c44a8578e985b294ad5ae2340a62f66bfc]

Signed-off-by: Modular Magician <[email protected]>
@modular-magician modular-magician merged commit ce6e55b into hashicorp:main Oct 18, 2024
4 checks passed
@modular-magician modular-magician deleted the downstream-pr-091b67c44a8578e985b294ad5ae2340a62f66bfc branch November 16, 2024 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

google_monitoring_alert_policy: support "Notify on incident resolution"
1 participant