forked from integrations/terraform-provider-github
/
data_source_github_organization_teams.go
149 lines (123 loc) · 3.25 KB
/
data_source_github_organization_teams.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package github
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/shurcooL/githubv4"
"log"
)
func dataSourceGithubOrganizationTeams() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubOrganizationTeamsRead,
Schema: map[string]*schema.Schema{
"root_teams_only": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"teams": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"node_id": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"privacy": {
Type: schema.TypeString,
Computed: true,
},
"members": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"repositories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}
}
func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
rootTeamsOnly := d.Get("root_teams_only").(bool)
log.Print("[INFO] Refreshing GitHub Teams for Organization: ", orgName)
var query TeamsQuery
variables := map[string]interface{}{
"first": githubv4.Int(100),
"login": githubv4.String(orgName),
"cursor": (*githubv4.String)(nil),
"rootTeamsOnly": githubv4.Boolean(rootTeamsOnly),
}
var teams []interface{}
for {
err = client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return err
}
additionalTeams := flattenGitHubTeams(query)
teams = append(teams, additionalTeams...)
if !query.Organization.Teams.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Organization.Teams.PageInfo.EndCursor)
}
d.SetId(string(query.Organization.ID))
d.Set("teams", teams)
return nil
}
func flattenGitHubTeams(tq TeamsQuery) []interface{} {
teams := tq.Organization.Teams.Nodes
if len(teams) == 0 {
return make([]interface{}, 0)
}
flatTeams := make([]interface{}, len(teams))
for i, team := range teams {
t := make(map[string]interface{})
t["id"] = team.DatabaseID
t["node_id"] = team.ID
t["slug"] = team.Slug
t["name"] = team.Name
t["description"] = team.Description
t["privacy"] = team.Privacy
members := team.Members.Nodes
flatMembers := make([]string, len(members))
for i, member := range members {
flatMembers[i] = string(member.Login)
}
t["members"] = flatMembers
repositories := team.Repositories.Nodes
flatRepositories := make([]string, len(repositories))
for i, repository := range repositories {
flatRepositories[i] = string(repository.Name)
}
t["repositories"] = flatRepositories
flatTeams[i] = t
}
return flatTeams
}