Skip to content
25 changes: 23 additions & 2 deletions github/data_source_github_organization_team_sync_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package github

import (
"context"
"fmt"

"github.com/google/go-github/v83/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -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,
},
},
Comment on lines +15 to +22
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤦 Silly me. Do data sources even need Schema Migrations? @stevehipwell

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@stevehipwell any input on this?


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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 15 additions & 5 deletions github/data_source_github_organization_team_sync_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)),
},
},
},
})
Expand Down
12 changes: 11 additions & 1 deletion website/docs/d/organization_team_sync_groups.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down