forked from hashicorp/terraform
/
data_source_github_team.go
85 lines (72 loc) · 1.79 KB
/
data_source_github_team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)
}