Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions github/data_source_github_team_external_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package github

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubTeamExternalGroups() *schema.Resource {
return &schema.Resource{
Description: "Retrieve external groups for a specific GitHub team.",
ReadContext: dataSourceGithubTeamExternalGroupsRead,
Schema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the GitHub team.",
},
"external_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
slug := d.Get("slug").(string)

externalGroups, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug)
if err != nil {
return diag.FromErr(err)
}

// convert to JSON in order to marshal to format we can return
jsonGroups, err := json.Marshal(externalGroups.Groups)
if err != nil {
return diag.FromErr(err)
}

groupsState := make([]map[string]any, 0)
err = json.Unmarshal(jsonGroups, &groupsState)
if err != nil {
return diag.FromErr(err)
}

if err := d.Set("external_groups", groupsState); err != nil {
return diag.FromErr(err)
}

id, err := buildID(orgName, slug)
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

return nil
}
62 changes: 62 additions & 0 deletions github/data_source_github_team_external_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package github

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func TestAccGithubTeamExternalGroupsDataSource(t *testing.T) {

t.Run("errors when querying a non-existing team", func(t *testing.T) {
config := `
data "github_team_external_groups" "test" {
slug = "non-existing-team-slug"
}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(`Not Found`),
},
},
})
})

t.Run("returns empty list for team without external groups", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
}

data "github_team_external_groups" "test" {
slug = github_team.test.slug
}
`, teamName)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListSizeExact(0)),
},
},
},
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func Provider() *schema.Provider {
"github_rest_api": dataSourceGithubRestApi(),
"github_ssh_keys": dataSourceGithubSshKeys(),
"github_team": dataSourceGithubTeam(),
"github_team_external_groups": dataSourceGithubTeamExternalGroups(),
"github_tree": dataSourceGithubTree(),
"github_user": dataSourceGithubUser(),
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
Expand Down
10 changes: 3 additions & 7 deletions website/docs/d/external_groups.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ N/A. This resource will retrieve all the external groups belonging to an organiz
## Attributes Reference

* `external_groups` - an array of external groups belonging to the organization. Each group consists of the fields documented below.

___


* `group_id` - the ID of the group.
* `group_name` - the name of the group.
* `updated_at` - the date the group was last updated.
* `group_id` - the ID of the group.
* `group_name` - the name of the group.
* `updated_at` - the date the group was last updated.

33 changes: 33 additions & 0 deletions website/docs/d/team_external_groups.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "github"
page_title: "GitHub: github_team_external_groups"
description: |-
Retrieve external groups for a specific GitHub team.
---

# github\_team\_external\_groups

Use this data source to retrieve external groups for a specific GitHub team.

## Example Usage

```hcl
data "github_team_external_groups" "example" {
slug = "example"
}

output "groups" {
value = data.github_team_external_groups.example.external_groups
}
```

## Argument Reference

* `slug` - (Required) The slug of the GitHub team.

## Attributes Reference

* `external_groups` - An array of external groups for the team. Each group consists of the fields documented below.
* `group_id` - The ID of the external group.
* `group_name` - The name of the external group.
* `updated_at` - The date the group was last updated.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@
<li>
<a href="/docs/providers/github/d/team.html">github_team</a>
</li>
<li>
<a href="/docs/providers/github/d/team_external_groups.html">github_team_external_groups</a>
</li>
<li>
<a href="/docs/providers/github/d/user.html">github_user</a>
</li>
Expand Down