Skip to content
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/github_team_membership: Make role updatable #137

Merged
merged 2 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions github/resource_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
func resourceGithubMembership() *schema.Resource {

return &schema.Resource{
Create: resourceGithubMembershipCreate,
Create: resourceGithubMembershipCreateOrUpdate,
Read: resourceGithubMembershipRead,
Update: resourceGithubMembershipUpdate,
Update: resourceGithubMembershipCreateOrUpdate,
Delete: resourceGithubMembershipDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -35,7 +35,7 @@ func resourceGithubMembership() *schema.Resource {
}
}

func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error {
func resourceGithubMembershipCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

membership, _, err := client.Organizations.EditOrgMembership(context.TODO(),
Expand Down Expand Up @@ -74,24 +74,6 @@ func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) erro
return nil
}

func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

membership, _, err := client.Organizations.EditOrgMembership(context.TODO(),
d.Get("username").(string),
meta.(*Organization).name,
&github.Membership{
Role: github.String(d.Get("role").(string)),
},
)
if err != nil {
return err
}
d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login))

return nil
}

func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

Expand Down
7 changes: 3 additions & 4 deletions github/resource_github_team_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
func resourceGithubTeamMembership() *schema.Resource {

return &schema.Resource{
Create: resourceGithubTeamMembershipCreate,
Create: resourceGithubTeamMembershipCreateOrUpdate,
Read: resourceGithubTeamMembershipRead,
Update: resourceGithubTeamMembershipCreateOrUpdate,
Delete: resourceGithubTeamMembershipDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

// editing team memberships are not supported by github api so forcing new on any changes
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Expand All @@ -34,15 +34,14 @@ func resourceGithubTeamMembership() *schema.Resource {
"role": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "member",
ValidateFunc: validateValueFunc([]string{"member", "maintainer"}),
},
},
}
}

func resourceGithubTeamMembershipCreate(d *schema.ResourceData, meta interface{}) error {
func resourceGithubTeamMembershipCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

teamIdString := d.Get("team_id").(string)
Expand Down
58 changes: 20 additions & 38 deletions github/resource_github_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,20 @@ func TestAccGithubTeamMembership_basic(t *testing.T) {
var membership github.Membership
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

testAccGithubTeamMembershipUpdateConfig := fmt.Sprintf(`
resource "github_membership" "test_org_membership" {
username = "%s"
role = "member"
}

resource "github_team" "test_team" {
name = "tf-acc-test-team-membership-%s"
description = "Terraform acc test group"
}

resource "github_team_membership" "test_team_membership" {
team_id = "${github_team.test_team.id}"
username = "%s"
role = "maintainer"
}
`, testCollaborator, randString, testCollaborator)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubTeamMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubTeamMembershipConfig(randString, testCollaborator),
Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "member"),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "member", &membership),
),
},
{
Config: testAccGithubTeamMembershipUpdateConfig,
Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "maintainer"),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "maintainer", &membership),
Expand All @@ -66,7 +48,7 @@ func TestAccGithubTeamMembership_importBasic(t *testing.T) {
CheckDestroy: testAccCheckGithubTeamMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubTeamMembershipConfig(randString, testCollaborator),
Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "member"),
},
{
ResourceName: "github_team_membership.test_team_membership",
Expand Down Expand Up @@ -183,22 +165,22 @@ func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *g
}
}

func testAccGithubTeamMembershipConfig(randString, username string) string {
func testAccGithubTeamMembershipConfig(randString, username, role string) string {
return fmt.Sprintf(`
resource "github_membership" "test_org_membership" {
username = "%s"
role = "member"
}

resource "github_team" "test_team" {
name = "tf-acc-test-team-membership-%s"
description = "Terraform acc test group"
}

resource "github_team_membership" "test_team_membership" {
team_id = "${github_team.test_team.id}"
username = "%s"
role = "member"
}
`, username, randString, username)
resource "github_membership" "test_org_membership" {
username = "%s"
role = "member"
}

resource "github_team" "test_team" {
name = "tf-acc-test-team-membership-%s"
description = "Terraform acc test group"
}

resource "github_team_membership" "test_team_membership" {
team_id = "${github_team.test_team.id}"
username = "%s"
role = "%s"
}
`, username, randString, username, role)
}