Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Teams may be nested
Browse files Browse the repository at this point in the history
  • Loading branch information
alindeman authored and paultyng committed May 11, 2018
1 parent ca9732c commit a60a056
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
29 changes: 25 additions & 4 deletions github/resource_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func resourceGithubTeam() *schema.Resource {
Default: "secret",
ValidateFunc: validateValueFunc([]string{"secret", "closed"}),
},
"parent_team_id": {
Type: schema.TypeInt,
Optional: true,
},
"ldap_dn": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -46,11 +50,18 @@ func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
n := d.Get("name").(string)
desc := d.Get("description").(string)
p := d.Get("privacy").(string)
githubTeam, _, err := client.Organizations.CreateTeam(context.TODO(), meta.(*Organization).name, &github.NewTeam{

newTeam := &github.NewTeam{
Name: n,
Description: &desc,
Privacy: &p,
})
}
if parentTeamID, ok := d.GetOk("parent_team_id"); ok {
id := int64(parentTeamID.(int))
newTeam.ParentTeamID = &id
}

githubTeam, _, err := client.Organizations.CreateTeam(context.TODO(), meta.(*Organization).name, newTeam)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,6 +91,11 @@ func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
d.Set("description", team.Description)
d.Set("name", team.Name)
d.Set("privacy", team.Privacy)
if parent := team.Parent; parent != nil {
d.Set("parent_team_id", parent.GetID())
} else {
d.Set("parent_team_id", "")
}
d.Set("ldap_dn", team.GetLDAPDN())
return nil
}
Expand All @@ -96,13 +112,18 @@ func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
description := d.Get("description").(string)
privacy := d.Get("privacy").(string)
editedTeam := github.NewTeam{

editedTeam := &github.NewTeam{
Name: name,
Description: &description,
Privacy: &privacy,
}
if parentTeamID, ok := d.GetOk("parent_team_id"); ok {
id := int64(parentTeamID.(int))
editedTeam.ParentTeamID = &id
}

team, _, err = client.Organizations.EditTeam(context.TODO(), *team.ID, &editedTeam)
team, _, err = client.Organizations.EditTeam(context.TODO(), *team.ID, editedTeam)
if err != nil {
return err
}
Expand Down
54 changes: 51 additions & 3 deletions github/resource_github_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,38 @@ func TestAccGithubTeam_basic(t *testing.T) {
Config: testAccGithubTeamConfig(randString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubTeamExists("github_team.foo", &team),
testAccCheckGithubTeamAttributes(&team, name, "Terraform acc test group"),
testAccCheckGithubTeamAttributes(&team, name, "Terraform acc test group", nil),
),
},
{
Config: testAccGithubTeamUpdateConfig(randString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubTeamExists("github_team.foo", &team),
testAccCheckGithubTeamAttributes(&team, updatedName, "Terraform acc test group - updated"),
testAccCheckGithubTeamAttributes(&team, updatedName, "Terraform acc test group - updated", nil),
),
},
},
})
}

func TestAccGithubTeam_hierarchical(t *testing.T) {
var parent, child github.Team
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
parentName := fmt.Sprintf("tf-acc-parent-%s", randString)
childName := fmt.Sprintf("tf-acc-child-%s", randString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubTeamDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubTeamHierarchicalConfig(randString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubTeamExists("github_team.parent", &parent),
testAccCheckGithubTeamAttributes(&parent, parentName, "Terraform acc test parent team", nil),
testAccCheckGithubTeamExists("github_team.child", &child),
testAccCheckGithubTeamAttributes(&child, childName, "Terraform acc test child team", &parent),
),
},
},
Expand Down Expand Up @@ -81,7 +105,7 @@ func testAccCheckGithubTeamExists(n string, team *github.Team) resource.TestChec
}
}

func testAccCheckGithubTeamAttributes(team *github.Team, name, description string) resource.TestCheckFunc {
func testAccCheckGithubTeamAttributes(team *github.Team, name, description string, parentTeam *github.Team) 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)
Expand All @@ -91,6 +115,14 @@ func testAccCheckGithubTeamAttributes(team *github.Team, name, description strin
return fmt.Errorf("Team description does not match: %s, %s", *team.Description, description)
}

if parentTeam == nil && team.Parent != nil {
return fmt.Errorf("Team parent ID was expected to be empty, but was %d", team.Parent.GetID())
} else if parentTeam != nil && team.Parent == nil {
return fmt.Errorf("Team parent ID was expected to be %d, but was not present", parentTeam.GetID())
} else if parentTeam != nil && team.Parent.GetID() != parentTeam.GetID() {
return fmt.Errorf("Team parent ID does not match: %d, %d", team.Parent.GetID(), parentTeam.GetID())
}

return nil
}
}
Expand Down Expand Up @@ -137,3 +169,19 @@ resource "github_team" "foo" {
}
`, randString)
}

func testAccGithubTeamHierarchicalConfig(randString string) string {
return fmt.Sprintf(`
resource "github_team" "parent" {
name = "tf-acc-parent-%s"
description = "Terraform acc test parent team"
privacy = "closed"
}
resource "github_team" "child" {
name = "tf-acc-child-%s"
description = "Terraform acc test child team"
privacy = "closed"
parent_team_id = "${github_team.parent.id}"
}
`, randString, randString)
}
1 change: 1 addition & 0 deletions website/docs/r/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following arguments are supported:
* `description` - (Optional) A description of the team.
* `privacy` - (Optional) The level of privacy for the team. Must be one of `secret` or `closed`.
Defaults to `secret`.
* `parent_team_id` - (Optional) The ID of the parent team, if this is a nested team.
* `ldap_dn` - (Optional) The LDAP Distinguished Name of the group where membership will be synchronized. Only available in GitHub Enterprise.

## Attributes Reference
Expand Down

0 comments on commit a60a056

Please sign in to comment.