From e7bfdea27437f1a9ad595e96fd40a15ff8668134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Mon, 22 May 2017 15:29:18 +0200 Subject: [PATCH] Add github_team data source (#14614) * Add github_team data source * github_team: add doc * github_team data source: fix acceptance test --- .../github/data_source_github_team.go | 85 +++++++++++++++++++ .../github/data_source_github_team_test.go | 33 +++++++ builtin/providers/github/provider.go | 1 + .../providers/github/d/team.html.markdown | 30 +++++++ website/source/layouts/github.erb | 4 +- 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/github/data_source_github_team.go create mode 100644 builtin/providers/github/data_source_github_team_test.go create mode 100644 website/source/docs/providers/github/d/team.html.markdown diff --git a/builtin/providers/github/data_source_github_team.go b/builtin/providers/github/data_source_github_team.go new file mode 100644 index 000000000..8ef25c239 --- /dev/null +++ b/builtin/providers/github/data_source_github_team.go @@ -0,0 +1,85 @@ +package github + +import ( + "context" + "fmt" + "log" + "strconv" + + "github.com/google/go-github/github" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGithubTeam() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubTeamRead, + + Schema: map[string]*schema.Schema{ + "slug": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "privacy": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "permission": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error { + slug := d.Get("slug").(string) + log.Printf("[INFO] Refreshing Gitub Team: %s", slug) + + client := meta.(*Organization).client + + team, err := getGithubTeamBySlug(client, meta.(*Organization).name, slug) + if err != nil { + return err + } + + d.SetId(strconv.Itoa(*team.ID)) + d.Set("name", *team.Name) + d.Set("description", *team.Description) + d.Set("privacy", *team.Privacy) + d.Set("permission", *team.Permission) + d.Set("members_count", *team.MembersCount) + d.Set("repos_count", *team.ReposCount) + + return nil +} + +func getGithubTeamBySlug(client *github.Client, org string, slug string) (team *github.Team, err error) { + opt := &github.ListOptions{PerPage: 10} + for { + teams, resp, err := client.Organizations.ListTeams(context.TODO(), org, opt) + if err != nil { + return team, err + } + + for _, t := range teams { + if *t.Slug == slug { + return t, nil + } + } + + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + + return team, fmt.Errorf("Could not find team with slug: %s", slug) +} diff --git a/builtin/providers/github/data_source_github_team_test.go b/builtin/providers/github/data_source_github_team_test.go new file mode 100644 index 000000000..7d27a4a95 --- /dev/null +++ b/builtin/providers/github/data_source_github_team_test.go @@ -0,0 +1,33 @@ +package github + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccGithubTeamDataSource_noMatchReturnsError(t *testing.T) { + slug := "non-existing" + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGithubTeamDataSourceConfig(slug), + ExpectError: regexp.MustCompile(`Could not find team`), + }, + }, + }) +} + +func testAccCheckGithubTeamDataSourceConfig(slug string) string { + return fmt.Sprintf(` +data "github_team" "test" { + slug = "%s" +} +`, slug) +} diff --git a/builtin/providers/github/provider.go b/builtin/providers/github/provider.go index 27a779d30..e1e829f36 100644 --- a/builtin/providers/github/provider.go +++ b/builtin/providers/github/provider.go @@ -46,6 +46,7 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "github_user": dataSourceGithubUser(), + "github_team": dataSourceGithubTeam(), }, ConfigureFunc: providerConfigure, diff --git a/website/source/docs/providers/github/d/team.html.markdown b/website/source/docs/providers/github/d/team.html.markdown new file mode 100644 index 000000000..a8026adf9 --- /dev/null +++ b/website/source/docs/providers/github/d/team.html.markdown @@ -0,0 +1,30 @@ +--- +layout: "github" +page_title: "Github: github_team" +sidebar_current: "docs-github-datasource-team" +description: |- + Get information on a Github team. +--- + +# github\_team + +Use this data source to retrieve information about a Github team. + +## Example Usage + +``` +data "github_team" "example" { + slug = "example" +} +``` + +## Argument Reference + + * `slug` - (Required) The team slug. + +## Attributes Reference + + * `name` - the team's full name. + * `description` - the team's description. + * `privacy` - the team's privacy type. + * `permission` - the team's permission level. diff --git a/website/source/layouts/github.erb b/website/source/layouts/github.erb index 6a3e65ff0..694aa97ba 100644 --- a/website/source/layouts/github.erb +++ b/website/source/layouts/github.erb @@ -13,10 +13,12 @@ > Data Sources