-
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 all 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ func resourcePagerDutyTeamMembership() *schema.Resource { | |
return &schema.Resource{ | ||
Create: resourcePagerDutyTeamMembershipCreate, | ||
Read: resourcePagerDutyTeamMembershipRead, | ||
Update: resourcePagerDutyTeamMembershipUpdate, | ||
Delete: resourcePagerDutyTeamMembershipDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
|
@@ -30,6 +31,16 @@ 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 |
||
Default: "manager", | ||
ValidateFunc: validateValueFunc([]string{ | ||
"observer", | ||
"responder", | ||
"manager", | ||
}), | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -38,11 +49,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) | ||
} | ||
|
@@ -68,18 +80,53 @@ func resourcePagerDutyTeamMembershipRead(d *schema.ResourceData, meta interface{ | |
|
||
log.Printf("[DEBUG] Reading user: %s from team: %s", userID, teamID) | ||
|
||
user, _, err := client.Users.Get(userID, &pagerduty.GetUserOptions{}) | ||
resp, _, err := client.Teams.GetMembers(teamID, &pagerduty.GetMembersOptions{}) | ||
if err != nil { | ||
return handleNotFoundError(err, d) | ||
} | ||
|
||
if !isTeamMember(user, teamID) { | ||
log.Printf("[WARN] Removing %s since the user: %s is not a member of: %s", d.Id(), userID, teamID) | ||
d.SetId("") | ||
for _, member := range resp.Members { | ||
if member.User.ID == userID { | ||
d.Set("user_id", userID) | ||
d.Set("team_id", teamID) | ||
d.Set("role", member.Role) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
log.Printf("[WARN] Removing %s since the user: %s is not a member of: %s", d.Id(), userID, teamID) | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourcePagerDutyTeamMembershipUpdate(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] Updating user: %s to team: %s with role: %s", userID, teamID, role) | ||
|
||
// To update existing membership resource, We can use the same API as creating a new membership. | ||
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.Set("user_id", userID) | ||
d.Set("team_id", teamID) | ||
d.SetId(fmt.Sprintf("%s:%s", userID, teamID)) | ||
|
||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,26 @@ func TestAccPagerDutyTeamMembership_Basic(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccPagerDutyTeamMembership_WithRole(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: testAccCheckPagerDutyTeamMembershipWithRoleConfig(user, team, role), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamMembershipExists("pagerduty_team_membership.foo"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*pagerduty.Client) | ||
for _, r := range s.RootModule().Resources { | ||
|
@@ -62,6 +82,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,6 +93,19 @@ 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 | ||
} | ||
} | ||
|
@@ -94,3 +128,23 @@ resource "pagerduty_team_membership" "foo" { | |
} | ||
`, user, team) | ||
} | ||
|
||
func testAccCheckPagerDutyTeamMembershipWithRoleConfig(user, team, role string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "foo" { | ||
name = "%[1]v" | ||
email = "%[1]v@foo.com" | ||
} | ||
|
||
resource "pagerduty_team" "foo" { | ||
name = "%[2]v" | ||
description = "foo" | ||
} | ||
|
||
resource "pagerduty_team_membership" "foo" { | ||
user_id = "${pagerduty_user.foo.id}" | ||
team_id = "${pagerduty_team.foo.id}" | ||
role = "%[3]v" | ||
} | ||
`, user, team, role) | ||
} |
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