forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceGroups_data_source.go
More file actions
178 lines (147 loc) · 4.85 KB
/
deviceGroups_data_source.go
File metadata and controls
178 lines (147 loc) · 4.85 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
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 = &deviceGroupsDataSource{}
_ datasource.DataSourceWithConfigure = &deviceGroupsDataSource{}
)
type deviceGroupsDataSource struct {
client *simplemdm.Client
}
type deviceGroupsDataSourceModel struct {
DeviceGroups []deviceGroupsDataSourceGroupModel `tfsdk:"device_groups"`
}
type deviceGroupsDataSourceGroupModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func DeviceGroupsDataSource() datasource.DataSource {
return &deviceGroupsDataSource{}
}
func (d *deviceGroupsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_devicegroups"
}
func (d *deviceGroupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "⚠️ DEPRECATED: Device Groups have been superseded by Assignment Groups in SimpleMDM. " +
"Please use the simplemdm_assignmentgroups data source instead. " +
"This data source is maintained for backward compatibility only. " +
"Fetches the collection of device groups from your SimpleMDM account.",
DeprecationMessage: "Device Groups are deprecated. Use simplemdm_assignmentgroups instead.",
Blocks: map[string]schema.Block{
"device_groups": schema.ListNestedBlock{
Description: "Collection of device group records returned by the API.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Device group identifier.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Device group name.",
},
},
},
},
},
}
}
func (d *deviceGroupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config deviceGroupsDataSourceModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
deviceGroups, err := fetchAllDeviceGroups(ctx, d.client)
if err != nil {
resp.Diagnostics.AddError(
"Unable to list SimpleMDM device groups",
err.Error(),
)
return
}
entries := make([]deviceGroupsDataSourceGroupModel, 0, len(deviceGroups))
for _, group := range deviceGroups {
entry := deviceGroupsDataSourceGroupModel{
ID: types.StringValue(strconv.Itoa(group.ID)),
Name: types.StringValue(group.Attributes.Name),
}
entries = append(entries, entry)
}
config.DeviceGroups = entries
diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}
func (d *deviceGroupsDataSource) 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
}
// fetchAllDeviceGroups retrieves all device groups with pagination support
func fetchAllDeviceGroups(ctx context.Context, client *simplemdm.Client) ([]deviceGroupData, error) {
var allGroups []deviceGroupData
startingAfter := 0
limit := 100
for {
url := fmt.Sprintf("https://%s/api/v1/device_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 deviceGroupsAPIResponse
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
}
// deviceGroupsAPIResponse represents the paginated API response
type deviceGroupsAPIResponse struct {
Data []deviceGroupData `json:"data"`
HasMore bool `json:"has_more"`
}
// deviceGroupData represents a single device group in the list response
type deviceGroupData struct {
ID int `json:"id"`
Type string `json:"type"`
Attributes deviceGroupAttributes `json:"attributes"`
}
type deviceGroupAttributes struct {
Name string `json:"name"`
}