2016-03-08 23:05:22 +01:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-19 02:28:46 +02:00
|
|
|
"os"
|
2016-03-08 23:05:22 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGithubTeamMembership_basic(t *testing.T) {
|
|
|
|
var membership github.Membership
|
|
|
|
|
2016-03-19 03:55:33 +01:00
|
|
|
testUser := os.Getenv("GITHUB_TEST_USER")
|
|
|
|
testAccGithubTeamMembershipConfig := fmt.Sprintf(`
|
|
|
|
resource "github_membership" "test_org_membership" {
|
|
|
|
username = "%s"
|
|
|
|
role = "member"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "github_team" "test_team" {
|
|
|
|
name = "foo"
|
|
|
|
description = "Terraform acc test group"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "github_team_membership" "test_team_membership" {
|
|
|
|
team_id = "${github_team.test_team.id}"
|
|
|
|
username = "%s"
|
|
|
|
role = "member"
|
|
|
|
}
|
|
|
|
`, testUser, testUser)
|
|
|
|
|
|
|
|
testAccGithubTeamMembershipUpdateConfig := fmt.Sprintf(`
|
|
|
|
resource "github_membership" "test_org_membership" {
|
|
|
|
username = "%s"
|
|
|
|
role = "member"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "github_team" "test_team" {
|
|
|
|
name = "foo"
|
|
|
|
description = "Terraform acc test group"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "github_team_membership" "test_team_membership" {
|
|
|
|
team_id = "${github_team.test_team.id}"
|
|
|
|
username = "%s"
|
|
|
|
role = "maintainer"
|
|
|
|
}
|
|
|
|
`, testUser, testUser)
|
|
|
|
|
2016-03-08 23:05:22 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGithubTeamMembershipDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccGithubTeamMembershipConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
|
|
|
|
testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "member", &membership),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccGithubTeamMembershipUpdateConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
|
|
|
|
testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "maintainer", &membership),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "github_team_membership" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t, u := parseTwoPartID(rs.Primary.ID)
|
|
|
|
membership, resp, err := conn.Organizations.GetTeamMembership(toGithubID(t), u)
|
|
|
|
if err == nil {
|
|
|
|
if membership != nil {
|
|
|
|
return fmt.Errorf("Team membership still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubTeamMembershipExists(n string, membership *github.Membership) 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("No team membership ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
t, u := parseTwoPartID(rs.Primary.ID)
|
|
|
|
|
|
|
|
teamMembership, _, err := conn.Organizations.GetTeamMembership(toGithubID(t), u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*membership = *teamMembership
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *github.Membership) 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("No team membership ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
t, u := parseTwoPartID(rs.Primary.ID)
|
|
|
|
|
|
|
|
teamMembership, _, err := conn.Organizations.GetTeamMembership(toGithubID(t), u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceRole := membership.Role
|
|
|
|
actualRole := teamMembership.Role
|
|
|
|
|
|
|
|
if *resourceRole != expected {
|
|
|
|
return fmt.Errorf("Team membership role %v in resource does match expected state of %v", *resourceRole, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *resourceRole != *actualRole {
|
|
|
|
return fmt.Errorf("Team membership role %v in resource does match actual state of %v", *resourceRole, *actualRole)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|