-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathdata_source_github_enterprise_scim_groups.go
More file actions
179 lines (164 loc) · 4.75 KB
/
data_source_github_enterprise_scim_groups.go
File metadata and controls
179 lines (164 loc) · 4.75 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package github
import (
"context"
"fmt"
"net/url"
"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 dataSourceGithubEnterpriseSCIMGroups() *schema.Resource {
return &schema.Resource{
Description: "Retrieves SCIM groups provisioned for a GitHub enterprise.",
ReadContext: dataSourceGithubEnterpriseSCIMGroupsRead,
Schema: map[string]*schema.Schema{
"enterprise": {
Description: "The enterprise slug.",
Type: schema.TypeString,
Required: true,
},
"filter": {
Description: "Optional SCIM filter. See GitHub SCIM enterprise docs for supported filters.",
Type: schema.TypeString,
Optional: true,
},
"results_per_page": {
Description: "Number of results per request (mapped to SCIM 'count'). Used while auto-fetching all pages.",
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(1, 100)),
},
"schemas": {
Description: "SCIM response schemas.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"total_results": {
Description: "The total number of results returned by the SCIM endpoint.",
Type: schema.TypeInt,
Computed: true,
},
"start_index": {
Description: "The startIndex from the first SCIM page.",
Type: schema.TypeInt,
Computed: true,
},
"items_per_page": {
Description: "The itemsPerPage from the first SCIM page.",
Type: schema.TypeInt,
Computed: true,
},
"resources": {
Description: "All SCIM groups.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: enterpriseSCIMGroupSchema(),
},
},
},
}
}
func dataSourceGithubEnterpriseSCIMGroupsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
enterprise := d.Get("enterprise").(string)
filter := d.Get("filter").(string)
count := d.Get("results_per_page").(int)
groups, first, err := enterpriseSCIMListAllGroups(ctx, client, enterprise, filter, count)
if err != nil {
return diag.FromErr(err)
}
flat := make([]any, 0, len(groups))
for _, group := range groups {
flat = append(flat, flattenEnterpriseSCIMGroup(group))
}
id := fmt.Sprintf("%s/scim-groups", enterprise)
if filter != "" {
id = fmt.Sprintf("%s?filter=%s", id, url.QueryEscape(filter))
}
d.SetId(id)
if err := d.Set("schemas", first.Schemas); err != nil {
return diag.FromErr(err)
}
if first.TotalResults != nil {
if err := d.Set("total_results", *first.TotalResults); err != nil {
return diag.FromErr(err)
}
}
startIndex := 1
if first.StartIndex != nil && *first.StartIndex > 0 {
startIndex = *first.StartIndex
}
if err := d.Set("start_index", startIndex); err != nil {
return diag.FromErr(err)
}
itemsPerPage := count
if first.ItemsPerPage != nil && *first.ItemsPerPage > 0 {
itemsPerPage = *first.ItemsPerPage
}
if err := d.Set("items_per_page", itemsPerPage); err != nil {
return diag.FromErr(err)
}
if err := d.Set("resources", flat); err != nil {
return diag.FromErr(err)
}
return nil
}
func enterpriseSCIMGroupSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"schemas": {
Type: schema.TypeList,
Computed: true,
Description: "SCIM schemas for this group.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The SCIM group ID.",
},
"external_id": {
Type: schema.TypeString,
Computed: true,
Description: "The external ID for the group.",
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "The SCIM group displayName.",
},
"members": {
Type: schema.TypeList,
Computed: true,
Description: "Group members.",
Elem: &schema.Resource{Schema: enterpriseSCIMGroupMemberSchema()},
},
"meta": {
Type: schema.TypeList,
Computed: true,
Description: "Resource metadata.",
Elem: &schema.Resource{Schema: enterpriseSCIMMetaSchema()},
},
}
}
func enterpriseSCIMGroupMemberSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Member identifier.",
},
"ref": {
Type: schema.TypeString,
Computed: true,
Description: "Member reference URL.",
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "Member display name.",
},
}
}