diff --git a/github/data_source_github_organization_teams.go b/github/data_source_github_organization_teams.go index b7b6778534..2b85f43299 100644 --- a/github/data_source_github_organization_teams.go +++ b/github/data_source_github_organization_teams.go @@ -2,6 +2,7 @@ package github import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/shurcooL/githubv4" ) @@ -15,6 +16,17 @@ func dataSourceGithubOrganizationTeams() *schema.Resource { Optional: true, Default: false, }, + "summary_only": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "results_per_page": { + Type: schema.TypeInt, + Optional: true, + Default: 100, + ValidateFunc: validation.IntBetween(0, 100), + }, "teams": { Type: schema.TypeList, Computed: true, @@ -70,13 +82,17 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac client := meta.(*Owner).v4client orgName := meta.(*Owner).name rootTeamsOnly := d.Get("root_teams_only").(bool) + summaryOnly := d.Get("summary_only").(bool) + resultsPerPage := d.Get("results_per_page").(int) var query TeamsQuery + variables := map[string]interface{}{ - "first": githubv4.Int(100), + "first": githubv4.Int(resultsPerPage), "login": githubv4.String(orgName), "cursor": (*githubv4.String)(nil), "rootTeamsOnly": githubv4.Boolean(rootTeamsOnly), + "summaryOnly": githubv4.Boolean(summaryOnly), } var teams []interface{} diff --git a/github/data_source_github_organization_teams_test.go b/github/data_source_github_organization_teams_test.go index f6398058f2..9c1d5d462e 100644 --- a/github/data_source_github_organization_teams_test.go +++ b/github/data_source_github_organization_teams_test.go @@ -86,4 +86,86 @@ func TestAccGithubOrganizationTeamsDataSource(t *testing.T) { }) + t.Run("queries summary only without error", func(t *testing.T) { + + config := ` + data "github_organization_teams" "all" { + summary_only = true + } + ` + + check := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.members"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.repositories"), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + t.Skip("individual account not supported for this operation") + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) + + t.Run("queries results_per_page only without error", func(t *testing.T) { + + config := ` + data "github_organization_teams" "all" { + results_per_page = 50 + } + ` + + check := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + t.Skip("individual account not supported for this operation") + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) + } diff --git a/github/util_v4_teams.go b/github/util_v4_teams.go index 87c20c86c2..186eb4c086 100644 --- a/github/util_v4_teams.go +++ b/github/util_v4_teams.go @@ -17,12 +17,12 @@ type TeamsQuery struct { Nodes []struct { Login githubv4.String } - } + } `graphql:"members @skip(if: $summaryOnly)"` Repositories struct { Nodes []struct { Name githubv4.String } - } + } `graphql:"repositories @skip(if: $summaryOnly)"` } PageInfo PageInfo } `graphql:"teams(first:$first, after:$cursor, rootTeamsOnly:$rootTeamsOnly)"` diff --git a/website/docs/d/organization_teams.html.markdown b/website/docs/d/organization_teams.html.markdown index ad51be75c2..4d4ef63e7b 100644 --- a/website/docs/d/organization_teams.html.markdown +++ b/website/docs/d/organization_teams.html.markdown @@ -27,8 +27,11 @@ data "github_organization_teams" "root_teams" { ## Attributes Reference -* `root_teams_only` - Only return teams that are at the organization's root, i.e. no nested teams. Defaults to `false`. -* `teams` - An Array of GitHub Teams. Each `team` block consists of the fields documented below. +* `teams` - (Required) An Array of GitHub Teams. Each `team` block consists of the fields documented below. +* `root_teams_only` - (Optional) Only return teams that are at the organization's root, i.e. no nested teams. Defaults to `false`. +* `summary_only` - (Optional) Exclude the members and repositories of the team from the returned result. Defaults to `false`. +* `results_per_page` - (Optional) Set the number of results per graphql query. Reducing this number can alleviate timeout errors. Accepts a value between 0 - 100. Defaults to `100`. + ___ The `team` block consists of: @@ -39,5 +42,5 @@ The `team` block consists of: * `name` - the team's full name. * `description` - the team's description. * `privacy` - the team's privacy type. - * `members` - List of team members. - * `repositories` - List of team repositories. + * `members` - List of team members. Not returned if `summary_only = true` + * `repositories` - List of team repositories. Not returned if `summary_only = true`