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
20 changes: 19 additions & 1 deletion docs/data-sources/external_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Use this data source to retrieve external groups belonging to an organization.

## Example Usage

### All external groups

```terraform
data "github_external_groups" "example_external_groups" {}

Expand All @@ -22,9 +24,25 @@ output "groups" {
}
```

### Filtered by display name

```terraform
data "github_external_groups" "example_external_groups_filtered" {
display_name = "my-group"
}

locals {
filtered_groups = data.github_external_groups.example_external_groups_filtered
}

output "groups" {
value = local.filtered_groups
}
```

## Argument Reference

N/A. This resource will retrieve all the external groups belonging to an organization.
- `display_name` - (Optional) Filter the list of external groups by display name. Only groups whose name contains this value will be returned.

## Attributes Reference

Expand Down
11 changes: 11 additions & 0 deletions examples/data-sources/external_groups/example_2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "github_external_groups" "example_external_groups_filtered" {
display_name = "my-group"
}

locals {
filtered_groups = data.github_external_groups.example_external_groups_filtered
}

output "groups" {
value = local.filtered_groups
}
15 changes: 14 additions & 1 deletion github/data_source_github_external_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func dataSourceGithubExternalGroups() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubExternalGroupsRead,
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Optional: true,
},
"external_groups": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -48,6 +52,11 @@ func dataSourceGithubExternalGroupsRead(ctx context.Context, d *schema.ResourceD

opts := &github.ListExternalGroupsOptions{}

if v, ok := d.GetOk("display_name"); ok {
displayName := v.(string)
opts.DisplayName = &displayName
}

externalGroups := new(github.ExternalGroupList)

for {
Expand Down Expand Up @@ -80,6 +89,10 @@ func dataSourceGithubExternalGroupsRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("/orgs/%v/external-groups", orgName))
resourceID := fmt.Sprintf("/orgs/%v/external-groups", orgName)
if opts.DisplayName != nil {
resourceID = fmt.Sprintf("/orgs/%v/external-groups/%v", orgName, *opts.DisplayName)
}
d.SetId(resourceID)
return nil
}
49 changes: 49 additions & 0 deletions github/data_source_github_external_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package github

import (
"testing"

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

func TestAccGithubExternalGroupsDataSource(t *testing.T) {
t.Run("queries_all", func(t *testing.T) {
config := `
data "github_external_groups" "all" {}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessEMUEnterprise(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_external_groups.all", "external_groups.#"),
),
},
},
})
})

t.Run("queries_with_display_name_filter", func(t *testing.T) {
config := `
data "github_external_groups" "filtered" {
display_name = "test"
}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessEMUEnterprise(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_external_groups.filtered", "external_groups.#"),
),
},
},
})
})
}
8 changes: 7 additions & 1 deletion templates/data-sources/external_groups.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ Use this data source to retrieve external groups belonging to an organization.

## Example Usage

### All external groups

{{ tffile "examples/data-sources/external_groups/example_1.tf" }}

### Filtered by display name

{{ tffile "examples/data-sources/external_groups/example_2.tf" }}

## Argument Reference

N/A. This resource will retrieve all the external groups belonging to an organization.
- `display_name` - (Optional) Filter the list of external groups by display name. Only groups whose name contains this value will be returned.

## Attributes Reference

Expand Down