Skip to content

Commit 6b9e025

Browse files
authored
Create data source for external groups (#1185)
* Initial commit of EMU data source * Set groups in state * Some renames * Correct google/go-github client version to v45 in new data source * Rename data source file to something more appropriate * Set a sensible ID * Add documentation for new data source * Fix website formatting
1 parent 86e4b44 commit 6b9e025

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

github/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func (c *Config) NewRESTClient(client *http.Client) (*github.Client, error) {
9999
}
100100

101101
func (c *Config) ConfigureOwner(owner *Owner) (*Owner, error) {
102-
103102
ctx := context.Background()
104103
owner.name = c.Owner
105104
if owner.name == "" {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/google/go-github/v45/github"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
)
11+
12+
func dataSourceGithubExternalGroups() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGithubExternalGroupsRead,
15+
Schema: map[string]*schema.Schema{
16+
"external_groups": {
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"group_id": {
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
},
25+
"group_name": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"updated_at": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
},
34+
},
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceGithubExternalGroupsRead(d *schema.ResourceData, meta interface{}) error {
41+
err := checkOrganization(meta)
42+
if err != nil {
43+
return err
44+
}
45+
client := meta.(*Owner).v3client
46+
orgName := meta.(*Owner).name
47+
48+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
49+
opts := &github.ListExternalGroupsOptions{}
50+
51+
externalGroups, _, err := client.Teams.ListExternalGroups(ctx, orgName, opts)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// convert to JSON in order to martial to format we can return
57+
jsonGroups, err := json.Marshal(externalGroups.Groups)
58+
if err != nil {
59+
return err
60+
}
61+
62+
groupsState := make([]map[string]interface{}, 0)
63+
err = json.Unmarshal(jsonGroups, &groupsState)
64+
if err != nil {
65+
return err
66+
}
67+
68+
if err := d.Set("external_groups", groupsState); err != nil {
69+
return err
70+
}
71+
72+
d.SetId(fmt.Sprintf("/orgs/%v/external-groups", orgName))
73+
return nil
74+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
137137
"github_branch": dataSourceGithubBranch(),
138138
"github_collaborators": dataSourceGithubCollaborators(),
139139
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
140+
"github_external_groups": dataSourceGithubExternalGroups(),
140141
"github_ip_ranges": dataSourceGithubIpRanges(),
141142
"github_membership": dataSourceGithubMembership(),
142143
"github_organization": dataSourceGithubOrganization(),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_external_group"
4+
description: |-
5+
Retrieve external groups belonging to an organization.
6+
---
7+
8+
# github\_external\_group
9+
10+
Use this data source to retrieve external groups belonging to an organization.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_external_groups" "example_external_groups" {}
16+
17+
locals {
18+
local_groups = "${data.github_external_groups.example_external_groups}"
19+
}
20+
21+
output "groups" {
22+
value = local.local_groups
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
N/A. This resource will retrieve all the external groups belonging to an organization.
29+
30+
## Attributes Reference
31+
32+
* `external_groups` - an array of external groups belonging to the organization. Each group consists of the fields documented below.
33+
34+
___
35+
36+
37+
* `group_id` - the ID of the group.
38+
* `group_name` - the name of the group.
39+
* `updated_at` - the date the group was last updated.
40+

website/github.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<li>
2323
<a href="/docs/providers/github/d/collaborators.html">github_collaborators</a>
2424
</li>
25+
<li>
26+
<a href="/docs/providers/github/d/external_groups.html">github_external_groups</a>
27+
</li>
2528
<li>
2629
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
2730
</li>

0 commit comments

Comments
 (0)