diff --git a/github/data_source_github_organization_team_sync_groups.go b/github/data_source_github_organization_team_sync_groups.go index 5ffc3d1f2b..48e843ee8c 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/v83/github" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -13,7 +12,21 @@ func dataSourceGithubOrganizationTeamSyncGroups() *schema.Resource { return &schema.Resource{ ReadContext: dataSourceGithubOrganizationTeamSyncGroupsRead, + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: dataSourceGithubOrganizationTeamSyncGroupsV0().CoreConfigSchema().ImpliedType(), + Upgrade: dataSourceGithubOrganizationTeamSyncGroupsStateUpgradeV0, + Version: 0, + }, + }, + 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, @@ -48,6 +61,10 @@ func dataSourceGithubOrganizationTeamSyncGroupsRead(ctx context.Context, d *sche }, } + if v, ok := d.GetOk("prefix_filter"); ok { + options.Query = v.(string) + } + groups := make([]any, 0) for { idpGroupList, resp, err := client.Teams.ListIDPGroupsInOrganization(ctx, orgName, options) @@ -65,7 +82,11 @@ func dataSourceGithubOrganizationTeamSyncGroupsRead(ctx context.Context, d *sche options.Page = resp.NextPageToken } - d.SetId(fmt.Sprintf("%s/github-org-team-sync-groups", orgName)) + id, err := buildID(orgName, "team-sync-groups", options.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_migration.go b/github/data_source_github_organization_team_sync_groups_migration.go new file mode 100644 index 0000000000..d5bc64c5f3 --- /dev/null +++ b/github/data_source_github_organization_team_sync_groups_migration.go @@ -0,0 +1,57 @@ +package github + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubOrganizationTeamSyncGroupsV0() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "group_id": { + Type: schema.TypeString, + Computed: true, + }, + "group_name": { + Type: schema.TypeString, + Computed: true, + }, + "group_description": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGithubOrganizationTeamSyncGroupsStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) { + tflog.Debug(ctx, "GitHub Organization Team Sync Groups State before migration", map[string]any{"state": rawState}) + + if rawState == nil { + return nil, fmt.Errorf("cannot migrate nil state") + } + + orgName := meta.(*Owner).name + + newID, err := buildID(orgName, "team-sync-groups", "") + if err != nil { + return nil, fmt.Errorf("error building migrated ID: %w", err) + } + + rawState["id"] = newID + + tflog.Debug(ctx, "GitHub Organization Team Sync Groups State after migration", map[string]any{"state": rawState}) + + return rawState, nil +} 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 a0779c84fb..e9dc518171 100644 --- a/github/data_source_github_organization_team_sync_groups_test.go +++ b/github/data_source_github_organization_team_sync_groups_test.go @@ -4,6 +4,9 @@ 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) { @@ -13,11 +16,18 @@ 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()), + }, + }, + { + 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/website/docs/d/organization_team_sync_groups.html.markdown b/website/docs/d/organization_team_sync_groups.html.markdown index 85b36fb436..4ab370d23b 100644 --- a/website/docs/d/organization_team_sync_groups.html.markdown +++ b/website/docs/d/organization_team_sync_groups.html.markdown @@ -12,9 +12,19 @@ Use this data source to retrieve the identity provider (IdP) groups for an organ ## Example Usage ```hcl -data "github_organization_team_sync_groups" "test" {} +data "github_organization_team_sync_groups" "all" {} ``` +```hcl +data "github_organization_team_sync_groups" "filtered" { + prefix_filter = "myprefix_" +} +``` + +## Argument Reference + +* `prefix_filter` - (Optional) Filters the results to return only those groups whose names begin with this value. + ## Attributes Reference * `groups` - An Array of GitHub Identity Provider Groups. Each `group` block consists of the fields documented below.