forked from integrations/terraform-provider-github
/
data_source_github_organization.go
125 lines (105 loc) · 2.58 KB
/
data_source_github_organization.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
package github
import (
"log"
"strconv"
"github.com/google/go-github/v39/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceGithubOrganization() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubOrganizationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"node_id": {
Type: schema.TypeString,
Computed: true,
},
"login": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"plan": {
Type: schema.TypeString,
Computed: true,
},
"repositories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"members": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
log.Printf("[INFO] Refreshing GitHub Organization: %s", name)
client := meta.(*Owner).v3client
ctx := meta.(*Owner).StopContext
organization, _, err := client.Organizations.Get(ctx, name)
if err != nil {
return err
}
plan := organization.GetPlan()
opts := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10, Page: 1},
}
var repoList []string
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, name, opts)
if err != nil {
return err
}
allRepos = append(allRepos, repos...)
opts.Page = resp.NextPage
if resp.NextPage == 0 {
break
}
}
for index := range allRepos {
repoList = append(repoList, allRepos[index].GetFullName())
}
membershipOpts := &github.ListMembersOptions{
ListOptions: github.ListOptions{PerPage: 10, Page: 1},
}
var memberList []string
var allMembers []*github.User
for {
members, resp, err := client.Organizations.ListMembers(ctx, name, membershipOpts)
if err != nil {
return err
}
allMembers = append(allMembers, members...)
membershipOpts.Page = resp.NextPage
if resp.NextPage == 0 {
break
}
}
for index := range allMembers {
memberList = append(memberList, *allMembers[index].Login)
}
d.SetId(strconv.FormatInt(organization.GetID(), 10))
d.Set("login", organization.GetLogin())
d.Set("name", organization.GetName())
d.Set("description", organization.GetDescription())
d.Set("plan", plan.Name)
d.Set("repositories", repoList)
d.Set("members", memberList)
return nil
}