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
77 changes: 77 additions & 0 deletions github/data_source_github_team_external_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package github

import (
"context"
"encoding/json"
"fmt"

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

func dataSourceGithubTeamExternalGroups() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubTeamExternalGroupsRead,
Schema: map[string]*schema.Schema{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add top-level Description

"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)
}

d.SetId(fmt.Sprintf("/orgs/%v/teams/%v/external-groups", orgName, slug))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use buildID(..)

return nil
}
89 changes: 89 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,89 @@
package github

import (
"fmt"
"regexp"
"testing"

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

func TestAccGithubTeamExternalGroupsDataSource(t *testing.T) {
t.Run("queries external groups for an existing team without error", 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)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_team_external_groups.test", "external_groups.#"),
)
Comment on lines +26 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use variables for these


resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ConfigStateChecks instead

},
},
})
})

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)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.github_team_external_groups.test", "external_groups.#", "0"),
)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,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
36 changes: 36 additions & 0 deletions website/docs/d/team_external_groups.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
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.