forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignmentGroups_data_source.go
More file actions
297 lines (260 loc) · 9.83 KB
/
assignmentGroups_data_source.go
File metadata and controls
297 lines (260 loc) · 9.83 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package provider
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/DavidKrau/simplemdm-go-client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &assignmentGroupsDataSource{}
_ datasource.DataSourceWithConfigure = &assignmentGroupsDataSource{}
)
type assignmentGroupsDataSource struct {
client *simplemdm.Client
}
type assignmentGroupsDataSourceModel struct {
AssignmentGroups []assignmentGroupsDataSourceGroupModel `tfsdk:"assignment_groups"`
}
type assignmentGroupsDataSourceGroupModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
AutoDeploy types.Bool `tfsdk:"auto_deploy"`
GroupType types.String `tfsdk:"group_type"`
InstallType types.String `tfsdk:"install_type"`
Priority types.Int64 `tfsdk:"priority"`
AppTrackLocation types.Bool `tfsdk:"app_track_location"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
DeviceCount types.Int64 `tfsdk:"device_count"`
GroupCount types.Int64 `tfsdk:"group_count"`
Apps types.Set `tfsdk:"apps"`
Profiles types.Set `tfsdk:"profiles"`
Devices types.Set `tfsdk:"devices"`
Groups types.Set `tfsdk:"groups"`
}
func AssignmentGroupsDataSource() datasource.DataSource {
return &assignmentGroupsDataSource{}
}
func (d *assignmentGroupsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_assignmentgroups"
}
func (d *assignmentGroupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetches the collection of assignment groups from your SimpleMDM account.",
Blocks: map[string]schema.Block{
"assignment_groups": schema.ListNestedBlock{
Description: "Collection of assignment group records returned by the API.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Assignment group identifier.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the assignment group.",
},
"auto_deploy": schema.BoolAttribute{
Computed: true,
Description: "Whether the assignment group automatically deploys apps.",
},
"group_type": schema.StringAttribute{
Computed: true,
Description: "The type of assignment group (standard or munki).",
},
"install_type": schema.StringAttribute{
Computed: true,
Description: "Install type used when the assignment group is of type munki.",
},
"priority": schema.Int64Attribute{
Computed: true,
Description: "The priority of the assignment group.",
},
"app_track_location": schema.BoolAttribute{
Computed: true,
Description: "Whether the SimpleMDM app tracks device location when installed for this assignment group.",
},
"created_at": schema.StringAttribute{
Computed: true,
Description: "Timestamp when the assignment group was created.",
},
"updated_at": schema.StringAttribute{
Computed: true,
Description: "Timestamp when the assignment group was last updated.",
},
"device_count": schema.Int64Attribute{
Computed: true,
Description: "Number of devices currently assigned to the assignment group.",
},
"group_count": schema.Int64Attribute{
Computed: true,
Description: "Number of device groups currently assigned to the assignment group.",
},
"apps": schema.SetAttribute{
Computed: true,
ElementType: types.StringType,
Description: "IDs of apps assigned to the assignment group.",
},
"profiles": schema.SetAttribute{
Computed: true,
ElementType: types.StringType,
Description: "IDs of profiles assigned to the assignment group.",
},
"devices": schema.SetAttribute{
Computed: true,
ElementType: types.StringType,
Description: "IDs of devices assigned directly to the assignment group.",
},
"groups": schema.SetAttribute{
Computed: true,
ElementType: types.StringType,
Description: "IDs of device groups assigned to the assignment group.",
},
},
},
},
},
}
}
func (d *assignmentGroupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config assignmentGroupsDataSourceModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
groups, err := fetchAllAssignmentGroups(ctx, d.client)
if err != nil {
resp.Diagnostics.AddError(
"Unable to list SimpleMDM assignment groups",
err.Error(),
)
return
}
entries := make([]assignmentGroupsDataSourceGroupModel, 0, len(groups))
for _, group := range groups {
entry := assignmentGroupsDataSourceGroupModel{
ID: types.StringValue(strconv.Itoa(group.ID)),
Name: types.StringValue(group.Attributes.Name),
AutoDeploy: types.BoolValue(group.Attributes.AutoDeploy),
GroupType: types.StringValue(group.Attributes.GroupType),
Priority: types.Int64Value(int64(group.Attributes.Priority)),
AppTrackLocation: types.BoolValue(group.Attributes.AppTrackLocation),
DeviceCount: types.Int64Value(int64(group.Attributes.DeviceCount)),
GroupCount: types.Int64Value(int64(group.Attributes.GroupCount)),
}
// Set install_type if available
if group.Attributes.GroupType == "munki" && group.Attributes.InstallType != "" {
entry.InstallType = types.StringValue(group.Attributes.InstallType)
} else {
entry.InstallType = types.StringNull()
}
// Set timestamps if available
if group.Attributes.CreatedAt != "" {
entry.CreatedAt = types.StringValue(group.Attributes.CreatedAt)
} else {
entry.CreatedAt = types.StringNull()
}
if group.Attributes.UpdatedAt != "" {
entry.UpdatedAt = types.StringValue(group.Attributes.UpdatedAt)
} else {
entry.UpdatedAt = types.StringNull()
}
// Set relationships
entry.Apps = buildStringSetFromRelationshipItems(group.Relationships.Apps.Data)
entry.Profiles = buildStringSetFromRelationshipItems(group.Relationships.Profiles.Data)
entry.Devices = buildStringSetFromRelationshipItems(group.Relationships.Devices.Data)
entry.Groups = buildStringSetFromRelationshipItems(group.Relationships.DeviceGroups.Data)
entries = append(entries, entry)
}
config.AssignmentGroups = entries
diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}
func (d *assignmentGroupsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*simplemdm.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
// fetchAllAssignmentGroups retrieves all assignment groups with pagination support
func fetchAllAssignmentGroups(ctx context.Context, client *simplemdm.Client) ([]assignmentGroupData, error) {
const maxPaginationIterations = 1000
var allGroups []assignmentGroupData
startingAfter := 0
limit := 100
iterations := 0
for {
if iterations >= maxPaginationIterations {
return nil, fmt.Errorf("exceeded maximum pagination iterations (%d), possibly too many groups or API error", maxPaginationIterations)
}
iterations++
// Check context cancellation
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("operation cancelled: %w", err)
}
url := fmt.Sprintf("https://%s/api/v1/assignment_groups?limit=%d", client.HostName, limit)
if startingAfter > 0 {
url += fmt.Sprintf("&starting_after=%d", startingAfter)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
body, err := client.RequestResponse200(req)
if err != nil {
return nil, err
}
var response assignmentGroupsAPIResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
allGroups = append(allGroups, response.Data...)
if !response.HasMore {
break
}
if len(response.Data) > 0 {
startingAfter = response.Data[len(response.Data)-1].ID
} else {
break
}
}
return allGroups, nil
}
// assignmentGroupsAPIResponse represents the paginated API response for assignment groups list
type assignmentGroupsAPIResponse struct {
Data []assignmentGroupData `json:"data"`
HasMore bool `json:"has_more"`
}
// assignmentGroupData represents a single assignment group in the list response
type assignmentGroupData struct {
ID int `json:"id"`
Type string `json:"type"`
Attributes assignmentGroupDataAttributes `json:"attributes"`
Relationships assignmentGroupRelationships `json:"relationships"`
}
type assignmentGroupDataAttributes struct {
Name string `json:"name"`
AutoDeploy bool `json:"auto_deploy"`
GroupType string `json:"group_type"`
InstallType string `json:"install_type"`
Priority int `json:"priority"`
AppTrackLocation bool `json:"app_track_location"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeviceCount int `json:"device_count"`
GroupCount int `json:"group_count"`
}