Add github_team data source (#14614)

* Add github_team data source

* github_team: add doc

* github_team data source: fix acceptance test
This commit is contained in:
Raphaël Pinson 2017-05-22 15:29:18 +02:00 committed by Paul Stack
parent 94e6128fcf
commit e7bfdea274
5 changed files with 152 additions and 1 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -46,6 +46,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"github_user": dataSourceGithubUser(),
"github_team": dataSourceGithubTeam(),
},
ConfigureFunc: providerConfigure,

View File

@ -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.

View File

@ -13,10 +13,12 @@
<li<%= sidebar_current(/^docs-github-datasource/) %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-github-datasource-user") %>>
<a href="/docs/providers/github/d/user.html">github_user</a>
</li>
<li<%= sidebar_current("docs-github-datasource-team") %>>
<a href="/docs/providers/github/d/team.html">github_team</a>
</li>
</ul>
</li>