Skip to content

Commit

Permalink
[PagerDuty#274] Updating ruleset, service integration, team membershi…
Browse files Browse the repository at this point in the history
…p, user contact, and notifications rules to have similar safeguards against eventual consistent reads.
  • Loading branch information
eric-spence-code committed Aug 29, 2021
1 parent ab529e3 commit 75e5f60
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 132 deletions.
55 changes: 29 additions & 26 deletions pagerduty/resource_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ func flattenTeam(v *pagerduty.RulesetObject) []interface{} {

return []interface{}{team}
}

func fetchPagerDutyRuleset(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client := meta.(*pagerduty.Client)
return resource.Retry(2*time.Minute, func() *resource.RetryError {
ruleset, _, err := client.Rulesets.Get(d.Id())
if err != nil {
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}
d.Set("name", ruleset.Name)
d.Set("type", ruleset.Type)

// if ruleset is found set to ResourceData
if ruleset.Team != nil {
d.Set("team", flattenTeam(ruleset.Team))
}
d.Set("routing_keys", ruleset.RoutingKeys)

return nil
})
}

func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

Expand All @@ -122,37 +149,13 @@ func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) er
if retryErr != nil {
return retryErr
}

return resourcePagerDutyRulesetRead(d, meta)
return fetchPagerDutyRuleset(d, meta, genError)
}

func resourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty ruleset: %s", d.Id())
return fetchPagerDutyRuleset(d, meta, handleNotFoundError)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
ruleset, _, err := client.Rulesets.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}
d.Set("name", ruleset.Name)
d.Set("type", ruleset.Type)

// if ruleset is found set to ResourceData
if ruleset.Team != nil {
d.Set("team", flattenTeam(ruleset.Team))
}
d.Set("routing_keys", ruleset.RoutingKeys)

return nil
})
}
func resourcePagerDutyRulesetUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
Expand Down
72 changes: 37 additions & 35 deletions pagerduty/resource_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,8 @@ func buildServiceIntegrationStruct(d *schema.ResourceData) *pagerduty.Integratio
return serviceIntegration
}

func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyServiceIntegration(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client := meta.(*pagerduty.Client)

serviceIntegration := buildServiceIntegrationStruct(d)

log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name)

service := d.Get("service").(string)

retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
if serviceIntegration, _, err := client.Services.CreateIntegration(service, serviceIntegration); err != nil {
if isErrCode(err, 400) {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
} else if serviceIntegration != nil {
d.SetId(serviceIntegration.ID)
}
return nil
})

if retryErr != nil {
return retryErr
}

return resourcePagerDutyServiceIntegrationRead(d, meta)
}

func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

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

service := d.Get("service").(string)

o := &pagerduty.GetIntegrationOptions{}
Expand All @@ -149,7 +116,7 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf
serviceIntegration, _, err := client.Services.GetIntegration(service, d.Id(), o)
if err != nil {
log.Printf("[WARN] Service integration read error")
errResp := handleNotFoundError(err, d)
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand Down Expand Up @@ -185,6 +152,41 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf
})
}

func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

serviceIntegration := buildServiceIntegrationStruct(d)

log.Printf("[INFO] Creating PagerDuty service integration %s", serviceIntegration.Name)

service := d.Get("service").(string)

retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
if serviceIntegration, _, err := client.Services.CreateIntegration(service, serviceIntegration); err != nil {
if isErrCode(err, 400) {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
} else if serviceIntegration != nil {
d.SetId(serviceIntegration.ID)
}
return nil
})

if retryErr != nil {
return retryErr
}

return fetchPagerDutyServiceIntegration(d, meta, genError)
}

func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Reading PagerDuty service integration %s", d.Id())
return fetchPagerDutyServiceIntegration(d, meta, handleNotFoundError)
}

func resourcePagerDutyServiceIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

Expand Down
67 changes: 34 additions & 33 deletions pagerduty/resource_pagerduty_team_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,15 @@ func resourcePagerDutyTeamMembership() *schema.Resource {
},
}
}
func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)
teamID := d.Get("team_id").(string)
role := d.Get("role").(string)

