-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathdata_source_github_enterprise_team.go
More file actions
135 lines (126 loc) · 3.89 KB
/
data_source_github_enterprise_team.go
File metadata and controls
135 lines (126 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package github
import (
"context"
"strconv"
"strings"
"github.com/google/go-github/v84/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))
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))
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
}