Skip to content

Commit b5ae884

Browse files
committed
feat: Add github_team_external_groups data source
Fixes #3295
1 parent 94f71cf commit b5ae884

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubTeamExternalGroups() *schema.Resource {
12+
return &schema.Resource{
13+
Description: "Retrieve external groups for a specific GitHub team.",
14+
ReadContext: dataSourceGithubTeamExternalGroupsRead,
15+
Schema: map[string]*schema.Schema{
16+
"slug": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
Description: "The slug of the GitHub team.",
20+
},
21+
"external_groups": {
22+
Type: schema.TypeList,
23+
Computed: true,
24+
Elem: &schema.Resource{
25+
Schema: map[string]*schema.Schema{
26+
"group_id": {
27+
Type: schema.TypeInt,
28+
Computed: true,
29+
},
30+
"group_name": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
"updated_at": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
},
39+
},
40+
},
41+
},
42+
}
43+
}
44+
45+
func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
46+
err := checkOrganization(meta)
47+
if err != nil {
48+
return diag.FromErr(err)
49+
}
50+
client := meta.(*Owner).v3client
51+
orgName := meta.(*Owner).name
52+
slug := d.Get("slug").(string)
53+
54+
externalGroups, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug)
55+
if err != nil {
56+
return diag.FromErr(err)
57+
}
58+
59+
// convert to JSON in order to marshal to format we can return
60+
jsonGroups, err := json.Marshal(externalGroups.Groups)
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
groupsState := make([]map[string]any, 0)
66+
err = json.Unmarshal(jsonGroups, &groupsState)
67+
if err != nil {
68+
return diag.FromErr(err)
69+
}
70+
71+
if err := d.Set("external_groups", groupsState); err != nil {
72+
return diag.FromErr(err)
73+
}
74+
75+
id, err := buildID(orgName, slug)
76+
if err != nil {
77+
return diag.FromErr(err)
78+
}
79+
d.SetId(id)
80+
81+
return nil
82+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
11+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
12+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
13+
)
14+
15+
func TestAccGithubTeamExternalGroupsDataSource(t *testing.T) {
16+
17+
t.Run("errors when querying a non-existing team", func(t *testing.T) {
18+
config := `
19+
data "github_team_external_groups" "test" {
20+
slug = "non-existing-team-slug"
21+
}
22+
`
23+
24+
resource.Test(t, resource.TestCase{
25+
PreCheck: func() { skipUnlessHasOrgs(t) },
26+
ProviderFactories: providerFactories,
27+
Steps: []resource.TestStep{
28+
{
29+
Config: config,
30+
ExpectError: regexp.MustCompile(`Not Found`),
31+
},
32+
},
33+
})
34+
})
35+
36+
t.Run("returns empty list for team without external groups", func(t *testing.T) {
37+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
38+
teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID)
39+
config := fmt.Sprintf(`
40+
resource "github_team" "test" {
41+
name = "%s"
42+
}
43+
44+
data "github_team_external_groups" "test" {
45+
slug = github_team.test.slug
46+
}
47+
`, teamName)
48+
49+
resource.Test(t, resource.TestCase{
50+
PreCheck: func() { skipUnlessHasOrgs(t) },
51+
ProviderFactories: providerFactories,
52+
Steps: []resource.TestStep{
53+
{
54+
Config: config,
55+
ConfigStateChecks: []statecheck.StateCheck{
56+
statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListSizeExact(0)),
57+
},
58+
},
59+
},
60+
})
61+
})
62+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ func Provider() *schema.Provider {
289289
"github_rest_api": dataSourceGithubRestApi(),
290290
"github_ssh_keys": dataSourceGithubSshKeys(),
291291
"github_team": dataSourceGithubTeam(),
292+
"github_team_external_groups": dataSourceGithubTeamExternalGroups(),
292293
"github_tree": dataSourceGithubTree(),
293294
"github_user": dataSourceGithubUser(),
294295
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_team_external_groups"
4+
description: |-
5+
Retrieve external groups for a specific GitHub team.
6+
---
7+
8+
# github\_team\_external\_groups
9+
10+
Use this data source to retrieve external groups for a specific GitHub team.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_team_external_groups" "example" {
16+
slug = "example"
17+
}
18+
19+
output "groups" {
20+
value = data.github_team_external_groups.example.external_groups
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
* `slug` - (Required) The slug of the GitHub team.
27+
28+
## Attributes Reference
29+
30+
* `external_groups` - An array of external groups for the team. Each group consists of the fields documented below.
31+
32+
___
33+
34+
* `group_id` - The ID of the external group.
35+
* `group_name` - The name of the external group.
36+
* `updated_at` - The date the group was last updated.

0 commit comments

Comments
 (0)