-
Notifications
You must be signed in to change notification settings - Fork 945
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceGithubTeamExternalGroups() *schema.Resource { | ||
| return &schema.Resource{ | ||
| ReadContext: dataSourceGithubTeamExternalGroupsRead, | ||
| Schema: map[string]*schema.Schema{ | ||
| "slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the GitHub team.", | ||
| }, | ||
| "external_groups": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "group_id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| "group_name": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "updated_at": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubTeamExternalGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| err := checkOrganization(meta) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| client := meta.(*Owner).v3client | ||
| orgName := meta.(*Owner).name | ||
| slug := d.Get("slug").(string) | ||
|
|
||
| externalGroups, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, slug) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| // convert to JSON in order to marshal to format we can return | ||
| jsonGroups, err := json.Marshal(externalGroups.Groups) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| groupsState := make([]map[string]any, 0) | ||
| err = json.Unmarshal(jsonGroups, &groupsState) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err := d.Set("external_groups", groupsState); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| d.SetId(fmt.Sprintf("/orgs/%v/teams/%v/external-groups", orgName, slug)) | ||
|
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 use |
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccGithubTeamExternalGroupsDataSource(t *testing.T) { | ||
| t.Run("queries external groups for an existing team without error", func(t *testing.T) { | ||
| randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
| teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID) | ||
| config := fmt.Sprintf(` | ||
| resource "github_team" "test" { | ||
| name = "%s" | ||
| } | ||
|
|
||
| data "github_team_external_groups" "test" { | ||
| slug = github_team.test.slug | ||
| } | ||
| `, teamName) | ||
|
|
||
| check := resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet("data.github_team_external_groups.test", "external_groups.#"), | ||
| ) | ||
|
Comment on lines
+26
to
+28
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. Don't use variables for these |
||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessHasOrgs(t) }, | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: check, | ||
|
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 use |
||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("errors when querying a non-existing team", func(t *testing.T) { | ||
| config := ` | ||
| data "github_team_external_groups" "test" { | ||
| slug = "non-existing-team-slug" | ||
| } | ||
| ` | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessHasOrgs(t) }, | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| ExpectError: regexp.MustCompile(`Not Found`), | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("returns empty list for team without external groups", func(t *testing.T) { | ||
| randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
| teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID) | ||
| config := fmt.Sprintf(` | ||
| resource "github_team" "test" { | ||
| name = "%s" | ||
| } | ||
|
|
||
| data "github_team_external_groups" "test" { | ||
| slug = github_team.test.slug | ||
| } | ||
| `, teamName) | ||
|
|
||
| check := resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttr("data.github_team_external_groups.test", "external_groups.#", "0"), | ||
| ) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessHasOrgs(t) }, | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: check, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| layout: "github" | ||
| page_title: "GitHub: github_team_external_groups" | ||
| description: |- | ||
| Retrieve external groups for a specific GitHub team. | ||
| --- | ||
|
|
||
| # github\_team\_external\_groups | ||
|
|
||
| Use this data source to retrieve external groups for a specific GitHub team. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```hcl | ||
| data "github_team_external_groups" "example" { | ||
| slug = "example" | ||
| } | ||
|
|
||
| output "groups" { | ||
| value = data.github_team_external_groups.example.external_groups | ||
| } | ||
| ``` | ||
|
|
||
| ## Argument Reference | ||
|
|
||
| * `slug` - (Required) The slug of the GitHub team. | ||
|
|
||
| ## Attributes Reference | ||
|
|
||
| * `external_groups` - An array of external groups for the team. Each group consists of the fields documented below. | ||
|
|
||
| ___ | ||
|
|
||
| * `group_id` - The ID of the external group. | ||
| * `group_name` - The name of the external group. | ||
| * `updated_at` - The date the group was last updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add top-level
Description