Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbai committed May 22, 2023
1 parent 9a7b6e4 commit 6e60162
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ type AlertPrometheusRuleGroupResourceModel struct {
}

type PrometheusRuleModel struct {
Action []PrometheusRuleGroupActionModel `tfschema:"action"`
Alert string `tfschema:"alert"`
Annotations map[string]string `tfschema:"annotations"`
Enabled bool `tfschema:"enabled"`
Expression string `tfschema:"expression"`
For string `tfschema:"for"`
Labels map[string]string `tfschema:"labels"`
Record string `tfschema:"record"`
ResolveConfiguration []PrometheusRuleResolveConfigurationModel `tfschema:"resolve_configuration"`
Severity int64 `tfschema:"severity"`
Action []PrometheusRuleGroupActionModel `tfschema:"action"`
Alert string `tfschema:"alert"`
Annotations map[string]string `tfschema:"annotations"`
Enabled bool `tfschema:"enabled"`
Expression string `tfschema:"expression"`
For string `tfschema:"for"`
Labels map[string]string `tfschema:"labels"`
Record string `tfschema:"record"`
AlertResolution []PrometheusRuleAlertResolutionModel `tfschema:"alert_resolution"`
Severity int64 `tfschema:"severity"`
}

type PrometheusRuleGroupActionModel struct {
ActionGroupId string `tfschema:"action_group_id"`
ActionProperties map[string]string `tfschema:"action_properties"`
}

type PrometheusRuleResolveConfigurationModel struct {
type PrometheusRuleAlertResolutionModel struct {
AutoResolved bool `tfschema:"auto_resolved"`
TimeToResolve string `tfschema:"time_to_resolve"`
}
Expand Down Expand Up @@ -80,16 +80,16 @@ func (r AlertPrometheusRuleGroupResource) CustomizeDiff() sdk.ResourceFunc {
return fmt.Errorf("one and only one of [rule.%d.record, rule.%d.alert] for %s must be set", i, i, model.Name)
}

// actions, severity, annotations, for, resolve_configuration must be empty when type is recording rule
// actions, severity, annotations, for, alert_resolution must be empty when type is recording rule
if r.Record != "" {
_, actionOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".action")
_, severityOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".severity")
_, annotationsOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".annotations")
_, forOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".for")
_, resolveConfigurationOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".resolve_configuration")
_, resolveConfigurationOk := metadata.ResourceDiff.GetOk("rule." + strconv.Itoa(i) + ".alert_resolution")

if actionOk || severityOk || annotationsOk || forOk || resolveConfigurationOk {
return fmt.Errorf("the rule.[%d].action, rule.[%d].severity, rule.[%d].annotations, rule.[%d].for and rule.[%d].resolve_configuration must be empty when the rule type of Alert Prometheus Rule Group (%s) is record", i, i, i, i, i, model.Name)
return fmt.Errorf("the rule.[%d].action, rule.[%d].severity, rule.[%d].annotations, rule.[%d].for and rule.[%d].alert_resolution must be empty when the rule type of Alert Prometheus Rule Group (%s) is record", i, i, i, i, i, model.Name)
}
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (r AlertPrometheusRuleGroupResource) Arguments() map[string]*pluginsdk.Sche
ValidateFunc: validation.StringIsNotEmpty,
},

"resolve_configuration": {
"alert_resolution": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Expand Down Expand Up @@ -455,9 +455,9 @@ func expandPrometheusRuleModel(inputList []PrometheusRuleModel, d *schema.Resour
}
output.Annotations = pointer.To(v.Annotations)
output.For = pointer.To(v.For)
output.ResolveConfiguration = expandPrometheusRuleResolveConfigurationModel(v.ResolveConfiguration)
output.ResolveConfiguration = expandPrometheusRuleAlertResolutionModel(v.AlertResolution)
} else {
// action, alert, severity, annotations, for, resolve_configuration must be empty when type is recording rule
// action, alert, severity, annotations, for, alert_resolution must be empty when type is recording rule
output.Record = pointer.To(v.Record)
}

Expand All @@ -482,7 +482,7 @@ func expandPrometheusRuleGroupActionModel(inputList []PrometheusRuleGroupActionM
return &outputList
}

