-
Notifications
You must be signed in to change notification settings - Fork 216
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
resource/pagerduty_team_membership: Support team member's role #151
Changes from 3 commits
40124d8
b3875bd
5e12f83
89b0e5f
6ce1d0c
003ba6c
8521226
3a0aaca
a560e09
70201a4
1f71c0c
48bb3b1
a4832b0
2a72eb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,12 @@ func resourcePagerDutyTeamMembership() *schema.Resource { | |
Required: true, | ||
ForceNew: true, | ||
}, | ||
"role": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation could probably be added:
https://api-reference.pagerduty.com/#!/Teams/put_teams_id_users_user_id There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! hashicorp@8521226 |
||
Type: schema.TypeString, | ||
Optional: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember there are issues when hashicorp/terraform-provider-google#3477 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to |
||
Computed: true, | ||
ForceNew: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not explicitely documented, but it is not required to recreate the team membership resource when only the role changes.
Not recreating the team membership avoids the user loosing the role even for a very short period. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I added
|
||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -38,11 +44,12 @@ func resourcePagerDutyTeamMembershipCreate(d *schema.ResourceData, meta interfac | |
|
||
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", userID, teamID) | ||
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.AddUser(teamID, userID); err != nil { | ||
if _, err := client.Teams.AddUserWithRole(teamID, userID, role); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the role is empty? Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I defined a default value for With validation implemented at hashicorp@8521226, this resource guarantees a valid value is set in |
||
if isErrCode(err, 500) { | ||
return resource.RetryableError(err) | ||
} | ||
|
@@ -73,7 +80,21 @@ func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{ | |
return err | ||
} | ||
|
||
if !isTeamMember(user, teamID) { | ||
d.Set("role", "") | ||
|
||
if isTeamMember(user, teamID) { | ||
resp, _, err := client.Teams.GetMembers(teamID, &pagerduty.GetMembersOptions{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't This could probably simplify things a bit, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using How about this? |
||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, member := range resp.Members { | ||
if member.User.ID == userID { | ||
d.Set("role", member.Role) | ||
break | ||
} | ||
} | ||
} else { | ||
log.Printf("[WARN] Removing %s since the user: %s is not a member of: %s", d.Id(), userID, teamID) | ||
d.SetId("") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,15 @@ import ( | |
func TestAccPagerDutyTeamMembership_Basic(t *testing.T) { | ||
user := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
team := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
role := "manager" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original test should probably be left untouched to ensure no regression is introduced. Maybe a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Created a new testcase for withRole. |
||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyTeamMembershipDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyTeamMembershipConfig(user, team), | ||
Config: testAccCheckPagerDutyTeamMembershipConfig(user, team, role), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"), | ||
), | ||
|
@@ -62,6 +63,7 @@ func testAccCheckPagerDutyTeamMembershipExists(n string) resource.TestCheckFunc | |
|
||
userID := rs.Primary.Attributes["user_id"] | ||
teamID := rs.Primary.Attributes["team_id"] | ||
role := rs.Primary.Attributes["role"] | ||
|
||
user, _, err := client.Users.Get(userID, &pagerduty.GetUserOptions{}) | ||
if err != nil { | ||
|
@@ -72,11 +74,24 @@ func testAccCheckPagerDutyTeamMembershipExists(n string) resource.TestCheckFunc | |
return fmt.Errorf("%s is not a member of: %s", userID, teamID) | ||
} | ||
|
||
resp, _, err := client.Teams.GetMembers(teamID, &pagerduty.GetMembersOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, member := range resp.Members { | ||
if member.User.ID == userID { | ||
if member.Role != role { | ||
return fmt.Errorf("%s does not have the role: %s in: %s", userID, role, teamID) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipConfig(user, team string) string { | ||
func testAccCheckPagerDutyTeamMembershipConfig(user, team, role string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "foo" { | ||
name = "%[1]v" | ||
|
@@ -91,6 +106,7 @@ resource "pagerduty_team" "foo" { | |
resource "pagerduty_team_membership" "foo" { | ||
user_id = "${pagerduty_user.foo.id}" | ||
team_id = "${pagerduty_team.foo.id}" | ||
role = "%[3]v" | ||
} | ||
`, user, team) | ||
`, user, team, role) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the original test should probably be left untouched to ensure no regression is introduced.
Maybe a new
TestAccPagerDutyTeamMembership_importWithRole
test should be introduced.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Created a new testcase for withRole.
hashicorp@6ce1d0c