Skip to content

Commit

Permalink
Merge pull request #211 from stmcallister/multi-rule
Browse files Browse the repository at this point in the history
Fixing Multi-Rule Creation Bug and Suppress Rule bug
  • Loading branch information
Scott McAllister authored Apr 20, 2020
2 parents ac83771 + ff10951 commit 0551ab3
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions pagerduty/resource_pagerduty_ruleset_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func resourcePagerDutyRulesetRule() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeString,
Type: schema.TypeBool,
Optional: true,
},
"threshold_value": {
Expand Down Expand Up @@ -600,13 +600,18 @@ func resourcePagerDutyRulesetRuleCreate(d *schema.ResourceData, meta interface{}

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

rule, _, err := client.Rulesets.CreateRule(rule.Ruleset.ID, rule)
if err != nil {
return err
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if rule, _, err := client.Rulesets.CreateRule(rule.Ruleset.ID, rule); err != nil {
return resource.RetryableError(err)
} else if rule != nil {
d.SetId(rule.ID)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

d.SetId(rule.ID)

return resourcePagerDutyRulesetRuleRead(d, meta)
}

Expand All @@ -618,10 +623,7 @@ func resourcePagerDutyRulesetRuleRead(d *schema.ResourceData, meta interface{})

retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if rule, _, err := client.Rulesets.GetRule(rulesetID, d.Id()); err != nil {
if isErrCode(err, 500) || isErrCode(err, 429) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
return resource.RetryableError(err)
} else if rule != nil {
if rule.Conditions != nil {
d.Set("conditions", flattenConditions(rule.Conditions))
Expand Down Expand Up @@ -653,10 +655,16 @@ func resourcePagerDutyRulesetRuleUpdate(d *schema.ResourceData, meta interface{}
log.Printf("[INFO] Updating PagerDuty ruleset rule: %s", d.Id())
rulesetID := d.Get("ruleset").(string)

if _, _, err := client.Rulesets.UpdateRule(rulesetID, d.Id(), rule); err != nil {
return err
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if _, _, err := client.Rulesets.UpdateRule(rulesetID, d.Id(), rule); err != nil {
return resource.RetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

return nil
}

Expand All @@ -666,10 +674,16 @@ func resourcePagerDutyRulesetRuleDelete(d *schema.ResourceData, meta interface{}
log.Printf("[INFO] Deleting PagerDuty ruleset rule: %s", d.Id())
rulesetID := d.Get("ruleset").(string)

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

d.SetId("")

return nil
Expand Down

0 comments on commit 0551ab3

Please sign in to comment.