func expandPrometheusRuleResolveConfigurationModel(inputList []PrometheusRuleResolveConfigurationModel) *prometheusrulegroups.PrometheusRuleResolveConfiguration {
func expandPrometheusRuleAlertResolutionModel(inputList []PrometheusRuleAlertResolutionModel) *prometheusrulegroups.PrometheusRuleResolveConfiguration {
if len(inputList) == 0 {
return nil
}
Expand Down Expand Up @@ -511,23 +511,14 @@ func flattenPrometheusRuleModel(inputList *[]prometheusrulegroups.PrometheusRule
actionsValue := flattenPrometheusRuleGroupActionModel(input.Actions)

output.Action = actionsValue

output.Alert = pointer.From(input.Alert)

output.Annotations = pointer.From(input.Annotations)

output.Enabled = pointer.From(input.Enabled)

output.For = pointer.From(input.For)

output.Labels = pointer.From(input.Labels)

output.Record = pointer.From(input.Record)

resolveConfigurationValue := flattenPrometheusRuleResolveConfigurationModel(input.ResolveConfiguration)

output.ResolveConfiguration = resolveConfigurationValue

resolveConfigurationValue := flattenPrometheusRuleAlertResolutionModel(input.ResolveConfiguration)
output.AlertResolution = resolveConfigurationValue
output.Severity = pointer.From(input.Severity)

outputList = append(outputList, output)
Expand Down Expand Up @@ -555,13 +546,13 @@ func flattenPrometheusRuleGroupActionModel(inputList *[]prometheusrulegroups.Pro
return outputList
}

func flattenPrometheusRuleResolveConfigurationModel(input *prometheusrulegroups.PrometheusRuleResolveConfiguration) []PrometheusRuleResolveConfigurationModel {
outputList := make([]PrometheusRuleResolveConfigurationModel, 0)
func flattenPrometheusRuleAlertResolutionModel(input *prometheusrulegroups.PrometheusRuleResolveConfiguration) []PrometheusRuleAlertResolutionModel {
outputList := make([]PrometheusRuleAlertResolutionModel, 0)
if input == nil {
return outputList
}

output := PrometheusRuleResolveConfigurationModel{}
output := PrometheusRuleAlertResolutionModel{}

output.AutoResolved = pointer.From(input.AutoResolved)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
location = "%s"
scopes = [azurerm_monitor_workspace.test.id]
rule {
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
record = "job_type:billing_jobs_duration_seconds:99p5m"
labels = {
team = "prod"
Expand All @@ -156,7 +158,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "import" {
scopes = azurerm_monitor_alert_prometheus_rule_group.test.scopes
rule {
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
record = "job_type:billing_jobs_duration_seconds:99p5m"
labels = {
team = "prod"
Expand All @@ -180,7 +184,6 @@ resource "azurerm_kubernetes_cluster" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%[2]d"
kubernetes_version = "1.25.5"
default_node_pool {
name = "default"
Expand All @@ -205,7 +208,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
scopes = [azurerm_monitor_workspace.test.id]
rule {
enabled = false
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
record = "job_type:billing_jobs_duration_seconds:99p5m"
labels = {
team = "prod"
Expand All @@ -214,7 +219,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
rule {
alert = "Billing_Processing_Very_Slow"
enabled = true
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
for = "PT5M"
severity = 2
action {
Expand All @@ -223,7 +230,7 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
actionName = "actionValue"
}
}
resolve_configuration {
alert_resolution {
auto_resolved = true
time_to_resolve = "PT10M"
}
Expand Down Expand Up @@ -255,7 +262,6 @@ resource "azurerm_kubernetes_cluster" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%[2]d"
kubernetes_version = "1.25.5"
default_node_pool {
name = "default"
Expand All @@ -274,7 +280,6 @@ resource "azurerm_kubernetes_cluster" "test2" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks2%[2]d"
kubernetes_version = "1.25.5"
default_node_pool {
name = "default"
Expand Down Expand Up @@ -306,7 +311,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
rule {
enabled = true
expression = "histogram_quantile(1.0, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
record = "job_type:billing_jobs_duration_seconds:99p6m"
labels = {
team2 = "prod2"
Expand All @@ -316,7 +323,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
rule {
alert = "Billing_Processing_Very_Slow2"
enabled = false
expression = "histogram_quantile(1.0, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
for = "PT4M"
severity = 1
action {
Expand All @@ -331,7 +340,7 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "test" {
actionName3 = "actionValue3"
}
}
resolve_configuration {
alert_resolution {
auto_resolved = false
time_to_resolve = "PT9M"
}
Expand Down
15 changes: 9 additions & 6 deletions website/docs/r/monitor_alert_prometheus_rule_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ resource "azurerm_kubernetes_cluster" "example" {
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
dns_prefix = "example-aks"
kubernetes_version = "1.25.5"
default_node_pool {
name = "default"
Expand All @@ -60,7 +59,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "example" {
scopes = [azurerm_monitor_workspace.example.id]
rule {
enabled = false
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
record = "job_type:billing_jobs_duration_seconds:99p5m"
labels = {
team = "prod"
Expand All @@ -70,7 +71,9 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "example" {
rule {
alert = "Billing_Processing_Very_Slow"
enabled = true
expression = "histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service=\" billing-processing \"}[5m])) by (job_type))"
expression = <<EOF
histogram_quantile(0.99, sum(rate(jobs_duration_seconds_bucket{service="billing-processing"}[5m])) by (job_type))
EOF
for = "PT5M"
severity = 2
Expand All @@ -81,7 +84,7 @@ resource "azurerm_monitor_alert_prometheus_rule_group" "example" {
}
}
resolve_configuration {
alert_resolution {
auto_resolved = true
time_to_resolve = "PT10M"
}
Expand Down Expand Up @@ -144,7 +147,7 @@ A `rule` block supports the following:

* `record` - (Optional) Specifies the recorded metrics name.

* `resolve_configuration` - (Optional) A `resolve_configuration` block as defined below.
* `alert_resolution` - (Optional) An `alert_resolution` block as defined below.

* `severity` - (Optional) Specifies the severity of the alerts fired by the rule. Possible values are between 0 and 4.

Expand All @@ -158,7 +161,7 @@ An `action` block supports the following:

---

A `resolve_configuration` block supports the following:
An `alert_resolution` block supports the following:

* `auto_resolved` - (Optional) Is the alert auto-resolution? Possible values are `true` and `false`.

Expand Down

0 comments on commit 6e60162

Please sign in to comment.