diff --git a/docs/data-sources/organization_team_sync_groups.md b/docs/data-sources/organization_team_sync_groups.md index c3a23f7aff..69977a57a4 100644 --- a/docs/data-sources/organization_team_sync_groups.md +++ b/docs/data-sources/organization_team_sync_groups.md @@ -1,29 +1,45 @@ --- page_title: "github_organization_team_sync_groups (Data Source) - GitHub" +subcategory: "" description: |- Get the external identity provider (IdP) groups for an organization. --- # github_organization_team_sync_groups (Data Source) -Use this data source to retrieve the identity provider (IdP) groups for an organization. +Get the external identity provider (IdP) groups for an organization. ## Example Usage ```terraform -data "github_organization_team_sync_groups" "test" {} +# Retrieve all IdP groups for the organization. +data "github_organization_team_sync_groups" "all" {} ``` -## Attributes Reference +```terraform +# Retrieve IdP groups whose names begin with a given prefix. +data "github_organization_team_sync_groups" "filtered" { + prefix_filter = "myprefix_" +} +``` -- `groups` - An Array of GitHub Identity Provider Groups. Each `group` block consists of the fields documented below. + +## Schema ---- +### Optional + +- `prefix_filter` (String) Filters the results to return only those that begin with the specified value. + +### Read-Only -The `group` block consists of: +- `groups` (List of Object) (see [below for nested schema](#nestedatt--groups)) +- `id` (String) The ID of this resource. -- `group_id` - The ID of the IdP group. + +### Nested Schema for `groups` -- `group_name` - The name of the IdP group. +Read-Only: -- `group_description` - The description of the IdP group. +- `group_description` (String) +- `group_id` (String) +- `group_name` (String) diff --git a/examples/data-sources/github_organization_team_sync_groups/data-source_1.tf b/examples/data-sources/github_organization_team_sync_groups/data-source_1.tf new file mode 100644 index 0000000000..654bbeada1 --- /dev/null +++ b/examples/data-sources/github_organization_team_sync_groups/data-source_1.tf @@ -0,0 +1,2 @@ +# Retrieve all IdP groups for the organization. +data "github_organization_team_sync_groups" "all" {} diff --git a/examples/data-sources/github_organization_team_sync_groups/data-source_2.tf b/examples/data-sources/github_organization_team_sync_groups/data-source_2.tf new file mode 100644 index 0000000000..08763a1f65 --- /dev/null +++ b/examples/data-sources/github_organization_team_sync_groups/data-source_2.tf @@ -0,0 +1,4 @@ +# Retrieve IdP groups whose names begin with a given prefix. +data "github_organization_team_sync_groups" "filtered" { + prefix_filter = "myprefix_" +} diff --git a/examples/data-sources/organization_team_sync_groups/example_1.tf b/examples/data-sources/organization_team_sync_groups/example_1.tf deleted file mode 100644 index 19c77b153c..0000000000 --- a/examples/data-sources/organization_team_sync_groups/example_1.tf +++ /dev/null @@ -1 +0,0 @@ -data "github_organization_team_sync_groups" "test" {} diff --git a/github/data_source_github_organization_team_sync_groups.go b/github/data_source_github_organization_team_sync_groups.go index 643b73b457..e59aee6b4a 100644 --- a/github/data_source_github_organization_team_sync_groups.go +++ b/github/data_source_github_organization_team_sync_groups.go @@ -2,7 +2,6 @@ package github import ( "context" - "fmt" "github.com/google/go-github/v89/github" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -12,8 +11,14 @@ import ( func dataSourceGithubOrganizationTeamSyncGroups() *schema.Resource { return &schema.Resource{ ReadContext: dataSourceGithubOrganizationTeamSyncGroupsRead, + Description: "Get the external identity provider (IdP) groups for an organization.", Schema: map[string]*schema.Schema{ + "prefix_filter": { + Type: schema.TypeString, + Optional: true, + Description: "Filters the results to return only those that begin with the specified value.", + }, "groups": { Type: schema.TypeList, Computed: true, @@ -49,6 +54,11 @@ func dataSourceGithubOrganizationTeamSyncGroupsRead(ctx context.Context, d *sche }, } + if v, ok := d.GetOk("prefix_filter"); ok { + q, _ := v.(string) + options.Query = q + } + groups := make([]any, 0) for { idpGroupList, resp, err := client.Teams.ListIDPGroupsInOrganization(ctx, orgName, options) @@ -66,7 +76,15 @@ func dataSourceGithubOrganizationTeamSyncGroupsRead(ctx context.Context, d *sche options.Page = resp.NextPageToken } - d.SetId(fmt.Sprintf("%s/github-org-team-sync-groups", orgName)) + query := options.Query + if query == "" { + query = "*" + } + id, err := buildID(orgName, query) + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) if err := d.Set("groups", groups); err != nil { return diag.Errorf("error setting groups: %v", err) } diff --git a/github/data_source_github_organization_team_sync_groups_test.go b/github/data_source_github_organization_team_sync_groups_test.go index c178503395..8a12492110 100644 --- a/github/data_source_github_organization_team_sync_groups_test.go +++ b/github/data_source_github_organization_team_sync_groups_test.go @@ -4,12 +4,15 @@ import ( "testing" "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 TestAccGithubOrganizationTeamSyncGroupsDataSource_existing(t *testing.T) { +func TestAccGithubOrganizationTeamSyncGroupsDataSource(t *testing.T) { t.Parallel() - t.Run("success", func(t *testing.T) { + t.Run("all", func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ @@ -18,11 +21,43 @@ func TestAccGithubOrganizationTeamSyncGroupsDataSource_existing(t *testing.T) { Steps: []resource.TestStep{ { Config: `data "github_organization_team_sync_groups" "test" {}`, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_team_sync_groups.test", "groups.#"), - resource.TestCheckResourceAttrSet("data.github_organization_team_sync_groups.test", "groups.0.group_id"), - resource.TestCheckResourceAttrSet("data.github_organization_team_sync_groups.test", "groups.0.group_name"), - ), + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("groups"), knownvalue.NotNull()), + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("groups").AtSliceIndex(0).AtMapKey("group_id"), knownvalue.NotNull()), + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("groups").AtSliceIndex(0).AtMapKey("group_name"), knownvalue.NotNull()), + }, + }, + }, + }) + }) + + t.Run("filtered", func(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessEnterprise(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: `data "github_organization_team_sync_groups" "test" { prefix_filter = "acctest-github-provider" }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("prefix_filter"), knownvalue.StringExact("acctest-github-provider")), + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("groups"), knownvalue.NotNull()), + }, + }, + { + Config: `data "github_organization_team_sync_groups" "test" { prefix_filter = "nonexistent_prefix_" }`, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("prefix_filter"), knownvalue.StringExact("nonexistent_prefix_")), + statecheck.ExpectKnownValue("data.github_organization_team_sync_groups.test", + tfjsonpath.New("groups"), knownvalue.ListSizeExact(0)), + }, }, }, }) diff --git a/templates/data-sources/organization_team_sync_groups.md.tmpl b/templates/data-sources/organization_team_sync_groups.md.tmpl deleted file mode 100644 index 119f4639d3..0000000000 --- a/templates/data-sources/organization_team_sync_groups.md.tmpl +++ /dev/null @@ -1,27 +0,0 @@ ---- -page_title: "{{.Name}} ({{.Type}}) - {{.RenderedProviderName}}" -description: |- - Get the external identity provider (IdP) groups for an organization. ---- - -# {{.Name}} ({{.Type}}) - -Use this data source to retrieve the identity provider (IdP) groups for an organization. - -## Example Usage - -{{ tffile "examples/data-sources/organization_team_sync_groups/example_1.tf" }} - -## Attributes Reference - -- `groups` - An Array of GitHub Identity Provider Groups. Each `group` block consists of the fields documented below. - ---- - -The `group` block consists of: - -- `group_id` - The ID of the IdP group. - -- `group_name` - The name of the IdP group. - -- `group_description` - The description of the IdP group.