Skip to content

Commit

Permalink
Merge branch 'PagerDuty:master' into add_pagerduty_event_orchestratio…
Browse files Browse the repository at this point in the history
…ns_datasource
  • Loading branch information
fgouteroux authored Jan 27, 2023
2 parents fe26f68 + 5431f12 commit c3b73fb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.9.3 (January 26, 2023)

BUG FIXES:
* `resource/pagerduty_team_membership`: EF-3964 Address `team_membership` inconsistency after `create` and `update` ([#621](https://github.com/PagerDuty/terraform-provider-pagerduty/pull/621))

## 2.9.2 (January 17, 2023)

BUG FIXES:
Expand Down
35 changes: 33 additions & 2 deletions pagerduty/resource_pagerduty_team_membership.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/v2/helper/resource"
Expand Down Expand Up @@ -44,6 +45,36 @@ func resourcePagerDutyTeamMembership() *schema.Resource {
}
}

func maxRetries() int {
return 4
}

func retryDelayMs() int {
return 500
}

func calculateDelay(retryCount int) time.Duration {
return time.Duration(retryCount*retryDelayMs()) * time.Millisecond
}

func fetchPagerDutyTeamMembershipWithRetries(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error, retryCount int, neededRole string) error {
if retryCount >= maxRetries() {
return nil
}
if err := fetchPagerDutyTeamMembership(d, meta, errCallback); err != nil {
return err
}
fetchedRole, userId, teamId := d.Get("role").(string), d.Get("user_id"), d.Get("team_id")
if strings.Compare(neededRole, fetchedRole) == 0 {
return nil
}
log.Printf("[DEBUG] Warning role '%s' fetched from PD is different from the role '%s' from config for user: %s from team: %s, retrying...", fetchedRole, neededRole, userId, teamId)

retryCount++
time.Sleep(calculateDelay(retryCount))
return fetchPagerDutyTeamMembershipWithRetries(d, meta, errCallback, retryCount, neededRole)
}

func fetchPagerDutyTeamMembership(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, err := meta.(*Config).Client()
if err != nil {
Expand Down Expand Up @@ -109,7 +140,7 @@ func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interfac

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

return fetchPagerDutyTeamMembership(d, meta, genError)
return fetchPagerDutyTeamMembershipWithRetries(d, meta, genError, 0, d.Get("role").(string))
}

func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -146,7 +177,7 @@ func resourcePagerDutyTeamMembershipUpdate(d *schema.ResourceData, meta interfac

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

return nil
return fetchPagerDutyTeamMembershipWithRetries(d, meta, genError, 0, d.Get("role").(string))
}

func resourcePagerDutyTeamMembershipDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
31 changes: 31 additions & 0 deletions pagerduty/resource_pagerduty_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ func TestAccPagerDutyTeamMembership_WithRole(t *testing.T) {
})
}

func TestAccPagerDutyTeamMembership_WithRoleConsistentlyAssigned(t *testing.T) {
user := fmt.Sprintf("tf-%s", acctest.RandString(5))
team := fmt.Sprintf("tf-%s", acctest.RandString(5))
firstRole := "observer"
secondRole := "responder"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyTeamMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyTeamMembershipWithRoleConfig(user, team, firstRole),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"),
resource.TestCheckResourceAttr(
"pagerduty_team_membership.foo", "role", firstRole),
),
},
{
Config: testAccCheckPagerDutyTeamMembershipWithRoleConfig(user, team, secondRole),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"),
resource.TestCheckResourceAttr(
"pagerduty_team_membership.foo", "role", secondRole),
),
},
},
})
}

func TestAccPagerDutyTeamMembership_DestroyWithEscalationPolicyDependant(t *testing.T) {
user := fmt.Sprintf("tf-%s", acctest.RandString(5))
team := fmt.Sprintf("tf-%s", acctest.RandString(5))
Expand Down

0 comments on commit c3b73fb

Please sign in to comment.