forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrollments_data_source.go
More file actions
187 lines (162 loc) · 5.92 KB
/
enrollments_data_source.go
File metadata and controls
187 lines (162 loc) · 5.92 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
package provider
import (
"context"
"fmt"
"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 = &enrollmentsDataSource{}
_ datasource.DataSourceWithConfigure = &enrollmentsDataSource{}
)
type enrollmentsDataSource struct {
client *simplemdm.Client
}
type enrollmentsDataSourceModel struct {
Enrollments []enrollmentsDataSourceEnrollmentModel `tfsdk:"enrollments"`
}
type enrollmentsDataSourceEnrollmentModel struct {
ID types.String `tfsdk:"id"`
DeviceGroupID types.String `tfsdk:"device_group_id"`
AssignmentGroupID types.String `tfsdk:"assignment_group_id"`
URL types.String `tfsdk:"url"`
UserEnrollment types.Bool `tfsdk:"user_enrollment"`
WelcomeScreen types.Bool `tfsdk:"welcome_screen"`
Authentication types.Bool `tfsdk:"authentication"`
DeviceID types.String `tfsdk:"device_id"`
AccountDriven types.Bool `tfsdk:"account_driven"`
}
func EnrollmentsDataSource() datasource.DataSource {
return &enrollmentsDataSource{}
}
func (d *enrollmentsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_enrollments"
}
func (d *enrollmentsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetches the collection of enrollments from your SimpleMDM account. " +
"One-time enrollments have a URL and can be used once. Account driven enrollments have a null URL and can be used multiple times.",
Blocks: map[string]schema.Block{
"enrollments": schema.ListNestedBlock{
Description: "Collection of enrollment records returned by the API.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Enrollment identifier.",
},
"device_group_id": schema.StringAttribute{
Computed: true,
Description: "Legacy device group associated with the enrollment (deprecated).",
},
"assignment_group_id": schema.StringAttribute{
Computed: true,
Description: "Assignment group associated with the enrollment (for New Groups Experience).",
},
"url": schema.StringAttribute{
Computed: true,
Description: "Enrollment URL for one-time enrollments. Will be null for account driven enrollments.",
},
"user_enrollment": schema.BoolAttribute{
Computed: true,
Description: "Whether this is a user enrollment.",
},
"welcome_screen": schema.BoolAttribute{
Computed: true,
Description: "Whether welcome screen is shown.",
},
"authentication": schema.BoolAttribute{
Computed: true,
Description: "Whether authentication is required.",
},
"device_id": schema.StringAttribute{
Computed: true,
Description: "Device that used this enrollment link.",
},
"account_driven": schema.BoolAttribute{
Computed: true,
Description: "Whether enrollment is account driven (URL is null).",
},
},
},
},
},
}
}
func (d *enrollmentsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config enrollmentsDataSourceModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
enrollments, err := fetchAllEnrollments(ctx, d.client)
if err != nil {
resp.Diagnostics.AddError(
"Unable to list SimpleMDM enrollments",
err.Error(),
)
return
}
entries := make([]enrollmentsDataSourceEnrollmentModel, 0, len(enrollments))
for _, enrollment := range enrollments {
flat := flattenEnrollment(&enrollment)
entry := enrollmentsDataSourceEnrollmentModel{
ID: types.StringValue(strconv.Itoa(flat.ID)),
UserEnrollment: types.BoolValue(flat.UserEnrollment),
WelcomeScreen: types.BoolValue(flat.WelcomeScreen),
Authentication: types.BoolValue(flat.Authentication),
}
if flat.DeviceGroupID != nil {
entry.DeviceGroupID = types.StringValue(strconv.Itoa(*flat.DeviceGroupID))
} else {
entry.DeviceGroupID = types.StringNull()
}
if flat.AssignmentGroupID != nil {
entry.AssignmentGroupID = types.StringValue(strconv.Itoa(*flat.AssignmentGroupID))
} else {
entry.AssignmentGroupID = types.StringNull()
}
// More defensive URL null handling
if flat.URL == nil {
entry.URL = types.StringNull()
entry.AccountDriven = types.BoolValue(true)
} else if *flat.URL == "" {
entry.URL = types.StringNull()
entry.AccountDriven = types.BoolValue(true)
} else {
entry.URL = types.StringValue(*flat.URL)
entry.AccountDriven = types.BoolValue(false)
}
if flat.DeviceID != nil {
entry.DeviceID = types.StringValue(strconv.Itoa(*flat.DeviceID))
} else {
entry.DeviceID = types.StringNull()
}
entries = append(entries, entry)
}
config.Enrollments = entries
diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}
func (d *enrollmentsDataSource) 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
}
// fetchAllEnrollments retrieves all enrollments with pagination support
func fetchAllEnrollments(ctx context.Context, client *simplemdm.Client) ([]enrollmentResponse, error) {
return listEnrollments(ctx, client, 0)
}