Skip to content

Commit

Permalink
Get updated test working, incl fixing test utility funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Davis committed Jul 5, 2024
1 parent ea3d095 commit 4ec40c0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 61 deletions.
59 changes: 58 additions & 1 deletion pagerduty/resource_pagerduty_team_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@ func resourcePagerDutyTeamMembershipUpdate(d *schema.ResourceData, meta interfac
return fetchPagerDutyTeamMembershipWithRetries(d, meta, genError, 0, d.Get("role").(string))
}

func getAllEPsForTeam(client *pagerduty.Client, teamID string) ([]string, error) {
const maxLimit = 100
var escalationPolicyIDs []string
opts := &pagerduty.ListEscalationPoliciesOptions{
TeamIDs: []string{teamID},
Limit: maxLimit,
Offset: 0,
}

for {
listResp, _, err := client.EscalationPolicies.List(opts)
if err != nil {
return nil, err
}

for _, ep := range listResp.EscalationPolicies {
escalationPolicyIDs = append(escalationPolicyIDs, ep.ID)
}

if !listResp.More {
break
}
opts.Offset += opts.Limit
}
return escalationPolicyIDs, nil
}

func filterEPsAssociatedToTeam(client *pagerduty.Client, teamID string, epsAssociatedToUser []string) ([]string, error) {
// take epsAssociatedToUser and return EP IDs asscociated with teamID
epIdsAssociatedWithTeam, err := getAllEPsForTeam(client, teamID)
if err != nil {
return nil, err
}

epsInBoth := intersect(epIdsAssociatedWithTeam, epsAssociatedToUser)
return epsInBoth, nil
}

func resourcePagerDutyTeamMembershipDelete(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
Expand All @@ -209,7 +247,12 @@ func resourcePagerDutyTeamMembershipDelete(d *schema.ResourceData, meta interfac
return err
}

epsDissociatedFromTeam, err := dissociateEPsFromTeam(client, teamID, epsAssociatedToUser)
epsAssociatedWithUserAndTeam, err := filterEPsAssociatedToTeam(client, teamID, epsAssociatedToUser)
if err != nil {
return err
}

epsDissociatedFromTeam, err := dissociateEPsFromTeam(client, teamID, epsAssociatedWithUserAndTeam)
if err != nil {
return err
}
Expand Down Expand Up @@ -336,3 +379,17 @@ func isTeamMember(user *pagerduty.User, teamID string) bool {

return found
}

func intersect(slice1, slice2 []string) []string {
m := make(map[string]struct{})
for _, item := range slice1 {
m[item] = struct{}{}
}
var intersection []string
for _, item := range slice2 {
if _, found := m[item]; found {
intersection = append(intersection, item)
}
}
return intersection
}
61 changes: 6 additions & 55 deletions pagerduty/resource_pagerduty_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,55 +316,29 @@ func TestAccPagerDutyTeamMembership_basic(t *testing.T) { // from gpt and modifi
{
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant_UnrelatedEPs(user, team1, team2, role, escalationPolicy1, escalationPolicy2),
Check: resource.ComposeTestCheckFunc(
printdebug("checkpoint1-1\n\n"),
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"),
printdebug("checkpoint1-2\n\n"),

testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.bar"),
printdebug("checkpoint1-3\n\n"),

resource.TestCheckResourceAttr("pagerduty_team_membership.bar", "role", "manager"),
printdebug("checkpoint1-4\n\n"),
),
},
{
// ResourceName: "pagerduty_team_membership.foo",
// ImportState: true,
// RefreshState: true,
Config: testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant_UnrelatedEPsUpdated(user, team1, team2, role, escalationPolicy1, escalationPolicy2),
Check: resource.ComposeTestCheckFunc(
printdebug("checkpoint2-1\n\n"),

testAccCheckPagerDutyTeamMembershipDestroyState("pagerduty_team_membership.foo"),
printdebug("checkpoint2-2\n\n"),

testAccCheckPagerDutyTeamExists("pagerduty_team.foobar"),
printdebug("checkpoint2-3\n\n"),

testAccCheckPagerDutyTeamMembershipNoExists("pagerduty_team_membership.foo"),
testAccCheckPagerDutyTeamExists("pagerduty_team.foo"),
testAccCheckPagerDutyTeamExists("pagerduty_team.bar"),
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
printdebug("checkpoint2-4\n\n"),

testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.bar"),
printdebug("checkpoint2-5\n\n"),

testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.foo", "foo"),
printdebug("checkpoint2-6\n\n"),

testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches("pagerduty_escalation_policy.bar", "bar"),
printdebug("checkpoint2-7\n\n"),
),
},
},
})
}

