Skip to content
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

Issue 463: Introduce catch_all #481

Merged
merged 10 commits into from
Apr 29, 2022
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
cloud.google.com/go v0.71.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/heimweh/go-pagerduty v0.0.0-20220315184127-a3965231ae4f
github.com/heimweh/go-pagerduty v0.0.0-20220422231448-43095fe5ba3f
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ github.com/heimweh/go-pagerduty v0.0.0-20211210233744-b65de43109c1 h1:49zl3n/g+f
github.com/heimweh/go-pagerduty v0.0.0-20211210233744-b65de43109c1/go.mod h1:JtJGtgN0y9KOCaqFMZFaBCWskpO/KK3Ro9TwjP9ss6w=
github.com/heimweh/go-pagerduty v0.0.0-20220208023456-83fe435832fb h1:p3faOVCU8L4wab9mpxN9Cm/VNVkPD8GMEfD0sPHw9nY=
github.com/heimweh/go-pagerduty v0.0.0-20220208023456-83fe435832fb/go.mod h1:JtJGtgN0y9KOCaqFMZFaBCWskpO/KK3Ro9TwjP9ss6w=
github.com/heimweh/go-pagerduty v0.0.0-20220315184127-a3965231ae4f h1:uAJJOxrO5FgLFeumom8jYD0AeFDwSur7CQkYwTabsyk=
github.com/heimweh/go-pagerduty v0.0.0-20220315184127-a3965231ae4f/go.mod h1:JtJGtgN0y9KOCaqFMZFaBCWskpO/KK3Ro9TwjP9ss6w=
github.com/heimweh/go-pagerduty v0.0.0-20220422231448-43095fe5ba3f h1:NLk7iDq85F2lz0q1gY32vZR506aYiNcgvV+Us1rX1q4=
github.com/heimweh/go-pagerduty v0.0.0-20220422231448-43095fe5ba3f/go.mod h1:JtJGtgN0y9KOCaqFMZFaBCWskpO/KK3Ro9TwjP9ss6w=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
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=
Expand Down
93 changes: 89 additions & 4 deletions pagerduty/resource_pagerduty_ruleset_rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -33,6 +34,10 @@ func resourcePagerDutyRulesetRule() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"catch_all": {
Type: schema.TypeBool,
Optional: true,
},
"conditions": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -319,7 +324,14 @@ func buildRulesetRuleStruct(d *schema.ResourceData) *pagerduty.RulesetRule {
Type: "ruleset",
ID: d.Get("ruleset").(string),
},
Conditions: expandConditions(d.Get("conditions").([]interface{})),
}

if _, ok := d.GetOk(("catch_all")); ok {
rule.CatchAll = true
}

if attr, ok := d.GetOk(("conditions")); ok {
rule.Conditions = expandConditions(attr.([]interface{}))
}

if attr, ok := d.GetOk("actions"); ok {
Expand Down Expand Up @@ -745,6 +757,43 @@ func resourcePagerDutyRulesetRuleCreate(d *schema.ResourceData, meta interface{}

log.Printf("[INFO] Creating PagerDuty ruleset rule for ruleset: %s", rule.Ruleset.ID)

// CatchAll rule is created by default.
// Indicating that provided Rule is CatchAll implies modifying it and not creating it
if rule.CatchAll {

log.Printf("[INFO] Found catch_all rule for ruleset: %s", rule.Ruleset.ID)

rulesetrules, _, err := client.Rulesets.ListRules(rule.Ruleset.ID)

if err != nil {
return err
}

if rulesetrules == nil {
return errors.New("No ruleset rule found. Catch-all Resource must exists")
}

var catchallrule *pagerduty.RulesetRule
for _, rule := range rulesetrules.Rules {
if rule.CatchAll {
catchallrule = rule
break
}
}

if catchallrule == nil {
return errors.New("No Catch-all rule found. Catch-all Resource must exists")
}

if err := performRulesetRuleUpdate(rule.Ruleset.ID, catchallrule.ID, rule, client); err != nil {
return err
}

d.SetId(catchallrule.ID)

return resourcePagerDutyRulesetRuleRead(d, meta)
}

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if rule, _, err := client.Rulesets.CreateRule(rule.Ruleset.ID, rule); err != nil {
return resource.RetryableError(err)
Expand Down Expand Up @@ -812,10 +861,14 @@ func resourcePagerDutyRulesetRuleUpdate(d *schema.ResourceData, meta interface{}
log.Printf("[INFO] Updating PagerDuty ruleset rule: %s", d.Id())
rulesetID := d.Get("ruleset").(string)

return performRulesetRuleUpdate(rulesetID, d.Id(), rule, client)
}

func performRulesetRuleUpdate(rulesetID string, id string, rule *pagerduty.RulesetRule, client *pagerduty.Client) error {
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if updatedRule, _, err := client.Rulesets.UpdateRule(rulesetID, d.Id(), rule); err != nil {
if updatedRule, _, err := client.Rulesets.UpdateRule(rulesetID, id, rule); err != nil {
return resource.RetryableError(err)
} else if rule.Position != nil && *updatedRule.Position != *rule.Position {
} else if rule.Position != nil && *updatedRule.Position != *rule.Position && rule.CatchAll != true {
log.Printf("[INFO] PagerDuty ruleset rule %s position %d needs to be %d", updatedRule.ID, *updatedRule.Position, *rule.Position)
return resource.RetryableError(fmt.Errorf("Error updating ruleset rule %s position %d needs to be %d", updatedRule.ID, *updatedRule.Position, *rule.Position))
}
Expand All @@ -834,9 +887,41 @@ func resourcePagerDutyRulesetRuleDelete(d *schema.ResourceData, meta interface{}
return err
}

log.Printf("[INFO] Deleting PagerDuty ruleset rule: %s", d.Id())
rulesetID := d.Get("ruleset").(string)

// Don't delete catch_all resource
if _, ok := d.GetOk(("catch_all")); ok {

log.Printf("[INFO] Rule %s is a catch_all rule, don't delete it, reset it instead", d.Id())

rule, _, err := client.Rulesets.GetRule(rulesetID, d.Id())

if err != nil {
return err
}

// Reset all available actions back to the default state of the catch_all rule
rule.Actions.Annotate = nil
rule.Actions.EventAction = nil
rule.Actions.Extractions = nil
rule.Actions.Priority = nil
rule.Actions.Route = nil
rule.Actions.Severity = nil
rule.Actions.Suppress = new(pagerduty.RuleActionSuppress)
rule.Actions.Suppress.Value = true
rule.Actions.Suspend = nil

if err := performRulesetRuleUpdate(rulesetID, d.Id(), rule, client); err != nil {
return err
}

d.SetId("")

return nil
}

log.Printf("[INFO] Deleting PagerDuty ruleset rule: %s", d.Id())

retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if _, err := client.Rulesets.DeleteRule(rulesetID, d.Id()); err != nil {
return resource.RetryableError(err)
Expand Down
Loading