Skip to content

Commit

Permalink
Merge pull request #314 from giranm/task/add-template-field-for-rules…
Browse files Browse the repository at this point in the history
…et-extraction

[Task] Add "template" field for Global Event Rules and Service Event Rules
  • Loading branch information
Scott McAllister authored Mar 10, 2021
2 parents c26f1eb + 020b4e6 commit a462319
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cloud.google.com/go v0.71.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd // indirect
google.golang.org/api v0.35.0 // indirect
google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ github.com/heimweh/go-pagerduty v0.0.0-20210211225831-18708f545aa5 h1:8yYQBU2sa5
github.com/heimweh/go-pagerduty v0.0.0-20210211225831-18708f545aa5/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4 h1:SdP0fGf1bSiJ807RIDprhs3XIIZXubNpUQPCdp1rMEU=
github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3 h1:W26FTjH1Sg3ngrdGuep+t6a0fpoELVEGbZh62ykkw7k=
github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
18 changes: 12 additions & 6 deletions pagerduty/resource_pagerduty_ruleset_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ func resourcePagerDutyRulesetRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"template": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -513,9 +517,10 @@ func expandExtractions(v interface{}) []*pagerduty.RuleActionExtraction {
for _, eai := range v.([]interface{}) {
ea := eai.(map[string]interface{})
ext := &pagerduty.RuleActionExtraction{
Target: ea["target"].(string),
Source: ea["source"].(string),
Regex: ea["regex"].(string),
Target: ea["target"].(string),
Source: ea["source"].(string),
Regex: ea["regex"].(string),
Template: ea["template"].(string),
}
rae = append(rae, ext)
}
Expand Down Expand Up @@ -677,9 +682,10 @@ func flattenExtractions(rae []*pagerduty.RuleActionExtraction) []interface{} {

for _, ex := range rae {
flatExtract := map[string]interface{}{
"target": ex.Target,
"source": ex.Source,
"regex": ex.Regex,
"target": ex.Target,
"source": ex.Source,
"regex": ex.Regex,
"template": ex.Template,
}
flatExtractList = append(flatExtractList, flatExtract)
}
Expand Down
6 changes: 6 additions & 0 deletions pagerduty/resource_pagerduty_ruleset_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func TestAccPagerDutyRulesetRule_Basic(t *testing.T) {
"pagerduty_ruleset_rule.foo", "conditions.0.subconditions.0.parameter.0.value", "disk space"),
resource.TestCheckResourceAttr(
"pagerduty_ruleset_rule.foo", "actions.0.annotate.0.value", rule),
resource.TestCheckResourceAttr(
"pagerduty_ruleset_rule.foo", "actions.0.extractions.1.template", "{{VAR1}} | {{VAR2}}"),
),
},
{
Expand Down Expand Up @@ -199,6 +201,10 @@ resource "pagerduty_ruleset_rule" "foo" {
source = "details.host"
regex = "(.*)"
}
extractions {
target = "summary"
template = "{{VAR1}} | {{VAR2}}"
}
}
variable {
type = "regex"
Expand Down
4 changes: 4 additions & 0 deletions pagerduty/resource_pagerduty_service_event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ func resourcePagerDutyServiceEventRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"template": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions pagerduty/resource_pagerduty_service_event_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func TestAccPagerDutyServiceEventRule_Basic(t *testing.T) {
"pagerduty_service_event_rule.foo", "conditions.0.subconditions.0.parameter.0.value", "disk space"),
resource.TestCheckResourceAttr(
"pagerduty_service_event_rule.foo", "actions.0.annotate.0.value", rule),
resource.TestCheckResourceAttr(
"pagerduty_service_event_rule.foo", "actions.0.extractions.1.template", "Overriding Summary"),
),
},
{
Expand Down Expand Up @@ -217,6 +219,10 @@ resource "pagerduty_service_event_rule" "foo" {
source = "source"
regex = "(.*)"
}
extractions {
target = "summary"
template = "Overriding Summary"
}
}
}
`, username, email, escalationPolicy, service, rule)
Expand Down
7 changes: 4 additions & 3 deletions vendor/github.com/heimweh/go-pagerduty/pagerduty/ruleset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ github.com/hashicorp/terraform-svchost/auth
github.com/hashicorp/terraform-svchost/disco
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/heimweh/go-pagerduty v0.0.0-20210226020252-e256912df9d4
# github.com/heimweh/go-pagerduty v0.0.0-20210309231526-3275f6c029e3
## explicit
github.com/heimweh/go-pagerduty/pagerduty
# github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
Expand Down
28 changes: 23 additions & 5 deletions website/docs/r/ruleset_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ resource "pagerduty_ruleset_rule" "foo" {
}
}
}
variable {
type = "regex"
name = "Src"
parameters {
value = "(.*)"
path = "payload.source"
}
}
actions {
route {
value = "P5DTL0K"
Expand All @@ -67,6 +75,10 @@ resource "pagerduty_ruleset_rule" "foo" {
source = "details.host"
regex = "(.*)"
}
extractions {
target = "summary"
template = "Warning: Disk Space Low on {{Src}}"
}
}
}
```
Expand Down Expand Up @@ -96,11 +108,17 @@ The following arguments are supported:
* `priority` (Optional) - The ID of the priority applied to the event.
* `severity` (Optional) - The [severity level](https://support.pagerduty.com/docs/rulesets#section-set-severity-with-event-rules) of the event. Can be either `info`,`error`,`warning`, or `critical`.
* `annotate` (Optional) - Note added to the event.
* `extractions` (Optional) - Allows you to copy important data from one event field to another. Extraction rules must use valid [RE2 regular expression syntax](https://github.com/google/re2/wiki/Syntax). Extraction objects consist of the following fields:
* `source` - Field where the data is being copied from.
* `target` - Field where the data is being copied to.
* `regex` - The conditions that need to be met for the extraction to happen.
* *NOTE: A rule can have multiple `extraction` objects attributed to it.*
* `extractions` (Optional) - Allows you to copy important data from one event field to another. Extraction objects may use *either* of the following field structures:
* `source` - Field where the data is being copied from. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.
* `target` - Field where the data is being copied to. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.
* `regex` - The conditions that need to be met for the extraction to happen. Must use valid [RE2 regular expression syntax](https://github.com/google/re2/wiki/Syntax).

*- **OR** -*

* `template` - A customized field message. This can also include variables extracted from the payload by using string interpolation.
* `target` - Field where the data is being copied to. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.

*NOTE: A rule can have multiple `extraction` objects attributed to it.*

* `suppress` (Optional) - Controls whether an alert is [suppressed](https://support.pagerduty.com/docs/rulesets#section-suppress-but-create-triggering-thresholds-with-event-rules) (does not create an incident).
* `value` - Boolean value that indicates if the alert should be suppressed before the indicated threshold values are met.
Expand Down
28 changes: 23 additions & 5 deletions website/docs/r/service_event_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ resource "pagerduty_service_event_rule" "foo" {
}
}
}
variable {
type = "regex"
name = "Src"
parameters {
value = "(.*)"
path = "source"
}
}
actions {
annotate {
value = "From Terraform"
Expand All @@ -44,6 +52,10 @@ resource "pagerduty_service_event_rule" "foo" {
source = "source"
regex = "(.*)"
}
extractions {
target = "summary"
template = "Warning: Disk Space Low on {{Src}}"
}
}
}
Expand Down Expand Up @@ -93,11 +105,17 @@ The following arguments are supported:
* `priority` (Optional) - The ID of the priority applied to the event.
* `severity` (Optional) - The [severity level](https://support.pagerduty.com/docs/rulesets#section-set-severity-with-event-rules) of the event. Can be either `info`,`error`,`warning`, or `critical`.
* `annotate` (Optional) - Note added to the event.
* `extractions` (Optional) - Allows you to copy important data from one event field to another. Extraction rules must use valid [RE2 regular expression syntax](https://github.com/google/re2/wiki/Syntax). Extraction objects consist of the following fields:
* `source` - Field where the data is being copied from. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field
* `target` - Field where the data is being copied to. must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field
* `regex` - The conditions that need to be met for the extraction to happen.
* *NOTE: A rule can have multiple `extraction` objects attributed to it.*
* `extractions` (Optional) - Allows you to copy important data from one event field to another. Extraction objects may use *either* of the following field structures:
* `source` - Field where the data is being copied from. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.
* `target` - Field where the data is being copied to. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.
* `regex` - The conditions that need to be met for the extraction to happen. Must use valid [RE2 regular expression syntax](https://github.com/google/re2/wiki/Syntax).

*- **OR** -*

* `template` - A customized field message. This can also include variables extracted from the payload by using string interpolation.
* `target` - Field where the data is being copied to. Must be a [PagerDuty Common Event Format (PD-CEF)](https://support.pagerduty.com/docs/pd-cef) field.

*NOTE: A rule can have multiple `extraction` objects attributed to it.*

* `suppress` (Optional) - Controls whether an alert is [suppressed](https://support.pagerduty.com/docs/rulesets#section-suppress-but-create-triggering-thresholds-with-event-rules) (does not create an incident).
* `value` - Boolean value that indicates if the alert should be suppressed before the indicated threshold values are met.
Expand Down

0 comments on commit a462319

Please sign in to comment.