-
Notifications
You must be signed in to change notification settings - Fork 263
Feat: add new alerting rule fields #2120
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
15fc370
6d0c920
96f5843
015d50e
52c0a91
127dec6
1938b36
0c8ed67
af63b06
93b5c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
goapi "github.com/grafana/grafana-openapi-client-go/client" | ||
"github.com/grafana/grafana-openapi-client-go/client/provisioning" | ||
"github.com/grafana/grafana-openapi-client-go/models" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
@@ -104,6 +105,28 @@ This resource requires Grafana 9.1.0 or later. | |
return oldDuration == newDuration | ||
}, | ||
}, | ||
"keep_firing_for": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The amount of time for which the rule will considered to be Recovering after initially Firing. Before this time has elapsed, the rule will continue to fire once it's been triggered.", | ||
ValidateDiagFunc: common.ValidateDurationWithDays, | ||
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool { | ||
oldDuration, _ := strfmt.ParseDuration(oldValue) | ||
newDuration, _ := strfmt.ParseDuration(newValue) | ||
return oldDuration == newDuration | ||
}, | ||
}, | ||
"missing_series_evals_to_resolve": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Description: "The number of missing series evaluations that must occur before the rule is considered to be resolved.", | ||
ValidateDiagFunc: func(i any, path cty.Path) (diags diag.Diagnostics) { | ||
if i != nil && i.(int) < 1 { | ||
return diag.Errorf("missing_series_evals_to_resolve must be greater than or equal to 1") | ||
} | ||
return nil | ||
}, | ||
}, | ||
"no_data_state": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
|
@@ -519,6 +542,15 @@ func packAlertRule(r *models.ProvisionedAlertRule) (interface{}, error) { | |
json["record"] = record | ||
} | ||
|
||
// FIXME: open api needs to be a reference to the duration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexander-akhmetov I forgot to about this discovery. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why it's not a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably because the For field is required, it's a pointer: go-swagger/go-swagger#1386 (comment), and KeepFiringFor is not required. |
||
if r.KeepFiringFor != 0 { | ||
json["keep_firing_for"] = r.KeepFiringFor.String() | ||
} | ||
|
||
if r.MissingSeriesEvalsToResolve > 1 { | ||
json["missing_series_evals_to_resolve"] = r.MissingSeriesEvalsToResolve | ||
} | ||
|
||
return json, nil | ||
} | ||
|
||
|
@@ -538,11 +570,22 @@ func unpackAlertRule(raw interface{}, groupName string, folderUID string, orgID | |
return nil, err | ||
} | ||
|
||
keepFiringForStr := json["keep_firing_for"].(string) | ||
if keepFiringForStr == "" { | ||
keepFiringForStr = "0" | ||
} | ||
keepFiringForDuration, err := strfmt.ParseDuration(keepFiringForStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ns, err := unpackNotificationSettings(json["notification_settings"]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
missingSeriesEvalsToResolve := int64(json["missing_series_evals_to_resolve"].(int)) | ||
|
||
// Check for conflicting fields before unpacking the rest of the rule. | ||
// This is a workaround due to the lack of support for ConflictsWith in Lists in the SDK. | ||
errState := json["exec_err_state"].(string) | ||
|
@@ -555,6 +598,12 @@ func unpackAlertRule(raw interface{}, groupName string, folderUID string, orgID | |
if forDuration != 0 { | ||
return nil, fmt.Errorf(incompatFieldMsgFmt, "for") | ||
} | ||
if keepFiringForDuration != 0 { | ||
return nil, fmt.Errorf(incompatFieldMsgFmt, "keep_firing_for") | ||
} | ||
if missingSeriesEvalsToResolve != 0 { | ||
return nil, fmt.Errorf(incompatFieldMsgFmt, "missing_series_evals_to_resolve") | ||
} | ||
if noDataState != "" { | ||
return nil, fmt.Errorf(incompatFieldMsgFmt, "no_data_state") | ||
} | ||
|
@@ -580,21 +629,23 @@ func unpackAlertRule(raw interface{}, groupName string, folderUID string, orgID | |
} | ||
|
||
rule := models.ProvisionedAlertRule{ | ||
UID: json["uid"].(string), | ||
Title: common.Ref(json["name"].(string)), | ||
FolderUID: common.Ref(folderUID), | ||
RuleGroup: common.Ref(groupName), | ||
OrgID: common.Ref(orgID), | ||
ExecErrState: common.Ref(errState), | ||
NoDataState: common.Ref(noDataState), | ||
For: common.Ref(strfmt.Duration(forDuration)), | ||
Data: data, | ||
Condition: common.Ref(condition), | ||
Labels: unpackMap(json["labels"]), | ||
Annotations: unpackMap(json["annotations"]), | ||
IsPaused: json["is_paused"].(bool), | ||
NotificationSettings: ns, | ||
Record: unpackRecord(json["record"]), | ||
UID: json["uid"].(string), | ||
Title: common.Ref(json["name"].(string)), | ||
FolderUID: common.Ref(folderUID), | ||
RuleGroup: common.Ref(groupName), | ||
OrgID: common.Ref(orgID), | ||
ExecErrState: common.Ref(errState), | ||
NoDataState: common.Ref(noDataState), | ||
For: common.Ref(strfmt.Duration(forDuration)), | ||
KeepFiringFor: strfmt.Duration(keepFiringForDuration), | ||
Data: data, | ||
Condition: common.Ref(condition), | ||
Labels: unpackMap(json["labels"]), | ||
Annotations: unpackMap(json["annotations"]), | ||
IsPaused: json["is_paused"].(bool), | ||
NotificationSettings: ns, | ||
Record: unpackRecord(json["record"]), | ||
MissingSeriesEvalsToResolve: missingSeriesEvalsToResolve, | ||
} | ||
|
||
return &rule, nil | ||
|
Uh oh!
There was an error while loading. Please reload this page.