2016-03-08 23:05:22 +01:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-03-12 15:05:18 +01:00
|
|
|
"context"
|
2016-03-08 23:05:22 +01:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
2017-03-17 11:47:43 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-03-08 23:05:22 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGithubTeam_basic(t *testing.T) {
|
|
|
|
var team github.Team
|
2017-03-17 11:47:43 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
name := fmt.Sprintf("tf-acc-test-%s", randString)
|
|
|
|
updatedName := fmt.Sprintf("tf-acc-test-updated-%s", randString)
|
2016-03-08 23:05:22 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGithubTeamDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubTeamConfig(randString),
|
2016-03-08 23:05:22 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubTeamExists("github_team.foo", &team),
|
2017-03-17 11:47:43 +01:00
|
|
|
testAccCheckGithubTeamAttributes(&team, name, "Terraform acc test group"),
|
2016-03-08 23:05:22 +01:00
|
|
|
),
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubTeamUpdateConfig(randString),
|
2016-03-08 23:05:22 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubTeamExists("github_team.foo", &team),
|
2017-03-17 11:47:43 +01:00
|
|
|
testAccCheckGithubTeamAttributes(&team, updatedName, "Terraform acc test group - updated"),
|
2016-03-08 23:05:22 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-28 18:30:24 +01:00
|
|
|
func TestAccGithubTeam_importBasic(t *testing.T) {
|
2017-03-17 11:47:43 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
|
2016-11-28 18:30:24 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGithubTeamDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubTeamConfig(randString),
|
2016-11-28 18:30:24 +01:00
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2016-11-28 18:30:24 +01:00
|
|
|
ResourceName: "github_team.foo",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-08 23:05:22 +01:00
|
|
|
func testAccCheckGithubTeamExists(n string, team *github.Team) 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 ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
2017-03-12 15:05:18 +01:00
|
|
|
githubTeam, _, err := conn.Organizations.GetTeam(context.TODO(), toGithubID(rs.Primary.ID))
|
2016-03-08 23:05:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*team = *githubTeam
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubTeamAttributes(team *github.Team, name, description string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if *team.Name != name {
|
|
|
|
return fmt.Errorf("Team name does not match: %s, %s", *team.Name, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *team.Description != description {
|
|
|
|
return fmt.Errorf("Team description does not match: %s, %s", *team.Description, description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubTeamDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "github_team" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:05:18 +01:00
|
|
|
team, resp, err := conn.Organizations.GetTeam(context.TODO(), toGithubID(rs.Primary.ID))
|
2016-03-08 23:05:22 +01:00
|
|
|
if err == nil {
|
|
|
|
if team != nil &&
|
|
|
|
fromGithubID(team.ID) == rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Team still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:43 +01:00
|
|
|
func testAccGithubTeamConfig(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-03-08 23:05:22 +01:00
|
|
|
resource "github_team" "foo" {
|
2017-03-17 11:47:43 +01:00
|
|
|
name = "tf-acc-test-%s"
|
2016-03-08 23:05:22 +01:00
|
|
|
description = "Terraform acc test group"
|
2016-04-11 14:09:25 +02:00
|
|
|
privacy = "secret"
|
2016-03-08 23:05:22 +01:00
|
|
|
}
|
2017-03-17 11:47:43 +01:00
|
|
|
`, randString)
|
|
|
|
}
|
2016-03-08 23:05:22 +01:00
|
|
|
|
2017-03-17 11:47:43 +01:00
|
|
|
func testAccGithubTeamUpdateConfig(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-03-08 23:05:22 +01:00
|
|
|
resource "github_team" "foo" {
|
2017-03-17 11:47:43 +01:00
|
|
|
name = "tf-acc-test-updated-%s"
|
2016-03-08 23:05:22 +01:00
|
|
|
description = "Terraform acc test group - updated"
|
2016-04-11 14:09:25 +02:00
|
|
|
privacy = "closed"
|
2016-03-08 23:05:22 +01:00
|
|
|
}
|
2017-03-17 11:47:43 +01:00
|
|
|
`, randString)
|
|
|
|
}
|