func printdebug(msg string) resource.TestCheckFunc {
return func(s *terraform.State) error {
fmt.Printf(msg)
return nil
}
}

// update this tomorrow
// the check should involve a call to the API and inspecting the state on the server
func testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches(n, expectedTeam string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -387,21 +361,6 @@ func testAccCheckPagerDutyEscalationPolicyTeamsFieldMatches(n, expectedTeam stri
}
}

func testAccCheckPagerDutyTeamMembershipDestroyState(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID != "" {
return fmt.Errorf("Resource still exists: %s", n)
}
return fmt.Errorf("dummy error: %s", "wow")
// return nil
}
}

func testAccCheckPagerDutyTeamMembershipDestroyWithEscalationPolicyDependant_UnrelatedEPs(user, team1, team2, role, escalationPolicy1, escalationPolicy2 string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
Expand Down Expand Up @@ -435,8 +394,6 @@ resource "pagerduty_escalation_policy" "foo" {
name = "%[4]v"
num_loops = 2
teams = [pagerduty_team.foo.id]
description = "foo"
rule {
escalation_delay_in_minutes = 10
target {
Expand All @@ -450,8 +407,6 @@ resource "pagerduty_escalation_policy" "bar" {
name = "%[6]v"
num_loops = 2
teams = [pagerduty_team.bar.id]
description = "bar"
rule {
escalation_delay_in_minutes = 10
target {
Expand All @@ -460,6 +415,7 @@ resource "pagerduty_escalation_policy" "bar" {
}
}
}
`, user, team1, role, escalationPolicy1, team2, escalationPolicy2)
}

Expand All @@ -479,8 +435,6 @@ resource "pagerduty_team" "bar" {
name = "%[5]v"
description = "bar"
}
// now the user should be on a single team
resource "pagerduty_team_membership" "bar" {
user_id = data.pagerduty_user.foo.id
team_id = pagerduty_team.bar.id
Expand All @@ -491,7 +445,6 @@ resource "pagerduty_escalation_policy" "foo" {
name = "%[4]v"
num_loops = 2
teams = [pagerduty_team.foo.id]
rule {
escalation_delay_in_minutes = 10
target {
Expand All @@ -504,9 +457,7 @@ resource "pagerduty_escalation_policy" "foo" {
resource "pagerduty_escalation_policy" "bar" {
name = "%[6]v"
num_loops = 2
description = "bar"
teams = [pagerduty_team.bar.id]
rule {
escalation_delay_in_minutes = 10
target {
Expand Down
12 changes: 7 additions & 5 deletions pagerdutyplugin/resource_pagerduty_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ func testAccCheckPagerDutyTeamDestroy(s *terraform.State) error {
return nil
}

func testAccCheckPagerDutyTeamExists(_ string) resource.TestCheckFunc {
func testAccCheckPagerDutyTeamExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, r := range s.RootModule().Resources {
ctx := context.Background()
if _, err := testAccProvider.client.GetTeamWithContext(ctx, r.Primary.ID); err != nil {
return fmt.Errorf("Received an error retrieving team %s ID: %s", err, r.Primary.ID)
client, _ := testAccProvider.Meta().(*Config).Client()
for name, r := range s.RootModule().Resources {
if name == n {
if _, _, err := client.Teams.Get(r.Primary.ID); err != nil {
return fmt.Errorf("Received an error retrieving team %s ID: %s", err, r.Primary.ID)
}
}
}
return nil
Expand Down

0 comments on commit 4ec40c0

Please sign in to comment.