Skip to content

Commit

Permalink
Add support for severity options in AlertPolicy (#9544) (#6741)
Browse files Browse the repository at this point in the history
* Add support for severity options in AlertPolicy

* Update description for severity options

* Add update test for severity options

* Format go changes

---------


[upstream:42bac036dc8ec30910c69ca6e6703fd6654ba7d1]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 11, 2023
1 parent b275ec8 commit 7ec4a77
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .changelog/9544.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
monitoring: added `severity` field to `google_monitoring_alert_policy` resource
```
5 changes: 0 additions & 5 deletions .teamcity/components/generated/packages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ var packages = mapOf(
"displayName" to "Environment Variables",
"path" to "./google-beta/envvar"
),
"fwmodels" to mapOf(
"name" to "fwmodels",
"displayName" to "Framework Models",
"path" to "./google-beta/fwmodels"
),
"fwprovider" to mapOf(
"name" to "fwprovider",
"displayName" to "Framework Provider",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,14 @@ entries in this field is
Type: schema.TypeString,
},
},
"severity": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"CRITICAL", "ERROR", "WARNING", ""}),
Description: `The severity of an alert policy indicates how important incidents generated
by that policy are. The severity level will be displayed on the Incident
detail page and in notifications. Possible values: ["CRITICAL", "ERROR", "WARNING"]`,
},
"user_labels": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -1058,6 +1066,12 @@ func resourceMonitoringAlertPolicyCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("user_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(userLabelsProp)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["userLabels"] = userLabelsProp
}
severityProp, err := expandMonitoringAlertPolicySeverity(d.Get("severity"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("severity"); !tpgresource.IsEmptyValue(reflect.ValueOf(severityProp)) && (ok || !reflect.DeepEqual(v, severityProp)) {
obj["severity"] = severityProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1208,6 +1222,9 @@ func resourceMonitoringAlertPolicyRead(d *schema.ResourceData, meta interface{})
if err := d.Set("user_labels", flattenMonitoringAlertPolicyUserLabels(res["userLabels"], d, config)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
if err := d.Set("severity", flattenMonitoringAlertPolicySeverity(res["severity"], d, config)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
if err := d.Set("documentation", flattenMonitoringAlertPolicyDocumentation(res["documentation"], d, config)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
Expand Down Expand Up @@ -1273,6 +1290,12 @@ func resourceMonitoringAlertPolicyUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("user_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["userLabels"] = userLabelsProp
}
severityProp, err := expandMonitoringAlertPolicySeverity(d.Get("severity"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("severity"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, severityProp)) {
obj["severity"] = severityProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1323,6 +1346,10 @@ func resourceMonitoringAlertPolicyUpdate(d *schema.ResourceData, meta interface{
updateMask = append(updateMask, "userLabels")
}

if d.HasChange("severity") {
updateMask = append(updateMask, "severity")
}

if d.HasChange("documentation") {
updateMask = append(updateMask, "documentation")
}
Expand Down Expand Up @@ -1992,6 +2019,10 @@ func flattenMonitoringAlertPolicyUserLabels(v interface{}, d *schema.ResourceDat
return v
}

func flattenMonitoringAlertPolicySeverity(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenMonitoringAlertPolicyDocumentation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -2851,6 +2882,10 @@ func expandMonitoringAlertPolicyUserLabels(v interface{}, d tpgresource.Terrafor
return m, nil
}

func expandMonitoringAlertPolicySeverity(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandMonitoringAlertPolicyDocumentation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testAccMonitoringAlertPolicy_basic(t *testing.T) {
CheckDestroy: testAccCheckAlertPolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, "ALIGN_RATE", filter),
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, "ALIGN_RATE", filter, "WARNING"),
},
{
ResourceName: "google_monitoring_alert_policy.basic",
Expand All @@ -67,24 +67,26 @@ func testAccMonitoringAlertPolicy_update(t *testing.T) {
conditionName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
filter1 := `metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"`
aligner1 := "ALIGN_RATE"
severity1 := "WARNING"
filter2 := `metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.type=\"gce_instance\"`
aligner2 := "ALIGN_MAX"
severity2 := "ERROR"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlertPolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner1, filter1),
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner1, filter1, severity1),
},
{
ResourceName: "google_monitoring_alert_policy.basic",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner2, filter2),
Config: testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner2, filter2, severity2),
},
{
ResourceName: "google_monitoring_alert_policy.basic",
Expand Down Expand Up @@ -235,7 +237,7 @@ func testAccMonitoringAlertPolicy_promql(t *testing.T) {
})
}

func testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner, filter string) string {
func testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner, filter, severity string) string {
return fmt.Sprintf(`
resource "google_monitoring_alert_policy" "basic" {
display_name = "%s"
Expand All @@ -257,8 +259,10 @@ resource "google_monitoring_alert_policy" "basic" {
threshold_value = "0.5"
}
}
severity = "%s"
}
`, alertName, conditionName, aligner, filter)
`, alertName, conditionName, aligner, filter, severity)
}

func testAccMonitoringAlertPolicy_fullCfg(alertName, conditionName1, conditionName2 string) string {
Expand Down Expand Up @@ -321,6 +325,8 @@ resource "google_monitoring_alert_policy" "full" {
}
}
severity = "WARNING"
documentation {
content = "test content"
mime_type = "text/markdown"
Expand Down Expand Up @@ -350,6 +356,8 @@ resource "google_monitoring_alert_policy" "mql" {
}
}
severity = "WARNING"
documentation {
content = "test content"
mime_type = "text/markdown"
Expand Down Expand Up @@ -384,6 +392,8 @@ resource "google_monitoring_alert_policy" "log" {
auto_close = "2000s"
}
severity = "WARNING"
documentation {
content = "test content"
mime_type = "text/markdown"
Expand Down Expand Up @@ -443,6 +453,8 @@ resource "google_monitoring_alert_policy" "promql" {
}
}
severity = "WARNING"
documentation {
content = "test content"
mime_type = "text/markdown"
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/monitoring_alert_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,13 @@ The following arguments are supported:
can contain only lowercase letters, numerals, underscores, and dashes. Keys
must begin with a letter.

* `severity` -
(Optional)
The severity of an alert policy indicates how important incidents generated
by that policy are. The severity level will be displayed on the Incident
detail page and in notifications.
Possible values are: `CRITICAL`, `ERROR`, `WARNING`.

* `documentation` -
(Optional)
Documentation that is included with notifications and incidents related
Expand Down

0 comments on commit 7ec4a77

Please sign in to comment.