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
39 changes: 39 additions & 0 deletions docs/data-sources/team_external_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
page_title: "github_team_external_groups (Data Source) - GitHub"
subcategory: ""
description: |-
Retrieve external groups for a specific GitHub team.
---

# github_team_external_groups (Data Source)

Retrieve external groups for a specific GitHub team.

## Example Usage

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

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `slug` (String) The slug of the GitHub team.

### Read-Only

- `external_groups` (List of Object) (see [below for nested schema](#nestedatt--external_groups))
- `id` (String) The ID of this resource.

<a id="nestedatt--external_groups"></a>
### Nested Schema for `external_groups`

Read-Only:

- `group_id` (Number)
- `group_name` (String)
- `updated_at` (String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "github_team_external_groups" "example" {
slug = "example"
}
89 changes: 89 additions & 0 deletions github/data_source_github_team_external_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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,
Comment thread
orirawlings marked this conversation as resolved.
Schema: map[string]*schema.Schema{
Comment thread
orirawlings marked this conversation as resolved.
"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 {
owner, ok := meta.(*Owner)
if !ok {
return diag.Errorf("expected type of %s to be *Owner", meta)
}
err := checkOrganization(owner)
if err != nil {
return diag.FromErr(err)
}
client := owner.v3client
orgName := owner.name
slug, ok := d.Get("slug").(string)
if !ok {
return diag.Errorf("expected type of %s to be string", d.Get("slug"))
}

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
}
61 changes: 61 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,61 @@
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() { skipUnlessEMUEnterprise(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() { skipUnlessEMUEnterprise(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 @@ -336,6 +336,7 @@ func NewProvider(version, commit string) func() *schema.Provider {
"github_team": dataSourceGithubTeam(),
"github_team_members": dataSourceGithubTeamMembers(),
"github_team_repositories": dataSourceGithubTeamRepositories(),
"github_team_external_groups": dataSourceGithubTeamExternalGroups(),
"github_tree": dataSourceGithubTree(),
"github_user": dataSourceGithubUser(),
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
Expand Down
Loading