Repository navigation
feat: Add github_team_external_groups data source
#3296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
orirawlings
wants to merge
1
commit into
integrations:main
Choose a base branch
from
orirawlings:team_external_groups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| --- | ||
| page_title: "github_team_external_groups (Data Source) - GitHub" | ||
| subcategory: "" | ||
| description: |- | ||
| Data source to retrieve external groups for a specific GitHub team. | ||
| --- | ||
|
|
||
| # github_team_external_groups (Data Source) | ||
|
|
||
| Data source to retrieve external groups for a specific GitHub team. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "github_team_external_groups" "example" { | ||
| slug = "example" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `slug` (String) The slug of the GitHub team. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `external_groups` (List of Object) List of external groups connected to the team. (see [below for nested schema](#nestedatt--external_groups)) | ||
| - `id` (String) The ID of this resource. | ||
|
|
||
| <a id="nestedatt--external_groups"></a> | ||
| ### Nested Schema for `external_groups` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `group_id` (Number) | ||
| - `group_name` (String) | ||
| - `updated_at` (String) | ||
| --> | ||
|
|
||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `slug` (String) The slug of the GitHub team. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `external_groups` (List of Object) List of external groups connected to the team. (see [below for nested schema](#nestedatt--external_groups)) | ||
| - `id` (String) The ID of this resource. | ||
|
|
||
| <a id="nestedatt--external_groups"></a> | ||
| ### Nested Schema for `external_groups` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `group_id` (Number) The internal ID of the group. | ||
| - `group_name` (String) The display name of the group. | ||
| - `updated_at` (String) The time of the last update for this group. |
3 changes: 3 additions & 0 deletions
3
examples/data-sources/github_team_external_groups/data-source-1.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| data "github_team_external_groups" "example" { | ||
| slug = "example" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/google/go-github/v92/github" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
| ) | ||
|
|
||
| func dataSourceGithubTeamExternalGroups() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Description: "Data source to retrieve external groups for a specific GitHub team.", | ||
| ReadContext: dataSourceGithubTeamExternalGroupsRead, | ||
| Schema: map[string]*schema.Schema{ | ||
|
orirawlings marked this conversation as resolved.
|
||
| "slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the GitHub team.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty), | ||
| }, | ||
| "external_groups": { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add descriptions and a documentation template, see the team members implementation for how to do this. |
||
| Description: "List of external groups connected to the team.", | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "group_id": { | ||
| Description: "The internal ID of the group.", | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| "group_name": { | ||
| Description: "The display name of the group.", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "updated_at": { | ||
| Description: "The time of the last update for this group.", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| meta, _ := m.(*Owner) | ||
|
|
||
| if ok, diags := checkOrganizationOK(meta); !ok { | ||
| return diags | ||
| } | ||
|
|
||
| orgName := meta.name | ||
| slug, ok := d.Get("slug").(string) | ||
| if !ok { | ||
| return diag.Errorf("expected type of %s to be string", d.Get("slug")) | ||
| } | ||
|
|
||
| externalGroups, _, err := meta.v3client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug) | ||
| if err != nil { | ||
| // Teams with explicit members cannot be externally managed, so they have no external groups. | ||
| var ghErr *github.ErrorResponse | ||
| if errors.As(err, &ghErr) && ghErr.Response != nil && | ||
| ghErr.Response.StatusCode == http.StatusBadRequest && | ||
| strings.Contains(ghErr.Message, "has explicit members") { | ||
| externalGroups = &github.ExternalGroupList{} | ||
| } else { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| groups := make([]map[string]any, 0) | ||
| for _, group := range externalGroups.Groups { | ||
| g := map[string]any{ | ||
| "group_id": group.GetGroupID(), | ||
| "group_name": group.GetGroupName(), | ||
| "updated_at": group.GetUpdatedAt().Format(time.RFC3339), | ||
| } | ||
|
|
||
| groups = append(groups, g) | ||
| } | ||
|
|
||
| id, err := buildID(orgName, slug) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| d.SetId(id) | ||
|
|
||
| if err := d.Set("external_groups", groups); err != nil { | ||
| return diag.Errorf("error setting external_groups: %v", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "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 TestAccGithubTeamExternalGroupsDataSource(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the happy path test? |
||
| t.Parallel() | ||
|
|
||
| skipUnlessEMUEnterprise(t) | ||
|
|
||
| t.Run("queries_empty_slug", func(t *testing.T) { | ||
| config := ` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "" | ||
| } | ||
| ` | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ExpectError: regexp.MustCompile(`expected "slug" to not be an empty string`), | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("queries_non_existent_team", func(t *testing.T) { | ||
| config := ` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "non-existing-team-slug" | ||
| } | ||
| ` | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ExpectError: regexp.MustCompile(`Not Found`), | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("queries_team_without_external_groups", func(t *testing.T) { | ||
| team := mustCreateTestTeam(t) | ||
| config := fmt.Sprintf(` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "%s" | ||
| } | ||
| `, team.GetSlug()) | ||
|
|
||
| mustRemoveAllMembersFromTeam(t, team) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListSizeExact(0)), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("queries_team_with_regular_membership", func(t *testing.T) { | ||
| team := mustCreateTestTeam(t) | ||
| config := fmt.Sprintf(` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "%s" | ||
| } | ||
| `, team.GetSlug()) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListSizeExact(0)), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("queries_team_with_external_groups", func(t *testing.T) { | ||
| if testAccConf.testExternalGroup1ID == 0 { | ||
| t.Skip("Skipping as no external groups are configured for the test organization") | ||
| } | ||
| team := mustCreateTestTeam(t) | ||
| groupID := int64(testAccConf.testExternalGroup1ID) | ||
| mustConnectTeamToExternalGroup(t, team, groupID) | ||
| config := fmt.Sprintf(` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "%s" | ||
| } | ||
| `, team.GetSlug()) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue("data.github_team_external_groups.test", tfjsonpath.New("external_groups"), knownvalue.ListExact([]knownvalue.Check{ | ||
| knownvalue.MapPartial(map[string]knownvalue.Check{ | ||
| "group_id": knownvalue.Int64Exact(groupID), | ||
| }), | ||
| })), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.