log.Printf("[DEBUG] Adding user: %s to team: %s with role: %s", userID, teamID, role)

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.Teams.AddUserWithRole(teamID, userID, role); err != nil {
if isErrCode(err, 500) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

return nil
})
if retryErr != nil {
return retryErr
}

d.SetId(fmt.Sprintf("%s:%s", userID, teamID))

return resourcePagerDutyTeamMembershipRead(d, meta)
}

func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyTeamMembership(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client := meta.(*pagerduty.Client)

userID, teamID := resourcePagerDutyTeamMembershipParseID(d.Id())

log.Printf("[DEBUG] Reading user: %s from team: %s", userID, teamID)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Teams.GetMembers(teamID, &pagerduty.GetMembersOptions{})
if err != nil {
errResp := handleNotFoundError(err, d)
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand All @@ -108,6 +77,38 @@ func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{
return nil
})
}
func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)
teamID := d.Get("team_id").(string)
role := d.Get("role").(string)

log.Printf("[DEBUG] Adding user: %s to team: %s with role: %s", userID, teamID, role)

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.Teams.AddUserWithRole(teamID, userID, role); err != nil {
if isErrCode(err, 500) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

return nil
})
if retryErr != nil {
return retryErr
}

d.SetId(fmt.Sprintf("%s:%s", userID, teamID))

return fetchPagerDutyTeamMembership(d, meta, genError)
}

func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
return fetchPagerDutyTeamMembership(d, meta, handleNotFoundError)
}

func resourcePagerDutyTeamMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
Expand Down
40 changes: 22 additions & 18 deletions pagerduty/resource_pagerduty_user_contact_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,9 @@ func buildUserContactMethodStruct(d *schema.ResourceData) *pagerduty.ContactMeth

return contactMethod
}
func resourcePagerDutyUserContactMethodCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

contactMethod := buildUserContactMethodStruct(d)

resp, _, err := client.Users.CreateContactMethod(userID, contactMethod)
if err != nil {
return err
}

d.SetId(resp.ID)

return resourcePagerDutyUserContactMethodRead(d, meta)
}

func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyUserContactMethod(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
Expand All @@ -138,6 +121,27 @@ func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interfa
})
}

func resourcePagerDutyUserContactMethodCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

contactMethod := buildUserContactMethodStruct(d)

resp, _, err := client.Users.CreateContactMethod(userID, contactMethod)
if err != nil {
return err
}

d.SetId(resp.ID)

return fetchPagerDutyUserContactMethod(d, meta, genError)
}

func resourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error {
return fetchPagerDutyUserContactMethod(d, meta, handleNotFoundError)
}

func resourcePagerDutyUserContactMethodUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

Expand Down
43 changes: 23 additions & 20 deletions pagerduty/resource_pagerduty_user_notification_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,14 @@ func buildUserNotificationRuleStruct(d *schema.ResourceData) *pagerduty.Notifica
return notificationRule
}

func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyUserNotificationRule(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

notificationRule := buildUserNotificationRuleStruct(d)

resp, _, err := client.Users.CreateNotificationRule(userID, notificationRule)
if err != nil {
return err
}

d.SetId(resp.ID)

return resourcePagerDutyUserNotificationRuleRead(d, meta)
}

func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Users.GetNotificationRule(userID, d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand All @@ -119,6 +101,27 @@ func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta inte
})
}

func resourcePagerDutyUserNotificationRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

userID := d.Get("user_id").(string)

notificationRule := buildUserNotificationRuleStruct(d)

resp, _, err := client.Users.CreateNotificationRule(userID, notificationRule)
if err != nil {
return err
}

d.SetId(resp.ID)

return fetchPagerDutyUserNotificationRule(d, meta, genError)
}

func resourcePagerDutyUserNotificationRuleRead(d *schema.ResourceData, meta interface{}) error {
return fetchPagerDutyUserNotificationRule(d, meta, handleNotFoundError)
}

func resourcePagerDutyUserNotificationRuleUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

Expand Down

0 comments on commit 75e5f60

Please sign in to comment.