-
Notifications
You must be signed in to change notification settings - Fork 933
[FEAT] Add support for Enterprise Teams #3008
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
vmvarela
wants to merge
11
commits into
integrations:main
Choose a base branch
from
vmvarela:enterprise-teams
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
02a9eae
feat(enterprise-teams): add utility functions for enterprise teams
github-actions[bot] 4a0a31c
feat(enterprise-teams): add github_enterprise_team resource
github-actions[bot] 7794464
feat(enterprise-teams): add github_enterprise_team_membership resource
github-actions[bot] aacb822
feat(enterprise-teams): add github_enterprise_team_organizations reso…
github-actions[bot] 050e82e
feat(enterprise-teams): add data sources for enterprise teams
github-actions[bot] e51d105
feat(enterprise-teams): register enterprise team resources and data s…
github-actions[bot] 0c7c7f1
fix(enterprise-teams): update go-github import to v82
github-actions[bot] b1d825e
test(enterprise-teams): migrate tests to terraform-plugin-testing and…
vmvarela 0df0be3
fix(enterprise-teams): address review findings
vmvarela 5c4fbad
fix(enterprise-teams): update go-github import from v82 to v83
vmvarela 6a6c9e5
fix(enterprise-teams): authoritative Create/Delete for team organizat…
vmvarela 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v83/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 dataSourceGithubEnterpriseTeam() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Description: "Gets information about a GitHub enterprise team.", | ||
| ReadContext: dataSourceGithubEnterpriseTeamRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "enterprise_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "slug": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Computed: true, | ||
| ExactlyOneOf: []string{"slug", "team_id"}, | ||
| Description: "The slug of the enterprise team.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "team_id": { | ||
| Type: schema.TypeInt, | ||
| Optional: true, | ||
| Computed: true, | ||
| ExactlyOneOf: []string{"slug", "team_id"}, | ||
| Description: "The numeric ID of the enterprise team.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)), | ||
| }, | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "The name of the enterprise team.", | ||
| }, | ||
| "description": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "A description of the enterprise team.", | ||
| }, | ||
| "organization_selection_type": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "Specifies which organizations in the enterprise should have access to this team.", | ||
| }, | ||
| "group_id": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "The ID of the IdP group to assign team membership with.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubEnterpriseTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
| enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string)) | ||
|
|
||
| var te *github.EnterpriseTeam | ||
| if v, ok := d.GetOk("team_id"); ok { | ||
| teamID := int64(v.(int)) | ||
| if teamID != 0 { | ||
| found, err := findEnterpriseTeamByID(ctx, client, enterpriseSlug, teamID) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if found == nil { | ||
| return diag.Errorf("could not find enterprise team %d in enterprise %s", teamID, enterpriseSlug) | ||
| } | ||
| te = found | ||
| } | ||
| } | ||
|
|
||
| if te == nil { | ||
| teamSlug := strings.TrimSpace(d.Get("slug").(string)) | ||
| if teamSlug == "" { | ||
| return diag.Errorf("one of slug or team_id must be set") | ||
| } | ||
| found, _, err := client.Enterprise.GetTeam(ctx, enterpriseSlug, teamSlug) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| te = found | ||
| } | ||
|
|
||
| d.SetId(buildTwoPartID(enterpriseSlug, strconv.FormatInt(te.ID, 10))) | ||
| if err := d.Set("enterprise_slug", enterpriseSlug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("slug", te.Slug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("team_id", int(te.ID)); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("name", te.Name); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if te.Description != nil { | ||
| if err := d.Set("description", *te.Description); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else { | ||
| if err := d.Set("description", ""); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
| orgSel := "" | ||
| if te.OrganizationSelectionType != nil { | ||
| orgSel = *te.OrganizationSelectionType | ||
| } | ||
| if orgSel == "" { | ||
| orgSel = "disabled" | ||
| } | ||
| if err := d.Set("organization_selection_type", orgSel); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if te.GroupID != "" { | ||
| if err := d.Set("group_id", te.GroupID); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else { | ||
| if err := d.Set("group_id", ""); err != nil { | ||
| return diag.FromErr(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,74 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "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 dataSourceGithubEnterpriseTeamMembership() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Description: "Retrieves information about a user's membership in a GitHub enterprise team.", | ||
| ReadContext: dataSourceGithubEnterpriseTeamMembershipRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "enterprise_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "team_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise team.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "username": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The username of the user.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "user_id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| Description: "The ID of the user.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubEnterpriseTeamMembershipRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
| enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string)) | ||
| teamSlug := strings.TrimSpace(d.Get("team_slug").(string)) | ||
| username := strings.TrimSpace(d.Get("username").(string)) | ||
|
|
||
| // Get the membership using the SDK | ||
| user, _, err := client.Enterprise.GetTeamMembership(ctx, enterpriseSlug, teamSlug, username) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| d.SetId(buildEnterpriseTeamMembershipID(enterpriseSlug, teamSlug, username)) | ||
| if err := d.Set("enterprise_slug", enterpriseSlug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("team_slug", teamSlug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("username", username); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if user != nil && user.ID != nil { | ||
| if err := d.Set("user_id", int(*user.ID)); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
68 changes: 68 additions & 0 deletions
68
github/data_source_github_enterprise_team_organizations.go
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,68 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "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 dataSourceGithubEnterpriseTeamOrganizations() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Description: "Lists organizations assigned to a GitHub enterprise team.", | ||
| ReadContext: dataSourceGithubEnterpriseTeamOrganizationsRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "enterprise_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "team_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise team.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)), | ||
| }, | ||
| "organization_slugs": { | ||
| Type: schema.TypeSet, | ||
| Computed: true, | ||
| Description: "Set of organization slugs that the enterprise team is assigned to.", | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| Set: schema.HashString, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubEnterpriseTeamOrganizationsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
| enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string)) | ||
| teamSlug := strings.TrimSpace(d.Get("team_slug").(string)) | ||
| orgs, err := listAllEnterpriseTeamOrganizations(ctx, client, enterpriseSlug, teamSlug) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| slugs := make([]string, 0, len(orgs)) | ||
| for _, org := range orgs { | ||
| if org.Login != nil && *org.Login != "" { | ||
| slugs = append(slugs, *org.Login) | ||
| } | ||
| } | ||
|
|
||
| d.SetId(buildEnterpriseTeamOrganizationsID(enterpriseSlug, teamSlug)) | ||
| if err := d.Set("enterprise_slug", enterpriseSlug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("team_slug", teamSlug); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("organization_slugs", slugs); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| return nil | ||
| } |
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.