-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve team_membership deletion logic
This seeks to fix issue #913 by only diassociating/re-associating the _team_-specific escalation policies for which the user-whose-membership-is-to-be-deleted is a rule target (rather than attempting to diassociate/re-associate _all_ the user-associated escalation policies to the team, as those escalation policies may be taken by other teams). In other words... Previously, team membership deletion attempted to diassociate _all_ escalation policies associated to the user from the team, then re-associate those policies back to the team. This could run into the following error if/when that user was a member of multiple teams (see issue #913 for details) and associated to escalation policies associated to _other_ teams: ``` pagerduty_team_membership.redacted: Still destroying... [id=REDACTED:REDACTED, 2m0s elapsed] | │ Error: PUT API call to https://api.pagerduty.com/teams/team1/escalation_policies/escalation_policy2 failed 400 Bad Request. Code: 2001, Errors: <nil>, Message: Escalation Policy has already been taken; Error while trying to associate back team "team1" to Escalation Policy "escalation_policy2". Resource succesfully deleted, but some team association couldn't be completed, so you need to run "terraform plan -refresh-only" and again "terraform apply/destroy" in order to remediate the drift ``` This seeks to fix that :)
- Loading branch information
Showing
2 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,35 @@ func TestAccPagerDutyTeamMembership_DestroyWithEscalationPolicyDependant(t *test | |
}) | ||
} | ||
|
||
func TestAccPagerDutyTeamMembership_DestroyWithEscalationPolicyDependantAndMultipleTeams(t *testing.T) { | ||
userOne := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
teamOne := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
teamTwo := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
role := "manager" | ||
escalationPolicyOne := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
escalationPolicyTwo := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyTeamMembershipDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAndMultipleTeams(userOne, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.one"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAndMultipleTeamsUpdated(userOne, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamMembershipNoExists("pagerduty_team_membership.one"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipDestroy(s *terraform.State) error { | ||
client, _ := testAccProvider.Meta().(*Config).Client() | ||
for _, r := range s.RootModule().Resources { | ||
|
@@ -297,3 +326,115 @@ resource "pagerduty_escalation_policy" "foo" { | |
} | ||
`, user, team, role, escalationPolicy) | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAndMultipleTeams(user, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "one" { | ||
name = "%[1]v" | ||
email = "%[1][email protected]" | ||
} | ||
resource "pagerduty_team" "one" { | ||
name = "%[2]v" | ||
description = "team_one" | ||
} | ||
resource "pagerduty_team" "two" { | ||
name = "%[3]v" | ||
description = "team_two" | ||
} | ||
resource "pagerduty_team_membership" "one" { | ||
user_id = pagerduty_user.one.id | ||
team_id = pagerduty_team.one.id | ||
role = "%[4]v" | ||
} | ||
resource "pagerduty_team_membership" "two" { | ||
user_id = pagerduty_user.one.id | ||
team_id = pagerduty_team.two.id | ||
role = "%[4]v" | ||
} | ||
resource "pagerduty_escalation_policy" "one" { | ||
name = "%s" | ||
num_loops = 2 | ||
teams = [pagerduty_team.one.id] | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.one.id | ||
} | ||
} | ||
} | ||
resource "pagerduty_escalation_policy" "two" { | ||
name = "%s" | ||
num_loops = 2 | ||
teams = [pagerduty_team.two.id] | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.one.id | ||
} | ||
} | ||
} | ||
`, user, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo) | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependantAndMultipleTeamsUpdated(user, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "one" { | ||
name = "%[1]v" | ||
email = "%[1][email protected]" | ||
} | ||
resource "pagerduty_team" "one" { | ||
name = "%[2]v" | ||
description = "team_one" | ||
} | ||
resource "pagerduty_team" "two" { | ||
name = "%[3]v" | ||
description = "team_two" | ||
} | ||
resource "pagerduty_team_membership" "two" { | ||
user_id = pagerduty_user.one.id | ||
team_id = pagerduty_team.two.id | ||
role = "%[4]v" | ||
} | ||
resource "pagerduty_escalation_policy" "one" { | ||
name = "%s" | ||
num_loops = 2 | ||
teams = [pagerduty_team.one.id] | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.one.id | ||
} | ||
} | ||
} | ||
resource "pagerduty_escalation_policy" "two" { | ||
name = "%s" | ||
num_loops = 2 | ||
teams = [pagerduty_team.two.id] | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.one.id | ||
} | ||
} | ||
} | ||
`, user, teamOne, teamTwo, role, escalationPolicyOne, escalationPolicyTwo) | ||
} |