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

Adding Retry to Escalation Policy Delete and Trim to User.name #309

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,20 @@ func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interf

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

if _, err := client.EscalationPolicies.Delete(d.Id()); err != nil {
return err
// Retrying to give other resources (such as services) to delete
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if _, err := client.EscalationPolicies.Delete(d.Id()); err != nil {
if isErrCode(err, 400) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}

d.SetId("")
Expand Down
16 changes: 12 additions & 4 deletions pagerduty/resource_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand All @@ -23,6 +24,13 @@ func resourcePagerDutyUser() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
// Suppress the diff shown if there are leading or trailing spaces
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == strings.TrimSpace(new) {
return true
}
return false
},
},

"email": {
Expand Down Expand Up @@ -90,7 +98,7 @@ func resourcePagerDutyUser() *schema.Resource {

func buildUserStruct(d *schema.ResourceData) *pagerduty.User {
user := &pagerduty.User{
Name: d.Get("name").(string),
Name: strings.TrimSpace(d.Get("name").(string)),
Email: d.Get("email").(string),
}

Expand All @@ -113,7 +121,7 @@ func buildUserStruct(d *schema.ResourceData) *pagerduty.User {
if attr, ok := d.GetOk("description"); ok {
user.Description = attr.(string)
}

log.Printf("[DEBUG] buildUserStruct-- d: .%v. user:%v.", d.Get("name").(string), user.Name)
return user
}

Expand All @@ -137,7 +145,7 @@ func resourcePagerDutyUserCreate(d *schema.ResourceData, meta interface{}) error
func resourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty user %s", d.Id())
log.Printf("[INFO] pooh Reading PagerDuty user %s", d.Id())

return resource.Retry(2*time.Minute, func() *resource.RetryError {
user, _, err := client.Users.Get(d.Id(), &pagerduty.GetUserOptions{})
Expand All @@ -150,7 +158,7 @@ func resourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error {

return nil
}

// Trimming whitespace on names in case of mistyped spaces
d.Set("name", user.Name)
d.Set("email", user.Email)
d.Set("time_zone", user.TimeZone)
Expand Down