Skip to content

Commit 1f462ba

Browse files
Merge remote-tracking branch 'origin/enterprise-teams' into release-vfork
# Conflicts: # github/provider.go
2 parents a704658 + 3d9ae09 commit 1f462ba

19 files changed

+2036
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/google/go-github/v82/github"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12+
)
13+
14+
func dataSourceGithubEnterpriseTeam() *schema.Resource {
15+
return &schema.Resource{
16+
Description: "Gets information about a GitHub enterprise team.",
17+
ReadContext: dataSourceGithubEnterpriseTeamRead,
18+
19+
Schema: map[string]*schema.Schema{
20+
"enterprise_slug": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
Description: "The slug of the enterprise.",
24+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
25+
},
26+
"slug": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Computed: true,
30+
ExactlyOneOf: []string{"slug", "team_id"},
31+
Description: "The slug of the enterprise team.",
32+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
33+
},
34+
"team_id": {
35+
Type: schema.TypeInt,
36+
Optional: true,
37+
Computed: true,
38+
ExactlyOneOf: []string{"slug", "team_id"},
39+
Description: "The numeric ID of the enterprise team.",
40+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
41+
},
42+
"name": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: "The name of the enterprise team.",
46+
},
47+
"description": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "A description of the enterprise team.",
51+
},
52+
"organization_selection_type": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "Specifies which organizations in the enterprise should have access to this team.",
56+
},
57+
"group_id": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: "The ID of the IdP group to assign team membership with.",
61+
},
62+
},
63+
}
64+
}
65+
66+
func dataSourceGithubEnterpriseTeamRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
67+
client := meta.(*Owner).v3client
68+
enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string))
69+
70+
var te *github.EnterpriseTeam
71+
if v, ok := d.GetOk("team_id"); ok {
72+
teamID := int64(v.(int))
73+
if teamID != 0 {
74+
found, err := findEnterpriseTeamByID(ctx, client, enterpriseSlug, teamID)
75+
if err != nil {
76+
return diag.FromErr(err)
77+
}
78+
if found == nil {
79+
return diag.Errorf("could not find enterprise team %d in enterprise %s", teamID, enterpriseSlug)
80+
}
81+
te = found
82+
}
83+
}
84+
85+
if te == nil {
86+
teamSlug := strings.TrimSpace(d.Get("slug").(string))
87+
if teamSlug == "" {
88+
return diag.Errorf("one of slug or team_id must be set")
89+
}
90+
found, _, err := client.Enterprise.GetTeam(ctx, enterpriseSlug, teamSlug)
91+
if err != nil {
92+
return diag.FromErr(err)
93+
}
94+
te = found
95+
}
96+
97+
d.SetId(buildTwoPartID(enterpriseSlug, strconv.FormatInt(te.ID, 10)))
98+
if err := d.Set("enterprise_slug", enterpriseSlug); err != nil {
99+
return diag.FromErr(err)
100+
}
101+
if err := d.Set("slug", te.Slug); err != nil {
102+
return diag.FromErr(err)
103+
}
104+
if err := d.Set("team_id", int(te.ID)); err != nil {
105+
return diag.FromErr(err)
106+
}
107+
if err := d.Set("name", te.Name); err != nil {
108+
return diag.FromErr(err)
109+
}
110+
if te.Description != nil {
111+
if err := d.Set("description", *te.Description); err != nil {
112+
return diag.FromErr(err)
113+
}
114+
} else {
115+
if err := d.Set("description", ""); err != nil {
116+
return diag.FromErr(err)
117+
}
118+
}
119+
orgSel := ""
120+
if te.OrganizationSelectionType != nil {
121+
orgSel = *te.OrganizationSelectionType
122+
}
123+
if orgSel == "" {
124+
orgSel = "disabled"
125+
}
126+
if err := d.Set("organization_selection_type", orgSel); err != nil {
127+
return diag.FromErr(err)
128+
}
129+
if te.GroupID != "" {
130+
if err := d.Set("group_id", te.GroupID); err != nil {
131+
return diag.FromErr(err)
132+
}
133+
} else {
134+
if err := d.Set("group_id", ""); err != nil {
135+
return diag.FromErr(err)
136+
}
137+
}
138+
139+
return nil
140+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
10+
)
11+
12+
func dataSourceGithubEnterpriseTeamMembership() *schema.Resource {
13+
return &schema.Resource{
14+
Description: "Retrieves information about a user's membership in a GitHub enterprise team.",
15+
ReadContext: dataSourceGithubEnterpriseTeamMembershipRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"enterprise_slug": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "The slug of the enterprise.",
22+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
23+
},
24+
"team_slug": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
Description: "The slug of the enterprise team.",
28+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
29+
},
30+
"username": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: "The username of the user.",
34+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
35+
},
36+
"user_id": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
Description: "The ID of the user.",
40+
},
41+
},
42+
}
43+
}
44+
45+
func dataSourceGithubEnterpriseTeamMembershipRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
46+
client := meta.(*Owner).v3client
47+
enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string))
48+
teamSlug := strings.TrimSpace(d.Get("team_slug").(string))
49+
username := strings.TrimSpace(d.Get("username").(string))
50+
51+
// Get the membership using the SDK
52+
user, _, err := client.Enterprise.GetTeamMembership(ctx, enterpriseSlug, teamSlug, username)
53+
if err != nil {
54+
return diag.FromErr(err)
55+
}
56+
57+
d.SetId(buildEnterpriseTeamMembershipID(enterpriseSlug, teamSlug, username))
58+
if err := d.Set("enterprise_slug", enterpriseSlug); err != nil {
59+
return diag.FromErr(err)
60+
}
61+
if err := d.Set("team_slug", teamSlug); err != nil {
62+
return diag.FromErr(err)
63+
}
64+
if err := d.Set("username", username); err != nil {
65+
return diag.FromErr(err)
66+
}
67+
if user != nil && user.ID != nil {
68+
if err := d.Set("user_id", int(*user.ID)); err != nil {
69+
return diag.FromErr(err)
70+
}
71+
}
72+
73+
return nil
74+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
10+
)
11+
12+
func dataSourceGithubEnterpriseTeamOrganizations() *schema.Resource {
13+
return &schema.Resource{
14+
Description: "Lists organizations assigned to a GitHub enterprise team.",
15+
ReadContext: dataSourceGithubEnterpriseTeamOrganizationsRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"enterprise_slug": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "The slug of the enterprise.",
22+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
23+
},
24+
"team_slug": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
Description: "The slug of the enterprise team.",
28+
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringIsNotEmpty)),
29+
},
30+
"organization_slugs": {
31+
Type: schema.TypeSet,
32+
Computed: true,
33+
Description: "Set of organization slugs that the enterprise team is assigned to.",
34+
Elem: &schema.Schema{Type: schema.TypeString},
35+
Set: schema.HashString,
36+
},
37+
},
38+
}
39+
}
40+
41+
func dataSourceGithubEnterpriseTeamOrganizationsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
42+
client := meta.(*Owner).v3client
43+
enterpriseSlug := strings.TrimSpace(d.Get("enterprise_slug").(string))
44+
teamSlug := strings.TrimSpace(d.Get("team_slug").(string))
45+
orgs, err := listAllEnterpriseTeamOrganizations(ctx, client, enterpriseSlug, teamSlug)
46+
if err != nil {
47+
return diag.FromErr(err)
48+
}
49+
50+
slugs := make([]string, 0, len(orgs))
51+
for _, org := range orgs {
52+
if org.Login != nil && *org.Login != "" {
53+
slugs = append(slugs, *org.Login)
54+
}
55+
}
56+
57+
d.SetId(buildEnterpriseTeamOrganizationsID(enterpriseSlug, teamSlug))
58+
if err := d.Set("enterprise_slug", enterpriseSlug); err != nil {
59+
return diag.FromErr(err)
60+
}
61+
if err := d.Set("team_slug", teamSlug); err != nil {
62+
return diag.FromErr(err)
63+
}
64+
if err := d.Set("organization_slugs", slugs); err != nil {
65+
return diag.FromErr(err)
66+
}
67+
return nil
68+
}

0 commit comments

Comments
 (0)